前言:【模式總覽】——————————by xingoohtml
這個模式使用的並很少,可是思想確實很廣泛。就是要分離抽象部分與實現部分。spa
實現弱關聯,即在運行時才產生依賴關係。code
下降代碼之間的耦合。htm
Abstraction 抽象部分的基類,定義抽象部分的基礎內容。blog
RefinedAbstraction 抽象部分的擴充,用於對基類的內容補充,添加特定場景的業務操做。get
Implementor 實現部分的基類,定義實現部分的基本內容。io
ConcreteImplementor 具體的實現類。class
1 不但願在抽象和它的實現部分之間有一個固定的綁定關係test
2 抽象部分以及實現部分都想經過子類生成必定的擴充內容基礎
3 對一個抽象的實現部分的修改對客戶不產生影響
1 package com.xingoo.test; 2 /** 3 * 抽象類基類 4 * @author xingoo 5 */ 6 abstract class Abstraction{ 7 abstract public void operation(Implementor imp); 8 } 9 /** 10 * 實現類 基類 11 * @author xingoo 12 */ 13 abstract class Implementor{ 14 abstract public void operation(); 15 } 16 /** 17 * 從新定義的抽象類 18 * @author xingoo 19 */ 20 class RefinedAbstraction extends Abstraction{ 21 public void operation(Implementor imp){ 22 imp.operation(); 23 System.out.println("RefinedAbstraction"); 24 } 25 } 26 /** 27 * 具體的實現類 28 * @author xingoo 29 */ 30 class ConcreteImplementorA extends Implementor{ 31 public void operation() { 32 System.out.println("ConcreteImplementorA"); 33 } 34 } 35 /** 36 * 具體的實現類 37 * @author xingoo 38 */ 39 class ConcreteImplementorB extends Implementor{ 40 public void operation() { 41 System.out.println("ConcreteImplementorB"); 42 } 43 } 44 public class test { 45 public static void main(String[] args){ 46 RefinedAbstraction abstraction = new RefinedAbstraction(); 47 abstraction.operation(new ConcreteImplementorA()); 48 49 abstraction.operation(new ConcreteImplementorB()); 50 } 51 } 52
ConcreteImplementorA RefinedAbstraction ConcreteImplementorB RefinedAbstraction