Objective-C Programming path: IOS Design Pattern Analysis Study Notes -- Chapter 2 Chapter 1st: Hello, Design Pattern 1: model, view, controller (MVC)
Model:
The model object maintains the data of the application and defines the specific logic of the operation data. Model objects can be reused because the knowledge they represent is applicable to specific problem areas. For example, a model object can represent a complex data structure, corresponding to a user's drawing on the screen, or simply a to-do list in a to-do application.
View:
View objects can respond to user operations and show themselves on the screen. View objects are usually used to obtain data from application model objects for display. It can work with some, whole, or multiple model objects of a model object. Generally, you can modify data through it.
Controller:
A controller object is like a man-in-the-middle between a view object and a model object. As a man-in-the-middle or focal point, it establishes a communication channel so that the view can respond with a higher awareness of the model.
Ii. MVC as a composite design model
MVC itself is not the most basic design pattern. It contains several more basic design patterns.
Cocoa (touch) MVC uses the following modes:
- Combination-view objects form a view hierarchy in a collaborative manner, which can contain both composite views (such as table views) and independent views (such as text boxes or buttons ). Each view node at each level can respond to user operations and draw itself on the screen.
- Command-this is a "target-action" mechanism. view objects can be postponed to execute other objects (such as controllers), so that other objects can be executed after some time. This mechanism forms the command mode.
- The intermediary-the Controller object acts as the intermediary, and the intermediary adopts the intermediary mode, which forms a two-way channel for data transmission between the model and the view object. The Controller object of the application communicates the model change to the view object.
- Policy-the controller can make a "policy" of the view object ". View objects are isolated in order to maintain their unique responsibilities as the data display, and all application-specific interface behavior decisions are delegated to their "policy" objects (both controllers ).
- Observer-the model object sends internal state change notifications to the Controller and other objects it is concerned.
Iii. design principles that affect the design model
The design pattern is time-proven and effective. It is an abstraction of the main aspects of specific object-oriented design issues, reflecting the important ideas of object-oriented design. Some design principles affect the design model. These principles are rules of thumb for building reusable and maintainable object-oriented applications:
- Programming for interfaces instead of implementations.
- The combination of objects rather than class inheritance is preferred.
Iv. Advantages and Disadvantages of Design Principles
Benefits of interface programming:
- As long as the object meets the interface required by the client, the client does not have to care about the exact type used.
- The client only knows the Protocol or abstract class of the interface, so the client does not have to care about the exact type used.
Advantages and disadvantages of class inheritance: (white box reuse)
Advantages:
- Class inheritance is simple and direct, because the link is statically defined during compilation.
- Reusable implementation is easy to modify
Disadvantages:
- Because class inheritance is defined during compilation, it is impossible to change the implementation from parent class inheritance during runtime.
- The description of a subclass is often defined in the parent class.
- Subclass directly faces the implementation details of the parent class, thus damaging the encapsulation.
- Any changes made to the implementation of the parent class will force the sub-classes to be changed because their implementations are linked together.
- Because the inherited implementation in the new problem scenario has expired or is not applicable, you must override the parent class or inherited implementation.
Advantages and disadvantages of object combination: (black box reuse)
Advantages:
- The encapsulation is not broken, because only the interface is used to access the object.
- This greatly reduces implementation dependencies because object implementations are defined through interfaces.
- You can replace any object with other objects of the same type at runtime.
- Helps keep classes encapsulated to focus on a single task.
- Classes and their hierarchies can be kept concise and cannot be managed due to excessive expansion.
Disadvantages:
- The design involves many objects.
- System behavior depends on the relationship between different objects, rather than being defined in a single class.
- In ideal cases, you can reuse it without creating new components. In rare cases, through object combination, you can only combine existing components to obtain all the required functions. In fact, existing components are not enough.
- Despite the disadvantages above, object combinations still offer many benefits to system design. We can overcome these shortcomings by using class inheritance in some parts, making it easier to create new components using existing components.
- The combination of objects rather than class inheritance is preferred, not to say that class inheritance is not used at all. You need to make a clear judgment on How to reuse classes and objects according to the specific situation.