Several design patterns are commonly used: Proxy mode, observer mode, MVC mode, singleton mode, strategy mode, Factory mode, MVVM
First, the agent
Scenario: When some function of a class needs to be implemented by another class, it is not certain that the class implementation will be.
Advantages: Decoupling
Agile principles: openness--the principle of closure
Example: TableView data source delegate, through and protocol cooperation, complete the request.
List row number delegate, custom delegate
A word summary: the function of passing objects to implement objects
Ii. observers
Scenario: Usually 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.
Third, MVC
Scenario: is a very old design pattern that logically divides the application through the data model, controller logic, and view display.
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. Single case
Scenario: Ensure that a program runs in a class that has only one instance that is used 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, ensuring that even if the user creates a singleton directly using the Alloc method
Class, the only static variable returned is the Singleton class.
V. Strategy
Scenario: Define the algorithm families and encapsulate them 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.
Precautions:
1. Easy-to-change behavior in stripping classes, embedding abstract base classes 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 behavior instances, set the variable behavior
Prevents the inheritance behavior mode, causes the unrelated behavior to pollute the subclass. Policy encapsulation and substitution have been completed.
Six, the factory
Scenario: Factory mode creates an instance of the class, with more than proxy mode, creating replaceable proxy classes.
"specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class. ”
The world is made up of a factory class that dynamically decides which product class to create, based on the parameters passed in.
Brief analysis of the structure diagram:
ConcreteProduct1 and ConcreteProduct2 two products have a common parent class iproject, the Simple factory class is simplefactory, Responsible for determining the production ConcreteProduct1 or ConcreteProduct2 products according to the different parameters passed in.
Advantages: Easy to replace, oriented to abstract programming, application only with abstract factory and variable class common abstract class has a call relationship.
Through the simple reconstruction of the factory model, we are idle of the low Lotus-fit code structure, so that the external expansion of open, to modify the closure. If you add any more methods of operation,
Several design patterns in IOS development