09@设计模式—(07)装饰模式

兄Die,写代码太累了?孤独寂寞冷?还没有女朋友吧?
关注微信公众号(瓠悠笑软件部落),送知识!送知识!送温暖!送工作!送女朋友!插科打诨哟!
huyouxiao.com
装饰模式(Decorator Pattern)在不修改原有对象内部数据结构的情况下往里面添加新的功能。它是结构型模式中的一种,实际上就是用已经存在的类来创建一个包装类。

这种设计模式创建一个装饰类,这个装饰类将包装原有的类,并为它提供附加的功能,同时原有类的方法的签名不变(就是方法名和方法参数都不变,同时返回数据类型也不变)。

具体实现
我们将创建一个Shape接口,然后创建一些实现类来实现这个Shape接口。接着我们创建一个抽象(abstract)类ShapeDecorator去实现Shape接口。同时抽象类中有一个类型为Shape的属性,这个属性作为这个接口的具体实现。新建RedShapeDecorator作为ShapeDecorator的子类。最后创建DecoratorPatternDemo类,我们将在这个类中使用RedShapeDecorator来装饰类型对象。
装饰模式类图

第一步:创建一个Shape接口

Shape.java

package com.pattern.decorator;

public interface Shape {
    void draw();
}

第二步:创建实现类(Rectangle和Circle)实现Shape接口

Rectangle.java

package com.pattern.decorator;

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

Circle.java

package com.pattern.decorator;

public class Circle implements Shape{

    public void draw() {
        System.out.println("Shape: Circle");
    }
    
}

第三步:创建抽象的装饰类实现Shape接口

ShapeDecorator.java

package com.pattern.decorator;

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

第四步:创建实现类(RedShapeDecorator)继承ShapeDecorator类

RedShapeDecorator.java

package com.pattern.decorator;

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");
    }
}

第五步:使用RedShapeDecorator来装饰Shape objects

DecoratorPatternDemo.java

package com.pattern.decorator;

public class DecoratorPatternDemo {
    public static void main(String args[]){
        Shape circle =new Circle();
        System.out.println("Circle with normal border");
        circle.draw();
        
        Shape redCircle =new RedShapeDecorator(new Circle());

        System.out.println("\nCircle of red border");
        redCircle.draw();
        
        Shape rectangle = new Rectangle();
        System.out.println("\nRectangle with normal border");
        rectangle.draw();
        
        Shape redRectangle =new RedShapeDecorator(new Rectangle());
       
        System.out.println("\nRectangle of red border");
        redRectangle.draw();
    }
}

第六步:验证输出

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle with normal border
Shape: Rectangle

Rectangle of red border
Shape: Rectangle
Border Color: Red
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值