设计模式(9)-装饰器模式

装饰器模式(Decorator Pattern)在不改变原有类的结构的情况下,对现有的对象添加新的功能。

一、需求举例:

几何中的图形(Shape)的子类有长方形(Rectangle),三角形(Triangle)和圆形(Circular);
颜色类:红色(Red),黄色(Yellow),蓝色(Blue)。
通过这些类,在不改变原有图形类的情况下,输出红色长方形,黄色三角形和蓝色圆形。

二、类图:

三、代码实例:

1.图形类

1.1 图形父类:IShape

package com.lance.decorator.shape;

public interface IShape {

    default void drawShape() {
        System.out.println("draw default shape.");
    }
}

1.2 圆形类:Circle

package com.lance.decorator.shape;

public class Circle implements IShape {
    @Override
    public void drawShape() {
        System.out.println("draw Circle.");
    }
}

1.3 三角形类:Triangle

package com.lance.decorator.shape;

public class Triangle implements IShape {
    @Override
    public void drawShape() {
        System.out.println("draw Triangle.");
    }
}

1.4 长方形类:

package com.lance.decorator.shape;

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

2.颜色装饰类:

2.1 颜色装饰父类:ColorDecorator

package com.lance.decorator.decorator;


import com.lance.decorator.shape.IShape;

public abstract class ColorDecorator implements IShape {

    protected IShape decoratedShape;

    public ColorDecorator(IShape decoratedShape) {
        this.decoratedShape = decoratedShape;
    }

    @Override
    public void drawShape() {
        decoratedShape.drawShape();
    }
}

2.2 红色装饰类:RedDecorator

package com.lance.decorator.decorator;

import com.lance.decorator.shape.IShape;

public class RedDecorator extends ColorDecorator {
    public RedDecorator(IShape decoratedShape) {
        super(decoratedShape);
    }

    @Override
    public void drawShape() {
        decoratedShape.drawShape();
        System.out.println("the color of shape is red.");
    }
}

2.3 黄色装饰类:YellowDecorator

package com.lance.decorator.decorator;

import com.lance.decorator.shape.IShape;

public class YellowDecorator extends ColorDecorator {

    public YellowDecorator(IShape decoratedShape) {
        super(decoratedShape);
    }

    @Override
    public void drawShape() {
        decoratedShape.drawShape();
        System.out.println("the color of shape is yellow.");
    }
}

2.4 蓝色装饰类:BlueDecorator

package com.lance.decorator.decorator;

import com.lance.decorator.shape.IShape;

public class BlueDecorator extends ColorDecorator {

    public BlueDecorator(IShape decoratedShape) {
        super(decoratedShape);
    }

    @Override
    public void drawShape() {
        decoratedShape.drawShape();
        System.out.println("the color of shape is blue.");
    }
}

3.主类:DecoratorPattern

package com.lance.decorator;

import com.lance.decorator.decorator.BlueDecorator;
import com.lance.decorator.decorator.RedDecorator;
import com.lance.decorator.decorator.YellowDecorator;
import com.lance.decorator.shape.Circle;
import com.lance.decorator.shape.IShape;
import com.lance.decorator.shape.Rectangle;
import com.lance.decorator.shape.Triangle;

public class DecoratorPattern {
    public static void main(String[] args) {


        System.out.println("==========start==========");

        IShape redRectangle = new RedDecorator(new Rectangle());
        redRectangle.drawShape();


        System.out.println("=======================");

        IShape yellowTriangle = new YellowDecorator(new Triangle());
        yellowTriangle.drawShape();

        System.out.println("=======================");

        IShape blueCircle = new BlueDecorator(new Circle());
        blueCircle.drawShape();

        System.out.println("==========end==========");


    }
}

输出结果:

==========start==========
draw Rectangle.
the color of shape is red.
=======================
draw Triangle.
the color of shape is yellow.
=======================
draw Circle.
the color of shape is blue.
==========end==========
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容