Skip to main content

iConcept: COCOA Design Pattern

A Design Pattern is a reocurring solution to a common software engineering problem. The idea behind design patterns is to not continually reinvent the wheel.
Design Patterns differ from Templates in that they do not contain executable code. http://goo.gl/OeSCu
An example of a Design Pattern is the Listener (Observer). A Listener is an object which waits for information from another object. Now, it is clear that this is a very useful basic idea. Listeners are used in all aspects of Mac OS X and Cocoa for many purposes, not the least of which is GUI management. However, rather than stumble over the same design issues whenever you need to implement a type of listener, the Listener is a documented design pattern that features prepared solutions to these issues. You don't need to discover for yourself that all the Observers for an object can be stored in a single array, because this is included in the Design Pattern.
For more on design patterns, including many examples: http://www.dofactory.com/patterns/Patterns.aspx http://www.vincehuston.org/dp/ (old url http://home.earthlink.net/~huston2/dp/patterns.html)

Question: Is there any place on the net to discuss advanced programming techniques, problems arising when trying to apply patterns and similar (preferably related to CeePlusPlus), just like this WikiWikiWeb is?
How about the Portland Pattern Repository? http://www.c2.com . It's the first WikiWikiWeb.

Digest/Directory of standard Gang of Four design patterns in Cocoa
It is informative to note that many of the famous design patterns in "Design Patterns" were first recognized in NeXTstep, a precursor to Cocoa, and in fact "Design Patterns" provides many such citations. Many of the patterns exist within Cocoa with different names than the Gang of Four gave them for the "Design Patterns" book.
The following is a digest/directory of standard Gang of Four design patterns and their names, roles, and relationships within Cocoa. For each pattern, there are links to pages within this site including discussions, named Cocoa classes that participate in the patterns, and commentary about the patterns in the context of Cocoa.
The place to start is Apple�s own design patterns documentation https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/DesignPatterns">CocoaDesignPatterns/DesignPatterns">CocoaDesignPatterns.html
Creational Patterns
*Abstract Factory
Creates an instance of several families of classes *
NSObject Objective-C class methods are sometimes call factory methods. This pattern is intrinsic to the Objective-C language. Every Objective C class is potentially an abstract factory. This pattern is also used to implement CocoaClassClustersSubClass">ShouldClassClusterPlaceholderBeASubClasshttp://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Builder Separates object construction from its representation *
NSDocumentManager? � Builds NSDocument instances with NSWindowManager(s), handles recent documents menu, builds document related responder chain� NSApplication? � Builds main menu, initial responder chain, run loop�
*Factory Method Creates an instance of several derived classes *
Objective-C class methods are sometimes call factory methods. This pattern is intrinsic to the Objective-C language. NSObject ClassClustersSubClass">ShouldClassClusterPlaceholderBeASubClasshttp://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/index.html
*Prototype A fully initialized instance to be copied or cloned *
NSCell is a Prototype when used with NSMatrix.
*Singleton A class of which only a single instance can exist *
SingletonDesignPattern SingletonAlternatives NSApplication NSNull NSFileManager NSWorkspace http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaObjects/Articles/CreateSingleton.html http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaObjects/Articles/ObjectCreation.html
Structural Patterns
*Adapter Match interfaces of different classes *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Bridge Separates an object�s interface from its implementation *
*Composite A tree structure of simple and composite objects *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.htmlWrappingIsBad
*Decorator Add responsibilities to objects dynamically *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Facade A single class that represents an entire subsystem *
NSImage - NSText and NSTextView encapsulate the functionality of complex text layout and editing which is internally implement with NSTextStorage, NSLayoutManager, and other classes.
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Flyweight A fine-grained instance used for efficient sharing * NSCell
*Proxy An object representing another object *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
Behavioral Patterns
*Chain of Resp. A way of passing a request between a chain of objects *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Command Encapsulate a command request as an object *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Interpreter A way to include language elements in a program *
Value Transformers?
*Iterator Sequentially access the elements of a collection *
NSEnumerator http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Mediator Defines simplified communication between classes *
NSObjectController, NSArrayController, NSTreeController, and NSUserDefaultsController http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Memento Capture and restore an object's internal state *
NSCoder NSKeyedArchiver and NSKeyUnarchiver http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Observer A way of notifying change to a number of classes *
NSNotificationCenterhttp://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*State Alter an object's behavior when its state changes *
*Strategy Encapsulates an algorithm inside a class *
NSLayoutMananger NSFormatter?
*TemplateMethod Defer the exact steps of an algorithm to a subclass *
http://developer.apple.com/documentation/Cocoa/Conceptual/DesignPatterns">CocoaDesignPatterns/index.html
*Visitor Defines a new operation to a class without change *
Cocoa Unique Patterns
*Target/Action A variation/simplification of the Command pattern *
NSControl NSActionCell @selector Similar to Qt Signals & Slots
*Method Swizzling A technique for runtime modification of the relationship between message selectors and the implementation that will be called in response to a message *
*Delegation Enable one object to control, modify, or specialize the behavior of another object through a loosely coupled interface.
This is really an example of the Strategy design pattern. In fact if you look at the "Application" algorithm implemented as a Strategy in Rob Martin's Agile Principles, Patterns, and Practices you see NSApplication + AppDelegatereproduced almost exactly.
Reference : Source Here 
http://www.stepwise.com/Articles/Technical/2000-03-03.01.html http://developer.apple.com/documentation/Cocoa/Conceptual/CommunicateWithObjects/Articles/Delegates.html

Comments

Popular posts from this blog

WPF-MVVM: RelayCommand Implementation

In WPF if we are implementing MVVM pattern then we need to play with Command rather than Events. You can use ICommand interface to create each command class. Implementation of ICommand in a class gives you CanExecute(), Execute() methods which take part in the action performed by Command.   Rather than making Command Class for each Command we can implement a generic Relay Command to get Command. Below is a RelayCommand class that we will implement.   ///   <summary>      ///  To register commands in MMVM pattern      ///   </summary>      class   RelayCommands  :  ICommand     {          readonly   Action < object > _execute;          readonly   Predicate < object > _canExecute;  ...

.Net List with Changed event

Sometimes we need a List which can notify user when an item is added. Here is the way that you can implement a generic ArrayList which notifies user at the time of an element is added.   using  System; using  System.Collections; namespace  ArchiveData.Logging {    // A delegate type for hooking up change notifications.    public   delegate   void   ChangedEventHandler ( object  sender,  EventArgs  e);    public   class   ListWithChangedEvent  :  ArrayList   {      // An event that clients can use to be notified whenever the      // elements of the list change.      public   event   ChangedEventHandler  Changed;      public   object  NewlyAddedItem {...

What is DispatcherTimer in wpf?

DispatcherTimer When you want to set a timer working with GUI, you always come across threading problem. The problem is that if you want to send some changes to UI that is constantly/continuously changing then that will make your UI unresponsive or in other words it will hang your UI.   To overcome from this situation, WPF gives us DispatcherTimer threading functionality that will take care of such continuously changing processing on UI thread and that will not hang your UI. We can accomplish same scenario in Win Form , through System.Windows.Forms.Timer and in WPF it is System.Windows.Threading.DispatcherTimer .   Difference between DispatcherTimer and Regular timer (System.Timers.Timer) DispatcherTimer is the regular timer. It fires its Tick event on the UI thread, you can do anything you want with the UI. System.Timers.Timer is an asynchronous timer, its Elapsed event runs on a thread pool thread. You have to be very careful in your event handler...