HeadFirst设计模式

👉Observer

当一个事物发生变化时,另一个可以获得通知,做 iOS 的都知道,KVO 就是观察者模式的体现。但是别忘了在 dealloc 中移除。

👉Decorator

以我的理解就是包装,类似于俄罗斯套娃,包装原有的类,来提供更多的功能,避免继承造成类膨胀的问题。通常是用来扩展一个已有类的功能。

Decorator is used to add new features of an existing object to create a new object. There is no restriction of freezing the object until all its features are added.

👉Factory

定义了一个创建对象的接口,由子类决定要初始化的是那个类。把类的实例化推迟到子类中。

👉Builder

One should use builder if he wants to limit the object creation with certain properties/features. For example there are 4-5 attributes which are mandatory to be set before the object is created or we want to freeze object creation until certain attributes are not set yet. Basically, use it instead of constructor

Decorator VS Builder

Patterns like builder and factory(and abstract factory) are used in creation of objects. And the patterns like decorator (also called as structural design patterns) are used for extensibility or to provide structural changes to already created objects.