07装饰模式Decorator

一、什么是装饰模式

  装饰( Decorator )模式又叫做包装模式。通 过一种对客户端透明的方式来扩展对象的功能, 是继承关系的一个替换方案。

二、装饰模式的结构

三、装饰模式的角色和职责

  抽象组件角色: 一个抽象接口,是被装饰类和 装饰类的父接口。

  具体组件角色:为抽象组件的实现类。

  抽象装饰角色:包含一个组件的引用,并定义了 与抽象组件一致的接口。

  具体装饰角色:为抽象装饰角色的实现类。负责 具体的装饰。

没装饰前

车接口

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 }
原文地址:https://www.cnblogs.com/justdoitba/p/9032154.html