設計模式(二) 裝飾模式

 

裝飾模式

裝飾模式又叫包裝模式。裝飾模式指的是在沒必要改變原類文件和使用繼承的狀況下,動態地擴展一個對象的功能。它是經過建立一個包裝對象,也就是裝飾來包裹真實的對象。程序員

 

裝飾模式的類圖

 

 

 在裝飾模式中的角色有:ide

  ●  抽象構件(Component)角色:給出一個抽象接口,以規範準備接收附加責任的對象。測試

  ●  具體構件(ConcreteComponent)角色:定義一個將要接收附加責任的類。this

  ●  裝飾(Decorator)角色:持有一個構件(Component)對象的實例,spa

      並定義一個與抽象構件接口一致的接口。code

  ●  具體裝飾(ConcreteDecorator)角色:負責給構件對象「貼上」附加的責任。component

 

源代碼實現

 

抽象構件(Component)角色對象

1 public interface Component {
2 
3     /**抽象接口方法*/
4     void service();
5 
6 }

 

 

具體構件(ConcreteComponent)角色blog

 1 public class ConcreteComponent implements Component{
 2 
 3     public ConcreteComponent() {
 4     }
 5 
 6     @Override
 7     public void service() {
 8         System.out.println("我要接收附加責任");
 9     }
10 }

 

 

裝飾(Decorator)角色繼承

 1 public abstract class Decorator implements Component {
 2 
 3     private Component component;
 4 
 5     public Decorator(Component component) {
 6         this.component = component;
 7     }
 8 
 9     @Override
10     public void service() {
11         component.service();
12     }
13 }

 

 

具體裝飾(ConcreteDecoratorA)角色

 1 public class DecoratorA extends Decorator {
 2 
 3     public DecoratorA(Component component) {
 4         super(component);
 5     }
 6 
 7     @Override
 8     public void service() {
 9         super.service();
10         System.out.println("我是裝飾者A");
11     }
12 }

 

 

具體裝飾(ConcreteDecoratorB)角色

 1 public class DecoratorB extends Decorator {
 2 
 3     public DecoratorB(Component component) {
 4         super(component);
 5     }
 6 
 7     @Override
 8     public void service() {
 9         super.service();
10         System.out.println("我是裝飾者B");
11     }
12 }

 

真實案例

程序員這個行業,不免會遇到晚上加班的狀況。若是加班太晚回不去就須要打車。咱們以打車這件事情去理解裝飾模式。
咱們知道出租車是小汽車的一種。打車能夠分爲幾件事情去執行:叫車、等待、坐車、付費結束。

打車的類圖

 

 在裝飾模式中的角色有

  ●  抽象構件(Taxi)角色:給出一個抽象接口driving(),每一個出租車都能開。

  ●  具體構件(Car)角色:每一個小汽車開以前都有一些準備工做,點火、踩離合、掛擋、鬆手剎...

  ●  裝飾(Work)角色:持有一個構件(Taxi)對象的實例,

      並定義一個與抽象構件接口一致的driving()接口。並附加一個work()接口。

  ●  具體裝飾(CallTaxi、Wait、ByBus、PayMoney)角色:實現具體的附加責任。

 

源代碼實現

 

抽象構件(Taxi)角色

1 public interface Taxi {
2 
3     /**給出一個抽象接口driving(),每一個出租車都能開*/
4     void driving();
5 
6 }

 

 

 

 

具體構件(Car)角色

 1 public class Car implements Taxi {
 2 
 3     public Car() {
 4     }
 5 
 6     @Override
 7     public void driving() {
 8         //每一個小汽車開以前都有一些準備工做,點火、踩離合、掛擋、鬆手剎...
 9         System.out.println("第一步準備工做:點火、踩離合、掛擋、鬆手剎...");
10     }
11 }

 

 

 

裝飾(Work)角色

 1 public abstract class Work implements Taxi{
 2 
 3     private Taxi taxi;
 4 
 5     public Work(Taxi taxi) {
 6         this.taxi = taxi;
 7     }
 8 
 9     @Override
10     public void driving() {
11         taxi.driving();
12     }
13 }

 

 

 

 

具體裝飾(CallTaxi)角色

 1 public class CallTaxi extends Work {
 2 
 3     public CallTaxi(Taxi taxi) {
 4         super(taxi);
 5     }
 6 
 7     @Override
 8     public void driving() {
 9         super.driving();
10         System.out.println("第二步:我要叫車了...");
11     }
12 }

 


具體裝飾(Wait)角色

 1 public class Wait extends Work {
 2 
 3     public Wait(Taxi taxi) {
 4         super(taxi);
 5     }
 6 
 7     @Override
 8     public void driving() {
 9         super.driving();
10         System.out.println("第三步:正在等待中...");
11     }
12 }

 


具體裝飾(ByBus)角色

 1 public class ByBus extends Work {
 2 
 3     public ByBus(Taxi taxi) {
 4         super(taxi);
 5     }
 6 
 7     @Override
 8     public void driving() {
 9         super.driving();
10         System.out.println("第四步:坐上車了...");
11     }
12 }

 


具體裝飾(PayMoney)角色

 

 1 public class PayMoney extends Work {
 2 
 3     public PayMoney(Taxi taxi) {
 4         super(taxi);
 5     }
 6 
 7     @Override
 8     public void driving() {
 9         super.driving();
10         System.out.println("第五步:到家付錢了...");
11     }
12 }

 

 

工廠(TaxiFactory)

1 public class TaxiFactory {
2 
3     /**出租車工廠控制出租車工做順序*/
4     public static Taxi geTaxi(){
5         //執行順序 Car.driving() -> CallTaxi.driving()-> Wait.driving()-> 
      //ByBus.driving()-> PayMoney.driving()
6 return new PayMoney(new ByBus(new Wait(new CallTaxi(new Car())))); 7 } 8 9 }

 

測試類(TestMain)

1 public class TestMain {
2 
3 
4     public static void main(String[] args) {
5         Taxi taxi = TaxiFactory.geTaxi();
6         taxi.driving();
7     }
8 
9 }

 

 

測試類執行結果

1 第一步準備工做:點火、踩離合、掛擋、鬆手剎...
2 第二步:我要叫車了...
3 第三步:正在等待中...
4 第四步:坐上車了...
5 第五步:到家付錢了...
相關文章
相關標籤/搜索