Prototype) |
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
|
1. the created object is independent of what it is and how it is created. 2. specify the class to be instantiated at runtime; 3. we don't want to have a factory at the same level as the product. 4. the differences between instances of different classes are only the status of some combinations. It is more convenient to create instances through replication. 5. classes are not easy to create. For example, when an object in a combination contains an object. |
|
The copyWithZone method in the NSCopying proxy. |
Factory Method) |
Define an interface for creating an object, but letsubclasses decide which class to instantiate. Factory Method lets a class defer instantiation tosubclasses.
|
1. classes of the objects to be created are unpredictable during compilation; 2. what does a class want its subclass to create at runtime? 3. A class has some help classes as its sub-classes, and you want to return specific local knowledge. |
|
Factory methods can be seen almost everywhere in the Cocoa Touch framework. Such that some "convenience" methods that return aninstance of the class.
|
Abstract Factory) |
Provides an interface for creating families of related or dependent objectswithout specifying their concrete classes.
|
|
|
|
Builder (creator) |
Separates the construction of a complex object from its representationso that the same construction process can create different representations.
|
1. You want to create a complex object that contains several steps. The algorithm used to create it should be independent of how each part is created. 2. You need to create an object using different steps. |
|
|
Singleton (Singleton) |
Ensures a class has only one instance, and provide a global point ofaccess to it.
|
1. A class must have only one instance and can only be accessed through a well-known method. 2. This only instance can be expanded only through subclass, and expansion does not damage the client code. |
|
UIApplication, UIAccelerometer, andNSFileManager.
|
Adapter) |
Converts the interface of a class into another interface clients except CT. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
|
1. The existing class interface does not meet your requirements. 2. You want a reusable class and a class collaboration that may not have a proper interface. 3. You need to adapt to many subclasses of a class, but it is impossible for each of them to generate an adapter subclass. Therefore, you can use an Object Adapter (delegate) to adapt to the methods of their parent class. |
|
Delegate, block |
Bridge) |
Decouples an independent action from its implementation so that the two canvary independently.
|
1. You do not want to bind an abstraction with its implementation forever. 2. The abstraction and its implementation will be extended by the quilt class. 3. Changing an abstract implementation does not affect client code4. if an additional subclass is required to make an implementation better, the implementation and abstraction must be separated. 5. You want to share an implementation in the abstraction of different objects. |
|
|
Facade) |
Provides a uniied interface to a set of interfaces in a system. Fac extends adedefines a higher-level interface that makes the subsystem easier to use.
|
1. Your subsystem becomes very complex. To implement a function that requires a good class, you can use a appearance to provide a simple interface for operating these classes. 2. You can use the appearance to layer your subsystem. Each subsystem has only one exterior for access. You can make them communicate simply by appearance. |
|
|
Mediator) |
Defines an object that encapsulates how a set of objects interacts. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, andit lets you vary their interaction independently.
|
1. A group of objects are independent of each other and it is difficult to understand their relationships due to their complex interactions. 2. It is difficult to reuse an object because it points to and interacts with many other objects. 3. If a logic or behavior is distributed in many classes, configuration is not performed in subclass mode. |
|
|
Observer (Observer) |
Defines a one-to-define dependency between objects so that whenone object changes state, all its dependents are notified and updated automatically.
|
1. There are two types of abstraction and dependency. Encapsulating them in different objects allows you to change and reuse them independently. 2. Changing an object also requires changing other objects, and the number of changed objects is not fixed. 3. An object must notify other objects without knowing what other objects are. |
|
Notification, KVO |
Composite) |
Compose objects into tree structures to represent part-wholehierarchies. Composite lets clients treat individual objects and compositions of objectsuniformly.
|
1. You want to use the abstract tree to present some objects. 2. You want the client to treat a group of objects and independent objects in the same way. |
|
UIViews |
Iterator) |
Provide a way to access to the elements of an aggregate object sequentially withoutexposing its underlying representation.
|
1. You need to access the content of a composite object without exposing the internal performance of a composite object. 2. You need to traverse composite objects in different ways. 3. You need to provide a unified way to traverse different types of composite objects. |
|
NSEnumerator |
Visitor (Visitor) |
The Visitor pattern represents an operation to be performed med on the elements of an objectstructure. Visitor lets you define a new operation without changing the classes of the elements onwhich it operates.
|
1. A complex object structure contains many other objects with different interfaces. You want to perform different operations based on their actual types. 2. You want to perform unrelated operations on objects in a composite structure, and these operations will not pollute these classes. You can place related operations in an access class and use these operations as needed. 3. You often need to add new complex operations to a stable class. |
|
|
Decorator) |
Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
|
1. You want to add operations for some objects dynamically and visually without affecting other objects. 2. You want to add new behaviors to a class that cannot be expanded. When the definition of a class is hidden or the quilt category is not allowed. 3. It is optional to expand the behavior of a class. |
|
|
Chain of Responsibility (Responsibility Chain) |
To avoid coupling the sender of a request to its receiverby giving more than one object a chance to handle the request. It chains the processing ing objectsand passes the request along the chain until an object handles it.
|
1. More than one object may process a request, but the specific object is only known at runtime. 2. You want to initiate a request to a group of objects, but do not specify which one is used to process the request. |
|
|
Template Method) |
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps ofan algorithm without changing the algorithm's structure.
|
1. You need to implement the unchanged part of an algorithm at one time and leave the special behaviors in the subclass for implementation. 2. Common behavior of sub-classes can be put into a public class to avoid code redundancy. Differences in existing code need to be put into new operations. You use the template method to call each new operation instead of the new Code. 3. Subclass extensions must be controllable. You can define a "hook" operation at a certain point. Subclass can be expanded on the hook. |
|
DrawRect in UIView: Method RotatingHeaderView RotatingFooterViewwillAnimateRotationToInterfaceOrientation: duration: willRotateToInterfaceOrientation: duration:
|
Strategy) |
Define a family of algorithms, encapsulate each one, and makethem interchangeable. Strategy lets the algorithm vary independently from clients that use it.
|
1. A class uses different conditions to define different behaviors in its operations. You can add related condition branches to their own policy classes. 2. You need different variants in an algorithm. 3. You need to avoid exposing the complexity of algorithms and algorithm-related data in front of users. |
|
|
Command) |
Encapsulate a request as an object, thereby letting you parameterizeclients with different requests, queue or log requests, and support undoable operations.
|
1. You want your program to support undo/redo2. You Want To parameterize an action into an object and execute operations and replace callbacks using different commands. 3. You want to specify that the queue and the execution of a request are at different time points. 4. You want to record the changes so they can be restored when the system fails. 5. You want the system to support a business that encapsulates a lot of data changes. This service can be modularized into a command object. |
|
NSInvocation, NSUndoManager, and the target-action
|
Flyweight (share yuan) |
Uses sharing to support large numbers of fine-grained objectsefficiently.
|
1. Your application uses many objects. 2. loading objects in the memory will affect the memory performance. 3. the unique status of many objects can be externalized or lightweight. 4. Some Related shared objects can replace the original objects after the unique state of the objects is removed. 5. Your application does not rely on the Object ID because the shared object cannot have a unique identifier. |
|
|
Proxy) |
Provides a surrogate or placeholder for another object to control accessto it.
|
1. You need a distant proxy to provide a local phenotype to represent an object in different locations or an object on the Internet. 2. You need a virtual proxy to create heavyweight objects as required. 3. You need a protection proxy to control access requests to the original object based on different access permissions. 4. You need a smart reference proxy to count the references to access real objects. It can also be used to lock the original object so that other objects cannot modify it. |
|
NSProxy |
Memento (Memorandum) |
Without violating encapsulation, capture and externalize an object 'sinternal state so that the object can be restored to this state later.
|
1. You need to save an object in the state of snapshot or a part of it, and it can be restored in the future. 2. You need to hide this interface that exposes its implementation if it gets the status. |
|
The Cocoa Touch framework has adopted the Memento pattern with archiving, propertylist serialization, and core data.
|