The Decorator Design Pattern

装饰者模式。

1:什么时候用?

当你想通过继承父类来增加新功能时候,考虑用装饰者模式作为替代。在软件开发中,提倡少用继承,多用组合。

在swift中实现装饰者模式,通常用两种方式:extension 和 delegation

The Decorator pattern dynamically adds behaviors and responsibilities to an object without modifying its code. It’s an alternative to subclassing where you modify a class’s behavior by wrapping it with another object.


Swift extensions are slightly different from the classic definition of a decorator, because a extension doesn’t hold an instance of the class it extends.

你可能感兴趣的:(The Decorator Design Pattern)