Date post: | 01-Jul-2015 |
Category: |
Software |
Upload: | eliah-nikans |
View: | 363 times |
Download: | 0 times |
Design Patternsin Cocoa Touch
Eliah Snakin
Design Pattern
a general reusable solution to a commonly occurring problem within a given context
GoF
View ModelController
MVC
MVC Communication Diagram
Model View
ControllerNoti
fyUpdate
UpdateEvents
MVC as a Compound Design Pattern
Composite Command
MediatorStrategy Observer
MVC as a Compound Design Pattern
Model View
ControllerNoti
fyUpdate
UpdateEventsComposite
Command
Mediator
Observer
Strategy
Program to an interface, not an implementation
?
Delegation
Template Method
DecoratorAdapter
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
for a task.
Catalogue patterns in Cocoa Touch
Prototype Factory Method
Abstract Factory Builder Singleton
Adapter Bridge Façade Mediator Observer
Composite Iterator Visitor Decorator Chain of Responsibility
Template Method Strategy Command Flyweight Proxy
Prototype Creational
GoF:!Specify the kinds of objects to create using a prototypical instance and create new objects by coping the prototype.
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.
Cocoa Touch Example:!Prototype cells in UITableView
- tableView:cellForRowAtIndexPath: - dequeueReusableCellWithIdentifier:
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.
Class clusterClass clusters group a number of private concrete subclasses
under a public abstract superclass.
Apple Documentation
The abstract superclass handles instantiationNSNumber *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.
Class clusters with Multiple Public Superclasses
Apple Documentation
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:…
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];
Singleton+ (id)sharedManager { static MyManager *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager;}
http://www.galloway.me.uk/tutorials/singleton-classes/
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.
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).
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.
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 KVOA 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
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.
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
Block-Based Enumeration !Fast Enumeration !Internal Enumeration
NSEnumerator *itemEnumerator = [anArray objectEnumerator];NSString *item; while (item = [itemEnumerator nextObject]) { }
[anArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {}];
for (NSString * item in anArray) {}
- (void)makeObjectsPerformSelector:(SEL)aSelector
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.
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).
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.
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.
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
Hap
py C
odin
g :)