软件构造.reuse.复用

博客介绍了可复用组件的层次和形态,重点阐述了类的多种复用方式,包括继承、委托、使用、关联和组合,还介绍了钩子方法。继承可扩展基类,委托依赖对象功能子集,钩子方法能实现应用扩展并解耦接口与行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Levels and morphology(形态) of reusable components

1.1
在这里插入图片描述
1.2 Types of Code Reuse
Types of Code Reuse

2.Approaches of reusing a class: inheritance

继承:利用extends来扩展一个基类。
inheritance:继承,java可以继承其他已经存在的类别的方法和属性,可以在继承的基础上进行重写,已经继承的属性和方法不能取消。

3.Approaches of reusing a class: delegation 委托

委托:一个对象请求另一个对象的功能,捕获一个操作并将其发送到另一个对象。
delegation:委托就是一个对象依赖于另一个对象的功能子集。(eg.一个对象调用另一个对象的某些功能和方法)
关系图如下:
在这里插入图片描述
eg
下面的LoggingList类中调用了println方法来实现类
在这里插入图片描述
eg

public class A {
    void foo() {
        this.bar(); 
    }
    void bar() { 
        System.out.println("a.bar");
    }
}
public class B {
    private A a;
    public B(A a) {
        this.a = a; 
    }
    void foo() {
        a.foo(); // call foo() on the a-instance }
    }
    void bar() {
        System.out.println("b.bar");
    }
}

A a = new A();
B b = new B(a); 
b.foo();//a.bar
b.bar();//b.bar

4.Approaches of reusing a class: use

use:顾名思义,使用对象对应类自己含有的字段或者方法
在这里插入图片描述

5.Approaches of reusing a class: association

association:你中有我,我中有你的关系
下图中老师和学生之间的字段或者方法可能互相存在着交际
但是下图中的学生和课程之间只可能是学生拥有课程,不可能是课程拥有学生
在这里插入图片描述

6.Approaches of reusing a class: composition

composition:就是一个东西是另一个大东西的组成部分
eg心脏是人的组成部分
在这里插入图片描述

7. Hook methods

Extensibility is provided by hook methods, which are overwritten by the application to extend the framework. 通过hook methods实现扩展

Hook methods systematically decouple the interfaces and behaviors of an application domain from the variations required by an application in a particular context. Hook methods 钩子方法:是对抽象方法的一个空实现,允许需要时通过继承,override将具体实现挂载上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值