1.Levels and morphology(形态) of reusable components
1.1
1.2 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将具体实现挂载上