0%

设计模式

微信截图_20200921232226.png

设计模式

设计模式也是一个重要环节,值得花时间在上面。

设计模式

Adapter模式

Adapter模式即适配器模式,对于适配器的理解参考现实生活中把交流电转换成直流电的电源适配器,用于填补现有的程序所需的程序之间差异的设计模式就是Adapter模式,有以下两种实现方式:

 1. 类适配器模式(使用继承的适配器)
 2. 对象适配器模式(使用委托的适配器)
所谓继承和委托的区别在哪呢?委托是指将某个方法中的实际处理交给其他实例的方法,继承则是自己进行处理,下面分别看下两种实现方式:

这里有一个需要被适配的Banner类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 被适配角色:交流100福特电源
* @author guozhenZhao
* @date 2018年6月21日
*/
public class Banner {
private String string;

public Banner(String string) {
super();
this.string = string;
}

public void showWithParen() {
System.out.println("("+ string +")");
}

public void showWithAster() {
System.out.println("*"+ string +"*");
}
}