装饰器模式(Decorator Pattern)是一种结构型设计模式。将对象放入到一个特殊封装的对象中,为这个对象绑定新的行为,具备新的能力,同时又不改变其原有结构。
如果你希望在无需修改代码的情况下即可使用对象,且希望在运行时为对象新增额外的行为,可以使用装饰模式。或者你用继承来扩展对象行为的方案难以实现或者根本不可行,你可以使用该模式。
// Shape.java 基础形状接口 public interface Shape { void draw(); }
// Circle.java 具体形状实现了基础形状接口 public class Circle implements Shape { @Override public void draw() { System.out.println("Circle::draw()"); } } // Square.java 具体形状实现了基础形状接口 public class Square implements Shape { @Override public void draw() { System.out.println("Square::draw()"); } }
// ShapeDecorator.java 抽象装饰类,是否实现Shape可选 public abstract class ShapeDecorator implements Shape { // public abstract class ShapeDecorator { protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape) { this.decoratedShape = decoratedShape; } public void draw() { decoratedShape.draw(); } }
// RedShapeDecorator.java 具体装饰器1 public class RedShapeDecorator extends ShapeDecorator { public RedShapeDecorator(Shape decoratedShape) { super(decoratedShape); } @Override public void draw() { decoratedShape.draw(); setRedColor(decoratedShape); } private void setRedColor(Shape decoratedShape) { System.out.println( "RedShapeDecorator::setRedColor() " + decoratedShape.getClass().getName() ); } } // ShadowShapeDecorator.java 具体装饰器2 public class ShadowShapeDecorator extends ShapeDecorator { public ShadowShapeDecorator(Shape decoratedShape) { super(decoratedShape); } @Override public void draw() { // decoratedShape.draw(); setShadow(decoratedShape); } private void setShadow(Shape decoratedShape) { System.out.println( "ShadowShapeDecorator::setShadow() " + decoratedShape.getClass().getName() ); } }
/** * 装饰器模式是将一个对象放到一个装饰器对象中,执行装饰器类里的方法时,对象的行为能力得到增强。 * 先声明具体对象,然后放到装饰器,得到一个带有装饰器的新对象,该对象具备了新的能力。 */ // 声明形状 Shape circle = new Circle(); Shape square = new Square(); // 增加红色装饰 ShapeDecorator redCircle = new RedShapeDecorator(circle); ShapeDecorator redSquare = new RedShapeDecorator(square); circle.draw(); redCircle.draw(); redSquare.draw(); // 增加影子装饰 ShadowShapeDecorator shadowCircle = new ShadowShapeDecorator(circle); ShadowShapeDecorator shadowSquare = new ShadowShapeDecorator(square); shadowCircle.draw(); shadowSquare.draw();
// Shape.go 基础形状接口 type Shape interface { Draw() GetName() string }
// Circle.go 具体形状实现了基础形状接口 type Circle struct { } func (c *Circle) Draw() { fmt.Println("Circle::Draw()") } func (c *Circle) GetName() string { return "Circle" } // Square.go 具体形状实现了基础形状接口 type Square struct { } func (c *Square) Draw() { fmt.Println("Square::Draw()") } func (c *Square) GetName() string { return "Square" }
// ShapeDecorator.go 抽象装饰类,是否实现Shape可选 type ShapeDecorator interface { Draw() }
// RedShapeDecorator.go 具体装饰器1 type RedShapeDecorator struct { DecoratedShape Shape } func (r *RedShapeDecorator) Draw() { r.DecoratedShape.Draw() r.SetRedColor(r.DecoratedShape) } func (r *RedShapeDecorator) SetRedColor(decoratedShape Shape) { fmt.Println("RedShapeDecorator::setRedColor() " + decoratedShape.GetName()) } // ShadowShapeDecorator.go 具体装饰器2 type ShadowShapeDecorator struct { DecoratedShape Shape } func (s *ShadowShapeDecorator) Draw() { // 装饰器根据需要是否调用形状的Draw方法 // s.DecoratedShape.Draw() s.SetShadow(s.DecoratedShape) } func (s *ShadowShapeDecorator) SetShadow(decoratedShape Shape) { fmt.Println("ShadowShapeDecorator::SetShadow() " + decoratedShape.GetName()) }
/** * 装饰器模式是将一个对象放到一个装饰器对象中,执行装饰器类里的方法时,对象的行为能力得到增强。 * 先声明具体对象,然后放到装饰器,得到一个带有装饰器的新对象,该对象具备了新的能力。 */ // 声明形状 var circle = new(src.Circle) var square = new(src.Square) // 增加红色装饰 var redCircle = &src.RedShapeDecorator{ DecoratedShape: circle, } var redSquare = &src.RedShapeDecorator{ DecoratedShape: square, } circle.Draw() redCircle.Draw() redSquare.Draw() // 增加影子装饰 var shadowCircle = &src.ShadowShapeDecorator{ DecoratedShape: circle, } var shadowSquare = &src.ShadowShapeDecorator{ DecoratedShape: square, } shadowCircle.Draw() shadowSquare.Draw()
不同语言实现设计模式:https://github.com/microwind/design-pattern