标签:closed .com draw div click onclick str strong ace
引言:
一、概述
二、使用步骤
实例:将把一个形状装饰上不同的颜色,同时又不改变形状类。
1. 创建一个接口: 图形
package cn.model; /** * 图形接口 */ public interface Shape { /** * 画图 */ void draw(); }
2. 创建这个接口的实现类:矩形/圆形
package cn.model; /** * 矩形 * @Description: * @Auther:zls * @Date:2019/5/6 16:51 */ public class Rectangle implements Shape { @Override public void draw() { System.out.println("画矩形"); } }
package cn.model; /** * 圆形 * @Description: * @Auther:zls * @Date:2019/5/6 16:52 */ public class Circle implements Shape { @Override public void draw() { System.out.println("画圆形"); } }
3. 创建装饰类
ShapeDecorator:创建实现了 Shape 接口的抽象装饰类(这里是为了方便扩展)
package cn.model; /** * 实现了 Shape 接口的抽象装饰类 * @Description: * @Auther:zls * @Date:2019/5/6 16:53 */ public abstract class ShapeDecorator implements Shape { protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape){ this.decoratedShape = decoratedShape; } @Override public void draw(){ decoratedShape.draw(); } }
RedShapeDecorator:创建扩展了 ShapeDecorator 类的实体装饰类
package cn.model; /** * 扩展了 ShapeDecorator 类的实体装饰类。 * @Description: * @Auther:zls * @Date:2019/5/6 16:55 */ 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("设置为红色"); } }
GreenShapeDecorator:装饰器类(这个类是我将ShapeDecorator,RedShapeDecorator两个合并了,方便初学者学习)
package cn.model; /** * @Description: * @Auther:zls * @Date:2019/5/6 17:07 */ public class GreenShapeDecorator implements Shape { protected Shape decoratedShape; private void setGreenBorder(Shape decoratedShape){ System.out.println("设置为绿色"); } @Override public void draw() { decoratedShape.draw(); setGreenBorder(decoratedShape); } public GreenShapeDecorator(Shape decoratedShape) { this.decoratedShape = decoratedShape; } }
4. 测试代码: 使用 RedShapeDecorator 来装饰 Shape 对象
package cn.model; /** * 使用 RedShapeDecorator 来装饰 Shape 对象 * @Description: * @Auther:zls * @Date:2019/5/6 16:57 */ public class DecoratorPatternDemo { public static void main(String[] args) { Shape circle = new Circle(); Shape redCircle = new RedShapeDecorator(new Circle()); Shape redRectangle = new RedShapeDecorator(new Rectangle()); System.out.println("---- 圆形 -----"); circle.draw(); System.out.println("\n----- 红色的圆形 ------"); redCircle.draw(); System.out.println("\n----- 红色的矩形 ------"); redRectangle.draw(); System.out.println("-------------"); GreenShapeDecorator greenCircle = new GreenShapeDecorator(new Circle()); greenCircle.draw(); GreenShapeDecorator greenRectangle = new GreenShapeDecorator(new Rectangle()); greenRectangle.draw(); } }
控制台结果打印:
---- 圆形 ----- 画圆形 ----- 红色的圆形 ------ 画圆形 设置为红色 ----- 红色的矩形 ------ 画矩形 设置为红色 ------------- 画圆形 设置为绿色 画矩形 设置为绿色
参考网址:
https://www.runoob.com/design-pattern/decorator-pattern.html
标签:closed .com draw div click onclick str strong ace
原文地址:https://www.cnblogs.com/shiyun32/p/10821248.html