In iOS development learning, I often don’t understand the development model of iOS. Today we will give a brief summary and discussion~
(I) Agent Mode
Application scenario: When certain functions of a class need to be implemented by other classes, but it is not certain which class will be implemented.
Advantages: Decoupling
Agile principle: open-closed principle
Example: Tableview's data source delegate, through cooperation with protocol, the delegation request is completed.
List row number delegate
Custom delegate
(II) Observer mode
Application scenario: Generally, it is the notification method of model layer pairs, controllers and view. It does not care who receives it, but is only responsible for publishing information.
Advantages: Decoupling
Agile principle: interface isolation principle, open-closed principle
Example: Notification Notification Center, Registration Notification Center, messages can be sent anywhere, and objects that register observers can be received.
kvo, the observer of the key-value pair change notification, has basically never used it.
(III) MVC mode
Application scenario: It is a very ancient design pattern in 1998. The application is logically divided through data model, controller logic, and view display.
Advantages: Make the system clear, the responsibilities are clear, and easy to maintain
Agile principle: open to extensions - closed to modifications
Example: model-i.e. data model, view-view display, controller performs logical control of UI display and data interaction.
(IV) Singleton Mode
Application scenario: Ensure that there is only one instance of a certain class during the program run, and it is used to control resource sharing.
Advantages: Easy to use, delay evaluation, easy to cross modules
Agility Principle: Single Responsibility Principle
Example: [UIApplication sharedApplication].
Note: Make sure that the user can only obtain a unique instance of the singleton class through the getInstance method.
java, C++ makes it without public constructors, privatize and overwrite its constructors.
In object c, rewrite the allocWithZone method to ensure that the user directly creates an instance of the singleton class using the alloc method.
What is returned is also the only static variable of this singleton class.
(V) Strategy Model
Application scenario: Define the algorithm family and encapsulate them so that they can be replaced by each other.
Advantages: Make the algorithm change independent of the user who uses the algorithm
Agile principle: interface isolation principle; use more combinations and less inheritance; programming for interfaces, not implementation.
Example: sorting algorithm, sortedArrayUsingSelector of NSArray; classic ducks can yell and fly.
Notes: 1. Remove behaviors that are easy to change in the class and embed abstract base classes through combinations.
2. The abstract base class of changing behavior is, all variable changes parent classes
3. The final instance of the user class, sets volatile behaviors by injecting behavior instances.
It prevents inheriting behavior methods and leads to irrelevant behavioral contamination of subclasses. Completed policy encapsulation and replaceability.
(VI) Factory model
Application scenario: Create instances of classes in factory mode, mostly cooperate with proxy mode, and create alternative proxy classes.
Advantages: Easy to replace, oriented towards abstract programming, application only has call relationships with common abstract classes of abstract factories and mutable classes.
Agile principle: DIP dependency inversion principle
Example: When relying on multiple different types of databases in a project deployment environment, it is necessary to use factory and proxy to complete ease of use replacement.
Note: When the software structure and requirements have not stabilized in the early stage of the project, it is not recommended to use this model because its disadvantages are also obvious.
Increases the complexity of the code, increases the calling level, and increases the memory burden. Therefore, be careful to prevent the abuse of the pattern.
The above is a summary of the design patterns in IOS development introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!