装饰者模式

2019-02-21  本文已影响0人  hai_world

步骤 1

创建一个接口:

 public interface Shape {
       void draw();
 }

步骤 2

创建实现接口的实体类。

步骤 3

创建实现了 Shape 接口的抽象装饰类。

步骤 4

创建扩展了 ShapeDecorator 类的实体装饰类。

步骤 5

使用 RedShapeDecorator 来装饰 Shape 对象。

步骤 6

执行程序,输出结果:

    Circle with normal border
    Shape: Circle
   ------------------------------------------
    Circle of red border
    Shape: Circle
    Border Color: Red
   ------------------------------------------
    Rectangle of red border    
    Shape: Rectangle
     Border Color: Red
上一篇 下一篇

猜你喜欢

热点阅读