7、設計模式(結構型)之橋接模式
阿新 • • 發佈:2018-12-09
作用
- 將抽象部分與實現部分分離,使它們都可以獨立的變化
何時使用
- 實現系統可能有多個角度分類,每一種角度都可能變化
優點
- 抽象和實現的分離
- 優秀的擴充套件能力
- 實現細節對客戶透明
例項
下面以買電子產品為例
package xin.chen.structural_type.bridge;
/**
* @Author: create_By: chenxin
* @Data:Created in 2018/8/29 14:58
* @Version:
* @Acton: 品牌
*/
public interface Brand {
void sale();
}
package xin.chen.structural_type.bridge; public class Huawei implements Brand{ @Override public void sale() { System.out.println("銷售華為"); } }
package xin.chen.structural_type.bridge;
public class Xiaomi implements Brand{
@Override
public void sale() {
System.out.println("銷售小米");
}
}
package xin.chen.structural_type.bridge; /** * @Author: create_By: chenxin * @Data:Created in 2018/8/29 15:00 * @Version: * @Acton: 電腦 */ public class Computer { public Brand brand; public Computer(Brand brand){ this.brand = brand; } public void sale(){ this.brand.sale(); } }
package xin.chen.structural_type.bridge;
public class Desktop extends Computer{
public Desktop(Brand brand) {
super(brand);
}
@Override
public void sale() {
super.sale();
System.out.println("銷售臺式");
}
}
package xin.chen.structural_type.bridge; public class Laptop extends Computer{ public Laptop(Brand brand) { super(brand); } @Override public void sale() { super.sale(); System.out.println("銷售筆記本"); } }
結構圖