四、结构性模式介绍
4.1 适配器模式(Adapter Pattern)
当我们需要在现有系统中使用一个类,但是这个类的接口与系统的要求不符合时,适配器模式就可以发挥作用。适配器模式通过引入一个中间件(适配器)来转换原始类的接口,使得原始类可以与系统进行适配。能够帮助解决不同接口之间的兼容性问题,提高系统的可维护性和可扩展性。
适配器模式的角色有:
- 目标(Target)接口
- 适配者(Adaptee)类
- 适配器(Adapter)类
4.1.1 类结构性适配器
场景
一个帅哥用华为手机看动物世界,一只小狗如何才能听懂华为手机的再讲什么呢,这时候就需要一个翻译功能,把人类的语言翻译成小狗的语言
1.定义一个接口 Talk 、以及实现类 HuaWeiPhone (手机播放的汉语)
java
public interface Talk {
String talk();
}
public class HuaWeiPhone implements Talk {
@Override
public String talk() {
System.out.println("手机--小狗翻译");
String content = "你在狗叫什么";
System.out.println(content);
return content;
}
}
2.定义一个接口 Translator 、实现类 DogTalkTranslator (将汉语转换为狗语)
java
public interface Translator {
String translate(String content);
}
public class DogTalkTranslator implements Translator{
@Override
public String translate(String content) {
if("你在狗叫什么".equals(content)){
return "汪汪汪汪汪汪";
}
if ("走开".equals(content)){
return "汪汪";
}
return "--------";
}
}
3.定义一个适配器类DogTalkAdapter
java
public class DogTalkAdapter extends DogTalkTranslator implements Talk {
private Talk target;//被适配对象
public DogTalkAdapter(Talk target){
this.target = target;
}
@Override
public String talk() {
String talk = target.talk();
//将汉语转化为狗语
String translate = translate(talk);
System.out.println("狗语:"+translate);
return talk;
}
}
4.测试类测试
java
public class MainTest {
public static void main(String[] args) {
HuaWeiPhone huaWeiPhone = new HuaWeiPhone();
DogTalkAdapter dogTalkAdapter = new DogTalkAdapter(huaWeiPhone);
huaWeiPhone.talk();
dogTalkAdapter.talk();
}
}
4.1.2 对象结构性适配器
1.定义一个适配器DogTalkAdapter (这里通过组合在对象的形式)
Java
public class DogTalkAdapter implements Talk {
//组合的方式
private Translator translator = new DogTalkTranslator();
private Talk target;//被适配对象
public DogTalkAdapter(Talk target){
this.target = target;
}
@Override
public String talk() {
String play = target.talk();
//转换语言
String translate = translator.translate(play);
System.out.println("狗语:"+translate);
return play;
}
}
2.测试类测试
java
public class MainTest {
public static void main(String[] args) {
DogTalkAdapter adapter = new DogTalkAdapter(new HuaWeiPhone());
adapter.talk();
}
}
4.2 桥接模式(Bridge Pattern)
它将抽象部分与它的实现部分分离开来,使它们都可以独立地变化。这种模式就像一座桥,连接了两个不同的部分,让它们可以相互协作
------------------------------------场景模拟------------------------------------
假如有一个帅哥,他在德国和英国都有一栋房子,那么他可以选择骑自行车或者骑摩托车去,这时候的摩托车还有自行车就相当于一个连接方法,将帅哥和房子通过某种方式连接到一起
------------------------------------代码实现------------------------------------
1.定义 两个工具类 Bicycle 、 Motorcycle
java
public class Bicycle extends AbstractVehicle {
@Override
String getHome() {
return "自行车:"+traffic.getTrafficInfo();
}
}
public class Motorcycle extends AbstractVehicle {
@Override
String getHome() {
return "摩托车:"+traffic.getTrafficInfo();
}
}
2.定了两个目的地England 、Germany
java
public class England extends AbstractTraffic {
public England(String speed, String time) {
super(speed, time);
}
}
public class Germany extends AbstractTraffic {
public Germany(String speed, String time) {
super(speed, time);
}
}
3.定义一个抽象类AbstractTraffic (定义了车辆的速度和时间)
java
public abstract class AbstractTraffic {
private String speed;
private String time;
public AbstractTraffic(String speed, String time){
this.speed = speed;
this.time = time;
}
String getTrafficInfo(){
return "速度:"+ speed +"==>"+"时间:"+ time;
}
}
4.定义一个抽象类AbstractVehicle (桥接的地方)
java
public abstract class AbstractVehicle {
AbstractTraffic traffic; //分离渠道【桥接的关注点】
abstract String getHome();
public void setTraffic(AbstractTraffic traffic) {
this.traffic = traffic;
}
}
5.测试类测试
Java
public class MainTest {
public static void main(String[] args) {
Motorcycle motorcycle = new Motorcycle();
motorcycle.setTraffic(new Germany("180km/h","99分钟"));
System.out.println(motorcycle.getHome());
motorcycle.setTraffic(new England("180km/h","88分钟"));
System.out.println(motorcycle.getHome());
Bicycle bicycle = new Bicycle();
bicycle.setTraffic(new Germany("160km/h","77分钟"));
System.out.println(bicycle.getHome());
bicycle.setTraffic(new England("160km/h","66分钟"));
System.out.println(bicycle.getHome());
}
}