IOS development: Checks several common design patterns, and ios Design Patterns

Source: Internet
Author: User
Tags notification center

IOS development: Checks several common design patterns, and ios Design Patterns

(1) proxy Mode

Application Scenario: when some functions of a class need to be implemented by another class, but you are not sure which class will be implemented.
Advantage: decoupling
Agile principle: open-closed Principle
Example: The tableview data source delegate. It works with protocol to complete the delegate request.
List row count delegate
Custom delegate

(2) Observer Mode
Application Scenario: Generally, the model layer is used to notify the controller and view. The model layer is only responsible for publishing information, regardless of who receives the notification.
Advantage: decoupling
Agile principle: interface isolation principle, open-closed Principle
Instance: Notification center, registration Notification center. messages can be sent anywhere, and the objects registered with the observer can receive messages.
Kvo, key-value pairs are basically never used by the observer who changes the notification.
(3) MVC Mode
Application Scenario: it is a very old design model. It uses data models, controller logic, and views to display and logically divide applications.
Advantage: makes the system clear, with clear levels, clear responsibilities, and easy maintenance
Agile principle: open to expansion-closed to Modification
Instance: model-the data model, view-view display, and controller are used to control the UI display and data interaction logic.


(4) Singleton Mode
Application Scenario: Ensure that there is only one instance for a class during the running period for resource sharing control.
Advantage: easy to use, low latency, and cross-Module
Agile principle: single Responsibility Principle
Instance: [UIApplication sharedApplication].
Note: Make sure that the user can only obtain the instance through the getInstance method, which is a unique instance of the singleton class.
Java and C ++ make it non-public constructor, private and overwrite its constructor.
In object c, override the allocWithZone method to ensure that even if you use the alloc method to directly create an instance of the singleton class,
The returned result is only the unique static variable of the singleton class.


(5) Rule Mode
Application scenarios: define algorithm families and encapsulate them so that they can be replaced with each other.
Advantage: Changes to the algorithm are independent of those who use the algorithm.
Agile principle: interface isolation principle; multi-purpose combination, less inheritance; interface programming, rather than implementation.
Example: Sorting Algorithm, sortedArrayUsingSelector of NSArray; a typical duck will be called a flying case.
Note: 1. Extract the behavior that is easy to change and embed the abstract base class in combination.
2. The changed Behavior Abstraction base class is, all the changeable parent classes
3. The final instance of the user class sets the variable behavior by injecting the behavior instance.
This prevents irrelevant behaviors from being contaminated by child classes. Policy encapsulation and replacement are completed.


(6) Factory Model
Application Scenario: Create a class instance in the factory mode. It works with the proxy mode to create a replaceable proxy class.
Advantage: it is easy to replace and oriented to abstract programming. The application only has a call Relationship with the Common abstract classes of abstract factories and easy to change classes.
Agility principle: DIP Dependency inversion principle
Instance: when the project deployment environment depends on multiple different types of databases, use the factory with proxy to replace ease of use
Note: This mode is not recommended when the software structure and requirements are not stable at the initial stage of the project, because its disadvantages are also obvious,
This increases the complexity of the Code, the calling level, and the memory burden. So pay attention to prevent the abuse of the mode.

 

In cocoaChina, I also found a more detailed article about the design model and posted the website for future use.

Http://www.cocoachina.com/ios/20141111/10187.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.