three main milestones in our design process:
Conceptualizing the ideas
Designing the look-and-feel
Architectural design
Tips:
Subclassing is
white-box reuse
Object composition is
black-box reuse
准则:
Program to an interface, not an implementation
模块间以protocal或基本抽象类通信..以达到最大化复用
1.
Prototype Pattern: 带clone方法的对象 …可以不同的class..以protocol统一clone接口
2.
Factory method Pattern: 返回抽象类型 id<…> 或含默认实现的abstract class类型..
用一个Abstract Factory Class ..让后来派生的factory 去重载factory method
3. Abstract
Factory Pattern:
Factory Method pattern that is used for generating concrete subclasses by an abstract class
如 NSNumber 的 numberWith* 方法…是factory method..并且返回的是NSNumber的各种subType
Class Clusters are a form of Abstract Factory
There is a distinction between factory methods for creating abstract products and factory methods for creating abstract factories. Obviously, factory methods like
intValue
and
boolValue
should be overridden in concrete factories (NSCFNumber and
NSCFBoolean)
to return actual values (products). Other factory methods like
numberWithBool:
and
numberWithInt:
are not for returning products but for returning actual factories that return products. They are not intended to be overridden in concrete factory subclasses.
4. Builder Pattern: Separates the construction of a complex object from its representation so that the same construction process can create different representations
client- >director->builder -> product
5. Singleton Pattern:
Ensures a class has only one instance, and provide a global point of access to it
we can use some Foundation functions to instantiate whatever object based on its class type. One of them is id NSAllocateObject (Class aClass, NSUInteger extraBytes, NSZone *zone). So if we want to instantiate an object of a class called “Singleton,” we can do the following:
Singleton *singleton = [NSAllocateObject ([Singleton class], 0, NULL) init];
Interface Adaptation
6. Adapter Pattern: 没有某operation, 直接叫某个
特定的
obj间接提供
class adapter:
object adapter:
Since it’s a “has a” relationship between
Adapter
and
Adaptee, Adapter
can adapt subclasses of
Adaptee
without much problem.
the delegate object is a adapter object
7. Bridge
Pattern: 没有某operation, 叫某种<xx>对象间接提供
8. Facade
Pattern:
Provides a unified interface to a set of interfaces in a system. Façade defines a higher-level interface that makes the subsystem easier to use
Decoupling of Objects
9. Mediator
Pattern:
Mediator pattern is very useful for situations where the application’s behavior is distributed among different objects and they depend on each other
controller
10. Observer
Pattern:
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically
KVO的方式:
Notification 是center 转发式
Abstract Collection
11. Composite
Pattern:
将相同type的对象组合成树形结构以表示“部分-整体”的层次结构。它使得客户对单个对象和复合对象的使用具有一致性。
New operations applied on the structure can be used with the Visitor pattern (Chapter 17) to have visitors “visit” each node for further processing without modifying the existing composite structure.
12. Iterator
Pattern:
Provide a way to access to the elements of an aggregate object sequentially without exposing its underlying representation
Behavioral Extension
13. Visitor
Pattern:
The Visitor pattern represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates
define any external behavior that can be applied to a composite structure as a “visitor.” Visitors “visit” each node in a complex structure to perform particular operations based on the actual type of a visited node. vistitor对象负责visit compostite object中的各元素
element对象在定义时, 需要预先定义visitor接口, 并含默认实现( 遍历结点 )
与
Iterator
Pattern的遍历不同的是:
Visitor
遍历轨迹跟内部结构及结点具体类型有关(不同的实例结点调用不到的visit方法)….
Iterator
只是按抽象基类简单地遍历
14. Decorator
Pattern:
Decorator 和 component 的接口是一致的
proxy跟decorator的区别就是..前者是为了限制等..后者为了扩展?
true subclass approach: 结构同上图, 可以成链
category:
15. Chain of responsibility
Pattern:
To avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. It chains the receiving objects and passes the request along the chain until an object handles it
Algorithm Encapsulation
16. Template Method
Pattern:
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure
17. Strategy
Pattern:
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
只封装action
18. Command pattern: 封装的是request..即 [target action]
target …action均可配置
统一命令接口, 不同的命令派生不同的类, 每一份实例接受变化的入参而重复使用之
Executed command objects can be collected and queued as a list of command history
undo…redo
NSundoManager
doesn’t retain its invocation target receivers,
Performance and Object Access
19. Flyweight pattern:
Uses sharing to support large numbers of fine-grained objects efficiently
抽出决定 unique 的 extrinsic 元素..剩余的 intrinsic 共享之
UITableViewCell 就是如此复用的
20.
Proxy pattern
:
Provides a surrogate or placeholder for another object to control access to it.
In general, a proxy acts as a surrogate or placeholder that controls access to another object, objects that are remote, expensive to create, or in need of securing.
virtual proxy: 提供与被代理对象同样的接口, 使得自身表现得跟被代理对象一样. 同时又加入一些扩展方法…通过用继承的方式实现..
如JCAsynImageView 就是UIIamgeView 的 proxy
proxy跟decorator的区别就是..前者是为了限制等..后者为了扩展?
Objective-C doesn’t support multiple inheritances. So if your proxy objects are not subclasses of anything specific in the Cocoa Touch framework, you may consider using NSProxy for your placeholders or surrogate objects.
State of Object
21. Memento
pattern
:
Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later
取原来复杂类的一个简单必要子集作为一个memento class.