Common design Patterns in iOS (1) proxy mode
Scenario: When some functionality of a class needs to be implemented by another class, it is not certain which class implementation is specific.
Advantage:
Agile principle: open-closed principle
Example: tableView
The data source delegate
, through and protocol
the cooperation, complete the request.
List row
number delegate
of customdelegate
(2) Viewer mode
Application scenario: Generally for model
layer pairs, controller
and the view
way of notification, 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
(3) MVC pattern
Application scenario: is a very old design pattern that uses data models, controller logic, and view shows to logically divide the application.
Advantages: To make the system, clear level, clearly defined responsibilities, easy maintenance.
Agile principles: Open to extensions-closed for modifications
Examples: model
-the data model, the view
view display, the controller
logical control of UI presentation and data interaction.
(4) Simple interest mode
Scenario: Ensure that a program runs on 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
Instance [UIApplication sharedApplication]
.
Precautions:
Make sure that the user is only able to getInstance
get through the method, the only instance of the simple interest class.
java
, c++
so that it does not have a public constructor, privatisation, and overrides its constructor.
Objective-C
, the overriding allocWithZone
method ensures that even if alloc
the user method creates an instance of the simple interest class directly, only the static variable of this simple interest class is returned.
(5) Strategy mode
Application scenario: Define the algorithm family, encapsulate it, and they can replace each other.
Advantage: Make the algorithm change independent of the user using the algorithm
Agile principle: Interface Isolation principle, multi-use combination, less inheritance, programming for the interface, not implementation.
Precautions:
- 1. The easy-to-change behavior in the Peel class is embedded in the abstract base class in a combined manner.
- 2. Changing behavior abstract base class for, all mutable changes of the parent class
- 3. The final instance of the user class, by injecting the behavior instance, sets the variable behavior, prevents the inheritance behavior way, causes the unrelated behavior to pollute the subclass. Policy encapsulation and substitution are completed.
(6) Factory mode
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 pattern misuse.
Common design Patterns for iOS