Reference: http://marshal.easymorse.com/archives/3296
There are some basic design patterns in iOS:
- Delegation
- Protocol
- Model-vew-controller
- Target-action
- Notification
- Declared properties
The following describes the usage of each design pattern.
Delegation
From: http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html
This is Apple's authoritative explanation.
Delegation, delegation mode (there is also a common proxy mode, the difference between the two is that the proxy mode is generally stricter, the same interface is implemented, the delegation only references the entrusted object ), is a simple and powerful mode that allows an object to act as another object. The delegate object persists to the reference of another object and sends a message to another object when appropriate. The delegate object can do some extra work when sending messages.
In the delegate mode in the cocoa framework, the delegate object is often an object in the framework, and the delegated object is a custom controller object. The delegated object maintains a weak reference to the delegated object.
The example in this document is developed on mac.
The delegated object is an instance of the NSWindow class, which declares the protocol (protocol). One of the methods is windowShouldClose. When you click the close button of the window, the window object sends the windowShouldClose message to the proxy object (windowDelegate) and asks whether to close the window. The proxy object returns a bool value, which controls the behavior of the window object.
There are similar examples in iOS. In the created Project, the classes directory contains xxAppDelegate, which is the delegate class. In this delegate class instance, UIWindow and Controller are referenced. Similar to the mac example above, the behavior of the referenced object can be controlled.
Protocol
It can be considered as an interface in java ). There are two protocol types in iOS:
- Formal protocol: The @ interface declaration requires sub-class implementation methods, which can be forced to implement (@ required) or optional (@ optional). The formal protocol is an extension of the objc language;
- Informal protocol is a category of NSObject, and category is a feature of the objc language. It allows you to add methods for a class without subclass implementation. The informal protocol implementation method is optional. Before the Optional protocol method does not introduce objc 2.0, the informal protocol is the basic way to implement delegation in the foundation and appkit classes.
Model-View-Controller
You should be familiar with the MVC design pattern.
Target-Action
From: http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/TargetAction.html
Target-action is a design pattern. The object maintains necessary information and sends messages to other objects when an event occurs. The maintained information consists of two parts:
- Action selector, which defines the name of the method to be called;
- Target, the object that receives the message.
When an event called action message occurs, the method message defined by action selector is sent to the target.
The target-action mode is generally used for custom controllers to process action messages in a way defined by application specifications.
Notification
A notification is a message used to notify one or more observer objects that an event occurs. Here, the recipient is the observer and observer. It is actually the observer mode.
Here, the recipient of the notification does not know who will receive the notification. In this way, the notification and the observer are loosely coupled.
Declared property
It is a declared attribute. For example:
@ Property (<# attributes #>) <# type #> <# name #>;
It is something that enhances the objc syntax and simplifies the "syntactic Sugar" nature of code writing.