07裝飾模式Decorator

1、什麼是裝飾模式測試

  裝飾( Decorator )模式又叫作包裝模式。通 過一種對客戶端透明的方式來擴展對象的功能, 是繼承關係的一個替換方案。this

 

2、裝飾模式的結構spa

 

3、裝飾模式的角色和職責code

  抽象組件角色: 一個抽象接口,是被裝飾類和 裝飾類的父接口。對象

  具體組件角色:爲抽象組件的實現類。blog

  抽象裝飾角色:包含一個組件的引用,並定義了 與抽象組件一致的接口。繼承

  具體裝飾角色:爲抽象裝飾角色的實現類。負責 具體的裝飾。接口

 

沒裝飾前get

車接口class

1 //車接口
2 public interface Car {
3     
4     public void show();
5     
6     public void run();
7 }
能夠跑的車
 1 //能夠跑的車
 2 public class RunCar implements Car {
 3 
 4     public void run() {
 5         System.out.println("能夠跑");
 6     }
 7 
 8     public void show() {
 9         this.run();
10     }
11 }

會飛的車

 1 //會飛的車
 2 public class FlyCar implements Car {
 3 
 4     public void show() {
 5         this.run();
 6         this.fly();
 7     }
 8 
 9     public void run() {
10         System.out.println("能夠跑");
11     }
12     
13     public void fly() {
14         System.out.println("能夠飛");
15     }
16 }

游泳的車

 1 //游泳的車
 2 public class SwimCar implements Car{
 3 
 4     public void run() {
 5         System.out.println("能夠跑");
 6     }
 7 
 8     public void Swim() {
 9         System.out.println("能夠遊");
10     }
11     
12     public void show() {
13         this.run();
14         this.Swim();
15     }
16 }

測試

1 public class MainClass {
2     public static void main(String[] args) {
3         Car flycar = new SwimCar();
4         flycar.show();
5     }
6 }

===========================================================================

車接口

1 public interface Car {
2     
3     public void show();
4     
5     public void run();
6 }

能夠跑的車

 1 //能夠跑的車
 2 public class RunCar implements Car {
 3 
 4     public void run() {
 5         System.out.println("能夠跑");
 6     }
 7 
 8     public void show() {
 9         this.run();
10     }
11 }

車裝飾

 1 //汽車裝飾
 2 public abstract class CarDecorator implements Car{
 3     private Car car;
 4     
 5     public Car getCar() {
 6         return car;
 7     }
 8 
 9     public void setCar(Car car) {
10         this.car = car;
11     }
12 
13     public CarDecorator(Car car) {
14         this.car = car;
15     }
16     
17     public abstract void show();
18 }

游泳車裝飾

 1 //游泳車裝飾
 2 public class SwimCarDecorator extends CarDecorator {
 3 
 4     public SwimCarDecorator(Car car) {
 5         super(car);
 6     }
 7 
 8     public void show() {
 9         this.getCar().show();
10         this.swim();
11     }
12     
13     public void swim() {
14         System.out.println("能夠遊");
15     }
16 
17     public void run() {
18         
19     }
20 }

飛車裝飾

 1 //飛車裝飾
 2 public class FlyCarDecorator extends CarDecorator{
 3 
 4     public FlyCarDecorator(Car car) {
 5         super(car);
 6     }
 7 
 8     public void show() {
 9         this.getCar().show();
10         this.fly();
11     }
12     
13     public void fly() {
14         System.out.println("能夠飛");
15     }
16 
17     public void run() {
18         
19     }
20 }

測試

 1 public class MainClass {
 2     public static void main(String[] args) {
 3         Car car = new RunCar();
 4         
 5         car.show();
 6         System.out.println("---------");
 7         
 8         Car swimcar = new SwimCarDecorator(car);
 9         swimcar.show();
10         System.out.println("---------");
11         
12         Car flySwimCar = new FlyCarDecorator(swimcar);
13         flySwimCar.show();
14     }
15 }
相關文章
相關標籤/搜索