Design Patterns in Cocoa Touch
-
Upload
eliah-nikans -
Category
Software
-
view
365 -
download
0
description
Transcript of Design Patterns in Cocoa Touch
![Page 1: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/1.jpg)
Design Patternsin Cocoa Touch
Eliah Snakin
![Page 2: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/2.jpg)
Design Pattern
a general reusable solution to a commonly occurring problem within a given context
GoF
![Page 3: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/3.jpg)
View ModelController
MVC
![Page 4: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/4.jpg)
MVC Communication Diagram
Model View
ControllerNoti
fyUpdate
UpdateEvents
![Page 5: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/5.jpg)
MVC as a Compound Design Pattern
Composite Command
MediatorStrategy Observer
![Page 6: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/6.jpg)
MVC as a Compound Design Pattern
Model View
ControllerNoti
fyUpdate
UpdateEventsComposite
Command
Mediator
Observer
Strategy
![Page 7: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/7.jpg)
Program to an interface, not an implementation
?
![Page 8: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/8.jpg)
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.
![Page 9: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/9.jpg)
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
![Page 10: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/10.jpg)
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:
![Page 11: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/11.jpg)
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.
![Page 12: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/12.jpg)
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.
![Page 13: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/13.jpg)
Class clusters with Multiple Public Superclasses
Apple Documentation
![Page 14: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/14.jpg)
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:…
![Page 15: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/15.jpg)
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];
![Page 16: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/16.jpg)
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/
![Page 17: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/17.jpg)
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.
![Page 18: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/18.jpg)
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).
![Page 19: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/19.jpg)
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.
![Page 20: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/20.jpg)
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
![Page 21: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/21.jpg)
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.
![Page 22: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/22.jpg)
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
![Page 23: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/23.jpg)
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.
![Page 24: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/24.jpg)
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).
![Page 25: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/25.jpg)
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.
![Page 26: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/26.jpg)
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.
![Page 27: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/27.jpg)
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
![Page 28: Design Patterns in Cocoa Touch](https://reader033.fdocuments.us/reader033/viewer/2022060121/5593f8b51a28abb8088b46ba/html5/thumbnails/28.jpg)
Hap
py C
odin
g :)