美文网首页
The Decorator Design Pattern

The Decorator Design Pattern

作者: mydisc1 | 来源:发表于2018-01-03 14:04 被阅读0次

装饰者模式。

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

      本文链接:https://www.haomeiwen.com/subject/wckhnxtx.html