pavel_kravchenko_mobile development

31
Cocoa Design Patterns Pavel Kravchenko, Ciklum

Upload: ciklum

Post on 28-Jun-2015

1.308 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pavel_Kravchenko_Mobile Development

Cocoa Design Patterns

Pavel Kravchenko, Ciklum

Page 2: Pavel_Kravchenko_Mobile Development

What is a Design Pattern?

A design pattern is: a template for a design

that solves a general problem

a tool of abstraction that is useful in architecture

A vocabulary to use when explaining complex software

Page 3: Pavel_Kravchenko_Mobile Development

Design principles

encapsulate the aspects of system structure

program to an interface, not an implementation

Minimize coupling

Page 4: Pavel_Kravchenko_Mobile Development

The Command

encapsulate a request as an object and

Separates message sender and reciever

Page 5: Pavel_Kravchenko_Mobile Development

The Command

Page 6: Pavel_Kravchenko_Mobile Development

The Command

Use when you want to:

parameterize objects by an action to perform (Commands are an object-oriented replacement for callbacks).

specify and execute requests at different times (you can transfer a command object to a different process).

support undo.

support logging changes.

Page 7: Pavel_Kravchenko_Mobile Development

The Command

Example: NSInvocation (encapsulates an Objective-C message)

An invocation object contains a target object, method selector, and method arguments

You can dynamically change its target and arguments

you can also obtain the return value from the object

you can repeatedly invoke a message with multiple variations in target and arguments

Page 8: Pavel_Kravchenko_Mobile Development

Abstract Factory

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

The client is decoupled from any of the specifics of the concrete object obtained from the factory.

Page 9: Pavel_Kravchenko_Mobile Development

Abstract Factory

Page 10: Pavel_Kravchenko_Mobile Development

Abstract Factory

Use the Abstract Factory pattern when:

• a system should be independent of how its products are created and represented.

• a system should be configured with one of multiple families of products.

• a family of related product objects is designed to be used together

Page 11: Pavel_Kravchenko_Mobile Development

Class cluster

A class cluster is an architecture that

groups a number of private, concrete subclasses under an abstract superclass. The abstract superclass declares methods for creating instances of its private subclasses.

Each object returned may belong to a different private concrete subclass.

you don’t, and can’t, choose the class of the instance.

Page 12: Pavel_Kravchenko_Mobile Development

Class cluster

NSNumber *aChar = [NSNumber numberWithChar:’a’];

NSNumber *anInt = [NSNumber numberWithInt:1];

NSNumber *aFloat = [NSNumber numberWithFloat:1.0];

NSNumber *aDouble = [NSNumber numberWithDouble:1.0];

Page 13: Pavel_Kravchenko_Mobile Development

Class cluster

Since numbers of different types can be converted from one type to another and can be represented as strings, for example, they could be represented by a single class.

aChar, anInt, aFloat, aDouble—are objects of different private subclasses but them interfaces declared by the NSNumber

Don't release objects returned from factory methods

Page 14: Pavel_Kravchenko_Mobile Development

Chain of Responsibility

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.

Chain the receiving objects and pass the request along the chain until an object handles it.

Each object either handles the request or passes it to the next object in the chain.

Page 15: Pavel_Kravchenko_Mobile Development

Chain of Responsibility

Page 16: Pavel_Kravchenko_Mobile Development

Chain of Responsibility

Use Chain of Responsibility when

more than one object may handle a request, and the handler isn't known apriori. The handler should be ascertained automatically.

you want to issue a request to one of several objects without specifying the receiver explicitly.

the set of objects that can handle a request should be specified dynamically.

Page 17: Pavel_Kravchenko_Mobile Development

Chain of Responsibility

Chain consists of a series of responder objects (that is, objects inheriting from UIResponder)

If a given responder object doesn’t handle a particular message, it passes the message to the next responder in the chain

An application can have as many responder chains as it has windows

Only one responder chain can be active at a time—the one associated with the currently active window.

Page 18: Pavel_Kravchenko_Mobile Development

Chain of Responsibility

If a view is managed by a UIViewController object, the view controller becomes the next responder in the chain.

The design of the view hierarchy, which is closely related to the responder chain, adapts the Composite pattern.

Action messages—messages originating from control objects—are based on the target-action mechanism, which is an instance of the Command pattern.

Page 19: Pavel_Kravchenko_Mobile Development

Composite

Compose related objects into tree structures to represent part-whole hierarchies.

Composite lets clients treat individual objects and compositions of objects uniformly.

Page 20: Pavel_Kravchenko_Mobile Development

Composite

Page 21: Pavel_Kravchenko_Mobile Development

Composite

Use the Composite pattern when

you want to represent part-whole hierarchies of objects.

you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

Page 22: Pavel_Kravchenko_Mobile Development

Composite

The views in a window are internally structured into a view hierarchy.

At the root of the hierarchy is a window and its content view, a transparent view that fills the window’s content rectangle.

Views that are added to the content view become subviews of it, and they become the superviews of any views added to them.

A view can have one (and only one) superview and zero or more subviews.

Page 23: Pavel_Kravchenko_Mobile Development

Composite

Page 24: Pavel_Kravchenko_Mobile Development

Iterator

Provide an interface to access the elements of an aggregate object sequentially without exposing its underlying representation.

transfers the responsibility for accessing the elements from the collection to an iterator.

Different iterators can carry out different traversal policies.

Page 25: Pavel_Kravchenko_Mobile Development

Iterator

Use the Iterator pattern

to access an aggregate object's contents without exposing its internal representation.

to support multiple traversals of aggregate objects.

to provide a uniform interface for traversing different aggregate structures.

Page 26: Pavel_Kravchenko_Mobile Development

Iterator

Page 27: Pavel_Kravchenko_Mobile Development

Iterator

The NSEnumerator class in the Foundation framework implements the Iterator pattern.

Instances of NSDirectoryEnumerator class recursively enumerate the contents of a directory in the file system.

The collection classes such as NSArray, NSSet, and NSDictionary include methods that return an enumerator appropriate to the type of collection.

Page 28: Pavel_Kravchenko_Mobile Development

Memento

The Memento pattern captures and externalizes an object’s internal state—without violating encapsulation—so that the object can be restored to this state later.

The Memento pattern keeps the important state of a key object external from that object to maintain cohesion.

Page 29: Pavel_Kravchenko_Mobile Development

Memento

Page 30: Pavel_Kravchenko_Mobile Development

Memento

Use the Memento pattern when

a snapshot of (some portion of) an object's state must be saved so that it can be restored to that state later, and

a direct interface to obtaining the state would expose implementation details and break the object's encapsulation.

Page 31: Pavel_Kravchenko_Mobile Development

Memento

Archiving converts the objects in a program, along with those objects’ properties (attributes and relationships) into an archive that can be stored in the file system or transmitted between processes or across a network.

The archive captures the object graph of a program as an architecture-independent stream of bytes that preserves the identity of the objects and the relationships among them.

object’s type is stored along with its data.

Encoding and decoding are operations that you perform using an NSCoder object, preferably using the keyed archiving technique (using NSKeyedArchiver classes).

The object being encoded and decoded must conform to the NSCoding protocol