装饰者模式

装饰者设计模式

动态的将新功能附加到对象上,在对象功能拓展方面,它比集成更有弹性,装饰者模式也体现了开闭原则。
1.动态将新功能附加到对象
2.ocp原则

装饰者的图解

抽象类:

public abstract class Drink {

    /**
     * 描述
     */
    public String des;
    /**
     * 价格
     */
    private float price = 0.0f;

    /**
     * 价格
     */
    public abstract float cost();

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
}

咖啡类:

public class Coffee extends Drink {
    /**
     * 价格
     *
     * @return
     */
    @Override
    public float cost() {
        return super.getPrice();
    }
}

咖啡的实现类:

public class Express extends Coffee{

    public Express(){
        setDes("意大利咖啡");
        setPrice(6.0f);
    }
}

public class LongBlack extends Coffee{

    public LongBlack(){
        setDes("美式咖啡");
        setPrice(16.0f);
    }
}

public class ShortBlack extends Coffee{

    public ShortBlack() {
        setDes("shortBlack");
        setPrice(8.0f);
    }
}

修饰者抽象类:

public class Decorator extends Drink {

    private Drink obj;

    public Decorator(Drink obj) {
        this.obj = obj;
    }

    @Override
    public float cost() {
        return super.getPrice()+obj.getPrice();
    }

    @Override
    public String getDes(){
        return super.des+obj.getDes();
    }
}

修饰者类:

public class Chocolate extends Decorator{

    public Chocolate(Drink obj) {
        super(obj);
        setDes("巧克力");
        setPrice(3.0f);
    }
}


public class Milk extends Decorator{

    public Milk(Drink obj) {
        super(obj);
        setDes("牛奶");
        setPrice(2.0f);
    }
}

public class Soy extends Decorator {
    public Soy(Drink obj) {
        super(obj);
        setDes("豆浆");
        setPrice(1.5f);
    }
}

测试类:

public class CoffeeBar {
    public static void main(String[] args) {
        Drink longBlack = new LongBlack();

        System.out.println(longBlack.cost());

        Milk milk = new Milk(longBlack);
        System.out.println(milk.cost());

        Chocolate chocolate = new Chocolate(milk);
        System.out.println(chocolate.cost());
    }
}

案例2:
1.我们将创建一个 Shape 接口和实现了 Shape 接口的实体类
2.创建一个实现了 Shape 接口的抽象装饰类 ShapeDecorator,并把 Shape 对象作为它的实例变量。
3。RedShapeDecorator 是实现了 ShapeDecorator 的实体类。
4.DecoratorPatternDemo 类使用 RedShapeDecorator 来装饰 Shape 对象(测试方法)。

步骤 1
创建一个接口:

Shape.java
public interface Shape {
   void draw();
}

步骤 2
创建实现接口的实体类。

Rectangle.java
public class Rectangle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Shape: Rectangle");
   }
}
Circle.java
public class Circle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Shape: Circle");
   }
}

步骤 3
创建实现了 Shape 接口的抽象装饰类。
ShapeDecorator.java


public abstract class ShapeDecorator implements Shape {
   protected Shape decoratedShape;
 
   public ShapeDecorator(Shape decoratedShape){
      this.decoratedShape = decoratedShape;
   }
 
   public void draw(){
      decoratedShape.draw();
   }  
}

步骤 4
创建扩展了 ShapeDecorator 类的实体装饰类。
RedShapeDecorator.java

public class RedShapeDecorator extends ShapeDecorator {
 
   public RedShapeDecorator(Shape decoratedShape) {
      super(decoratedShape);     
   }
 
   @Override
   public void draw() {
      decoratedShape.draw();         
      setRedBorder(decoratedShape);
   }
 
   private void setRedBorder(Shape decoratedShape){
      System.out.println("Border Color: Red");
   }
}

步骤 5
使用 RedShapeDecorator 来装饰 Shape 对象。
DecoratorPatternDemo.java

public class DecoratorPatternDemo {
   public static void main(String[] args) {
 
      Shape circle = new Circle();
      ShapeDecorator redCircle = new RedShapeDecorator(new Circle());
      ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());
      //Shape redCircle = new RedShapeDecorator(new Circle());
      //Shape redRectangle = new RedShapeDecorator(new Rectangle());
      System.out.println("Circle with normal border");
      circle.draw();
 
      System.out.println("\nCircle of red border");
      redCircle.draw();
 
      System.out.println("\nRectangle of red border");
      redRectangle.draw();
   }
}

步骤 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
posted @ 2021-11-04 01:32  King-DA  阅读(11)  评论(0)    收藏  举报