装饰模式

2017-06-24  本文已影响0人  上海马超23
component = new ConcreteComponent();
component = new ConcreteDecorator1(component);
component = new ConcreteDecorator2(component); // 现在这个原始组件已经被装饰了2次了
component.operator();

总结

优点

  1. Component 和 Decorator 互相不用知道具体的组件,只管调用统一抽象父类的operator方法。
  2. 继承是静态地扩展类的功能,装饰是动态增加;装饰避免了子类的膨胀。
  3. 装饰可以对中间子类做扩展,避免对该子类后续的子孙类改动影响

缺点

避免过多层的装饰,降低系统复杂度

上一篇 下一篇

猜你喜欢

热点阅读