设计模式--结构模式--桥接模式

一、基本概念

     理解基本原理,本篇写的非常好:https://baijiahao.baidu.com/s?id=1639383166201396225&wfr=spider&for=pc

    1、桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。其中接口模式更能说明其含义。

    2、角色:

        A:client:测试使用。

        B:Abstraction(抽象类)例子中的汽车

        C:Refined Abstraction(具体类),例子中各种品牌的汽车

        D:Implementor:例子中的轮胎。

二、简单例子

        A:Abstraction(抽象类)例子中的汽车        

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: AbstractCard.java
 6  * @Package: comm.pattern.struct.bridge
 7  * @Description: 不同品牌的汽车
 8  * @author yangzhancheng
 9  * @2020年3月20日:下午10:54:29
10  *
11  */
12 public abstract class AbstractCard {
13     
14     public Tire tire;
15     
16     public AbstractCard(Tire tire){
17         this.tire = tire;
18     }
19     
20     public abstract void run();
21     
22 }
View Code

        B:Refined Abstraction(具体类),例子中各种品牌的汽车

        B-1:

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: BMWCard.java
 6  * @Package: comm.pattern.struct.bridge
 7  * @Description: 描述该文件做什么
 8  * @author yangzhancheng
 9  * @2020年3月20日:下午11:18:25
10  *
11  */
12 public class BMWCard extends AbstractCard{
13     
14     
15     public BMWCard(Tire tire) {
16         super(tire);
17     }
18 
19     public  void run(){
20         System.out.print("宝马车" );
21         super.tire.addTire();
22     }
23 
24 }
View Code

        B-2:

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: WWWCard.java
 6  * @Package: comm.pattern.struct.bridge
 7  * @Description: 描述该文件做什么
 8  * @author yangzhancheng
 9  * @2020年3月20日:下午11:18:17
10  *
11  */
12 public class WWWCard extends AbstractCard{
13     
14     
15     public WWWCard(Tire tire) {
16         super(tire);
17     }
18 
19     public  void run(){
20         System.out.print("大众车" );
21         super.tire.addTire();
22     }
23 
24 }
View Code

        C:Implementor:例子中的轮胎。

        C-1:

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: Tire.java
 6  * @Package: comm.pattern.struct.bridge
 7  * @Description: 轮胎
 8  * @author yangzhancheng
 9  * @2020年3月20日:下午11:11:22
10  *
11  */
12 public interface Tire {
13 
14     public void addTire();
15 }
View Code

       C-2:

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: KUMHOTire.java
 6  * @Package: comm.pattern.struct.bridge
 7  * @Description: 描述该文件做什么
 8  * @author yangzhancheng
 9  * @2020年3月20日:下午11:17:51
10  *
11  */
12 public class KUMHOTire implements Tire{
13 
14     public void addTire(){
15         System.out.println(",这辆车的轮胎是锦湖轮胎");
16     }
17     
18 }
View Code

       C-3:

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: MICHELINTire.java
 6  * @Package: comm.pattern.struct.bridge
 7  * @Description: 描述该文件做什么
 8  * @author yangzhancheng
 9  * @2020年3月20日:下午11:17:56
10  *
11  */
12 public class MICHELINTire implements Tire{
13 
14     public void addTire(){
15         System.out.println(",这辆车的轮胎是米其林轮胎");
16     }
17     
18 }
View Code

        D:client:测试使用。

 1 package comm.pattern.struct.bridge;
 2 
 3 /**
 4  * 
 5  * @Title: Client.java
 6  * @Package: comm.pattern.action.visitor
 7  * @Description: 描述该文件做什么
 8  * @author yangzhancheng
 9  * @2020年3月14日:下午1:04:10
10  *
11  */
12 public class Client {
13 
14     public static void main(String[] args) {
15     
16         AbstractCard bmwm = new BMWCard(new MICHELINTire());
17         bmwm.run();
18         AbstractCard bmwk = new BMWCard(new KUMHOTire());
19         bmwk.run();
20         
21         
22         AbstractCard wwwm = new WWWCard(new MICHELINTire());
23         wwwm.run();
24         AbstractCard wwwk = new WWWCard(new KUMHOTire());
25         wwwk.run();
26         
27         
28     }
29 
30 }
View Code

运行结果

宝马车,这辆车的轮胎是米其林轮胎
宝马车,这辆车的轮胎是锦湖轮胎
大众车,这辆车的轮胎是米其林轮胎
大众车,这辆车的轮胎是锦湖轮胎

三、总结

      后续补充

猜你喜欢

转载自www.cnblogs.com/fating/p/12535755.html
今日推荐