Common design Patterns
(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: This mode is not recommended when the software structure and requirements are not stabilized at the beginning of the project, because its disadvantage is also obvious,
Increases the complexity of the code, increases the call level, and increases the memory load. So pay attention to preventing patterns from abusing.
What are the drawbacks of a single meeting?
Key Benefits:
1. Provides controlled access to the unique instance.
2, because there is only one object in the system memory, so it can save system resources, for some of the objects need to be created and destroyed frequently the singleton pattern can undoubtedly improve the performance of the system.
3. Allow a variable number of instances.
Main disadvantages:
1, because there is no abstraction layer in the simple interest mode, the expansion of the Singleton class has great difficulty.
2, the single-case category of excessive responsibility, to a certain extent, contrary to the "single principle of responsibility."
3, abuse of a single case will bring some negative problems, such as to save resources to the database connection pool object design as a singleton class, may cause the sharing connection pool object too many programs and a connection pool overflow, if the instantiated object is not exploited for a long time, the system will be considered as garbage, which will result in the loss of object state.
Design patterns commonly used in iOS development