Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pavel_Kravchenko_Mobile Development


Published on

  • Be the first to comment

  • Be the first to like this

Pavel_Kravchenko_Mobile Development

  1. 1. Cocoa Design Patterns <ul><li>Pavel Kravchenko, Ciklum </li></ul>
  2. 2. What is a Design Pattern? <ul><li>A design pattern is: </li></ul><ul><li>a template for a design that solves a general problem </li></ul><ul><li>a tool of abstraction that is useful in architecture </li></ul><ul><li>A vocabulary to use when explaining complex software </li></ul>
  3. 3. Design principles <ul><li>encapsulate the aspects of system structure </li></ul><ul><li>program to an interface, not an implementation </li></ul><ul><li>Minimize coupling </li></ul>
  4. 4. The Command <ul><li>encapsulate a request as an object and </li></ul><ul><li>Separates message sender and reciever </li></ul>
  5. 5. The Command
  6. 6. The Command <ul><li>Use when you want to: </li></ul><ul><li>parameterize objects by an action to perform (Commands are an object-oriented replacement for callbacks). </li></ul><ul><li>specify and execute requests at different times (you can transfer a command object to a different process). </li></ul><ul><li>support undo. </li></ul><ul><li>support logging changes. </li></ul>
  7. 7. The Command <ul><li>Example: NSInvocation (encapsulates an Objective-C message) </li></ul><ul><li>An invocation object contains a target object, method selector, and method arguments </li></ul><ul><li>You can dynamically change its target and arguments </li></ul><ul><li>you can also obtain the return value from the object </li></ul><ul><li>you can repeatedly invoke a message with multiple variations in target and arguments </li></ul>
  8. 8. Abstract Factory <ul><li>Provide an interface for creating families of related or dependent objects without specifying their concrete classes. </li></ul><ul><li>The client is decoupled from any of the specifics of the concrete object obtained from the factory. </li></ul>
  9. 9. Abstract Factory
  10. 10. Abstract Factory <ul><li>Use the Abstract Factory pattern when: </li></ul><ul><li>• a system should be independent of how its products are created and represented. </li></ul><ul><li>• a system should be configured with one of multiple families of products. </li></ul><ul><li>• a family of related product objects is designed to be used together </li></ul>
  11. 11. Class cluster <ul><li>A class cluster is an architecture that </li></ul><ul><li>groups a number of private, concrete subclasses under an abstract superclass. The abstract superclass declares methods for creating instances of its private subclasses. </li></ul><ul><li>Each object returned may belong to a different private concrete subclass. </li></ul><ul><li>you don’t, and can’t, choose the class of the instance. </li></ul>
  12. 12. Class cluster <ul><li>NSNumber *aChar = [NSNumber numberWithChar:’a’]; </li></ul><ul><li>NSNumber *anInt = [NSNumber numberWithInt:1]; </li></ul><ul><li>NSNumber *aFloat = [NSNumber numberWithFloat:1.0]; </li></ul><ul><li>NSNumber *aDouble = [NSNumber numberWithDouble:1.0]; </li></ul>
  13. 13. Class cluster <ul><li>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. </li></ul><ul><li>aChar , anInt , aFloat , aDouble —are objects of different private subclasses but them interfaces declared by the NSNumber </li></ul><ul><li>Don't release objects returned from factory methods </li></ul>
  14. 14. Chain of Responsibility <ul><li>Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. </li></ul><ul><li>Chain the receiving objects and pass the request along the chain until an object handles it. </li></ul><ul><li>Each object either handles the request or passes it to the next object in the chain. </li></ul>
  15. 15. Chain of Responsibility
  16. 16. Chain of Responsibility <ul><li>Use Chain of Responsibility when </li></ul><ul><li>more than one object may handle a request, and the handler isn't known apriori . The handler should be ascertained automatically. </li></ul><ul><li>you want to issue a request to one of several objects without specifying the receiver explicitly. </li></ul><ul><li>the set of objects that can handle a request should be specified dynamically. </li></ul>
  17. 17. Chain of Responsibility <ul><li>Chain consists of a series of responder objects (that is, objects inheriting from UIResponder ) </li></ul><ul><li>If a given responder object doesn’t handle a particular message, it passes the message to the next responder in the chain </li></ul><ul><li>An application can have as many responder chains as it has windows </li></ul><ul><li>Only one responder chain can be active at a time—the one associated with the currently active window. </li></ul>
  18. 18. Chain of Responsibility <ul><li>If a view is managed by a UIViewController object, the view controller becomes the next responder in the chain. </li></ul><ul><li>The design of the view hierarchy, which is closely related to the responder chain, adapts the Composite pattern. </li></ul><ul><li>Action messages—messages originating from control objects—are based on the target-action mechanism, which is an instance of the Command pattern. </li></ul>
  19. 19. Composite <ul><li>Compose related objects into tree structures to represent part-whole hierarchies. </li></ul><ul><li>Composite lets clients treat individual objects and compositions of objects uniformly. </li></ul>
  20. 20. Composite
  21. 21. Composite <ul><li>Use the Composite pattern when </li></ul><ul><li>you want to represent part-whole hierarchies of objects. </li></ul><ul><li>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. </li></ul>
  22. 22. Composite <ul><li>The views in a window are internally structured into a view hierarchy. </li></ul><ul><li>At the root of the hierarchy is a window and its content view, a transparent view that fills the window’s content rectangle. </li></ul><ul><li>Views that are added to the content view become subviews of it, and they become the superviews of any views added to them. </li></ul><ul><li>A view can have one (and only one) superview and zero or more subviews. </li></ul>
  23. 23. Composite
  24. 24. Iterator <ul><li>Provide an interface to access the elements of an aggregate object sequentially without exposing its underlying representation. </li></ul><ul><li>transfers the responsibility for accessing the elements from the collection to an iterator. </li></ul><ul><li>Different iterators can carry out different traversal policies. </li></ul>
  25. 25. Iterator <ul><li>Use the Iterator pattern </li></ul><ul><li>to access an aggregate object's contents without exposing its internal representation. </li></ul><ul><li>to support multiple traversals of aggregate objects. </li></ul><ul><li>to provide a uniform interface for traversing different aggregate structures. </li></ul>
  26. 26. Iterator
  27. 27. Iterator <ul><li>The NSEnumerator class in the Foundation framework implements the Iterator pattern. </li></ul><ul><li>Instances of NSDirectoryEnumerator class recursively enumerate the contents of a directory in the file system. </li></ul><ul><li>The collection classes such as NSArray , NSSet , and NSDictionary include methods that return an enumerator appropriate to the type of collection. </li></ul>
  28. 28. Memento <ul><li>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. </li></ul><ul><li>The Memento pattern keeps the important state of a key object external from that object to maintain cohesion. </li></ul>
  29. 29. Memento
  30. 30. Memento <ul><li>Use the Memento pattern when </li></ul><ul><li>a snapshot of (some portion of) an object's state must be saved so that it can be restored to that state later, and </li></ul><ul><li>a direct interface to obtaining the state would expose implementation details and break the object's encapsulation. </li></ul>
  31. 31. Memento <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>object’s type is stored along with its data. </li></ul><ul><li>Encoding and decoding are operations that you perform using an NSCoder object, preferably using the keyed archiving technique (using NSKeyedArchiver classes). </li></ul><ul><li>The object being encoded and decoded must conform to the NSCoding protocol </li></ul>