Introduction to several design patterns in iOS development

Source: Internet
Author: User

iOS development learning, often do not understand the development model of iOS, today we will be a simple summary and discussion ~
(a) Agent mode
Scenario: When some functionality of a class needs to be implemented by another class, it is not certain which class implementation is specific.
Advantages: Decoupling
Agile principle: open-closed principle
Example: TableView data source delegate, through and protocol cooperation, complete the request.
List row number delegate
Custom delegate


(ii) Observer pattern
Application scenario: Generally the model layer pair, controller and view of the notification method, do not care who to receive, only responsible for publishing information.
Advantages: Decoupling
Agile principles: Interface Isolation principle, open-closed principle
Example: Notification Notification Center, Registration Notification Center, any location can send a message, registered observer object can be received.
KVO, the key value to change the notice of the observer, usually basically useless.
(iii) MVC pattern
Application scenario: is a very old design pattern, through the data model, controller logic, view shows the application is logically divided.
Advantages: Make the system, clear level, responsibility clear, easy to maintain
Agile principles: Open to extensions-closed for modifications
Example: model-is the data model, view-view display, Controller for UI presentation and data interaction logic control.


(iv) Singleton mode
Application scenario: Ensure that a program runs in a class that has only one instance for resource sharing control.
Advantages: Easy to use, time-delay evaluation, ease of cross-module
Agile principles: a single responsibility principle
Example: [UIApplication sharedapplication].
Note: Make sure that the user is only available through the GetInstance method, only instances of the Singleton class.
Java,c++ it without a public constructor, privatizing and overwriting its constructors.
Object C, overriding the Allocwithzone method, guaranteeing that even if the user creates an instance of the Singleton class directly with the Alloc method,
The only static variable that is returned is only this singleton class.


(v) Strategy model
Application scenario: Define the algorithm family and encapsulate it so that they can be replaced with each other.
Advantage: Make the algorithm change independent of the user using the algorithm
Agile principles: Interface isolation principles, multi-use combinations, less inheritance, programming for interfaces, not implementations.
Example: Sorting algorithm, Nsarray sortedarrayusingselector; classic Duck will call, can fly case.
Caveats: 1, the easy-to-change behavior in the Peel class, embedded in the abstract base class in a composite way
2, the behavior of the change abstract base class for, all mutable change of the parent class
3, the final instance of the user class, set the variable behavior by injecting the behavior instance
Prevents the inheritance behavior mode, causes the unrelated behavior to pollute the subclass. Policy encapsulation and substitution have been completed.


(vi) Factory model
Scenario: Factory mode creates an instance of the class, with more than proxy mode, creating replaceable proxy classes.
Advantages: Easy to replace, oriented to abstract programming, application only with abstract factory and variable class common abstract class has a call relationship.
Agile principle: Dip dependency Inversion principle
Example: When a project deployment environment relies on several different types of databases, you need to use the factory Mate proxy to complete the ease of replacement
Note: In the early stages of the project, when the software structure and requirements are not stabilized, it is not recommended to use this mode, because its disadvantage is also obvious, increase the complexity of the code, increase the call level, increase the memory burden. So pay attention to preventing patterns from abusing.

Introduction to several design patterns in iOS development

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.