8. Delegation
A mechanism by which a host object embeds a weak
reference to another object—its delegate—and periodically
sends messages to the delegate when it requires its input
Template
Method
Decorator
Adapter
for a task.
10. Prototype Creational
GoF:!
Specify the kinds of objects to create using a prototypical instance and
create new objects by coping the prototype.
Cocoa Touch Example:!
Prototype cells in UITableView
Use dynamic prototypes to design one cell and then use it as the
template for other cells in the table. Use a dynamic prototype
when multiple cells in a table should use the same layout to
display information.
- tableView:cellForRowAtIndexPath:
- dequeueReusableCellWithIdentifier:
11. Abstract Factory Creational
GoF:!
Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.
Cocoa Touch Example:!
The interface declared by the abstract superclass, NSNumber
(Class cluster)
Class clusters group a number of private concrete subclasses
under a public abstract superclass.
12. Class cluster
Class clusters group a number of private concrete subclasses
under a public abstract superclass.
The abstract superclass handles instantiation
NSNumber *aChar = [NSNumber numberWithChar:’a’];
NSNumber *anInt = [NSNumber numberWithInt:1];
Each object returned by the factory methods belong to a
different private subclass which is hidden to users.
Apple Documentation
14. Factory Method Creational
GoF:!
Define an interface for creating an object, but let subclasses decide which
class to instantiate.
Cocoa Touch Example:!
Convenience class-methods, returning instances.
NSNumber
+ numberWithBool:
+ numberWithChar:
…
15. Singleton Creational
GoF:!
Ensure a class only has one instance, and provide a global point of
access to it.
Cocoa Touch Example:!
UIApplication, UIAccelerometer …
UIApplication *applicationSingleton = [UIApplication
sharedApplication];
!
UIAccelerometer *accelerometerSingleton =
[UIAccelerometer sharedAccelerometer];
17. Adapter Interface adaptation
GoF:!
Converts the interface of a class into another interface clients
expect. Adapter lets classes work together that couldn’t otherwise
because of incompatible interfaces.
Cocoa Touch Example:!
Delegation — mix of Adapter, Decorator, Template Method
Uses Protocol or Block
You should always use a class’s delegation mechanism instead
of subclassing the class, unless the delegation methods do not
allow you to accomplish your goal.
18. Façade Interface adaptation
GoF:!
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.
Cocoa Touch Example:!
NSImage provides a unified interface for loading and using images that
can be bitmap-based (such as those in JPEG, PNG, or TIFF format) or
vector-based (such as those in EPS or PDF format).
19. Mediator Decoupling
GoF:!
Defines an object that encapsulates how a set of objects interacts.
Mediator promotes loose coupling by keeping objects from referring
to each other explicitly, and it lets you vary their interaction
independently.
Cocoa Touch Example:!
UIViewController
AppKit implements the NSController class and its
subclasses. These classes and the bindings
technology are not available in iOS.
20. Observer Decoupling
GoF:!
Defines a one-to-many dependency between objects so that when
one object changes state, all its dependents are notified and
updated automatically.
Cocoa Touch Example:!
NSNotification, Key-Value Observing
Notifications KVO
A central object that provides
change notifications for all
observers
The observed object directly
transmits notifications to observers.
Mainly concerned with program
events in a broad sense
Tied to the values of specific object
properties
21. Composite Abstract collection
GoF:!
Compose objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects
uniformly.
Cocoa Touch Example:!
View Hierarchy — structural architecture that plays a part in both
drawing and event handling.
22. Iterator Abstract collection
GoF:!
Provide a way to access to the elements of an aggregate object sequentially
without exposing its underlying representation.
Cocoa Touch Example:!
NSEnumerator
NSEnumerator *itemEnumerator = [anArray objectEnumerator];
NSString *item;
while (item = [itemEnumerator nextObject]) { }
Block-Based Enumeration
!
Fast Enumeration
!
Internal Enumeration
[anArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {}];
for (NSString * item in anArray) {}
- (void)makeObjectsPerformSelector:(SEL)aSelector
23. Decorator Behavioural
GoF:!
Attaches additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to subclassing for extending functionality.
Cocoa Touch Example:!
NSAttributedString, NSScrollView, and UIDatePicker
Implemented with True Subclasses or Categories.
24. Chain of Responsibility Behavioural
GoF:!
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.
Cocoa Touch Example:!
UIResponder
If a view is managed by a UIViewController object, the
view controller becomes the next responder in the chain
(and from there the event or action message passes to
the view’s superview).
25. Template Method Algorithm Encapsulation
GoF:!
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.
The Template Method pattern is a fundamental design of Cocoa, and
indeed of object-oriented frameworks in general.
The pattern in Cocoa lets custom components of a
program hook themselves into an algorithm, but the
framework components determine when and how they
are needed.
26. Strategy Algorithm Encapsulation
GoF:!
Define a family of algorithms, encapsulate each one, and make
them interchangeable. Strategy lets the algorithm vary
independently from clients that use it.
Cocoa Touch Example:!
a controller determines the behavior of a view about what and when to
display the data from a model. The view itself knows how to draw
something but doesn’t know what until the controller tells it what to
display.
27. Command Algorithm Encapsulation
GoF:!
Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or log requests,
and support undoable operations.
Cocoa Touch Example:!
NSInvocation, somewhat a target-action mechanism of Cocoa