Design Pattern Overview
Process-oriented business processes are implemented by dividing functional modules and calling functions between each other. However, when the demand changes, you need to change the function. How many changes are called by the function, it is not easy to figure out how much data is associated. modifying a function is very likely to cause unnecessary bugs. Most of the time spent in maintenance and debugging is not about modifying bugs, but looking for bugs ...... It is useless to complain that demand is always changing. Changing the development process is king. The emergence of object-oriented programming is the problem brought about by changes.
Object-oriented encapsulation of areas that may change, so that it is easier to isolate the impact of code and changes. Obviously, objects are more stable and closed than flows, because of the complexity of object-oriented design, we all hope to make design solutions to cope with changes and improve reuse, and the design model can help us achieve this. By reusing accepted designs, we can avoid mistakes made by our predecessors when solving problems and benefit from others' experiences.
In the end, the object-oriented design model embodies abstract ideas, such as classes, abstract classes, and interfaces, all of which are abstract results at different levels and perspectives, however, only abstraction is not enough for program development. It also requires encapsulation, inheritance, and Polymorphism to make the software plump and vivid.
Adding a design pattern to the program is like the MSG, garlic, and chopped green onion that are put during cooking, so that the color and flavor of the fried food are delicious. That is to say, the integration of design patterns makes programs easier to maintain, expand, and reuse.
Which modes are available at first glance,
Based on the characteristics of the mode, there are three types: Creation type, structure type, and behavior type.
The creation mode has two main ideas:
1. encapsulation of specific classes to be used by the System
2. Hide the creation and combination of these classes
The Creation Mode emphasizes the creation mechanism of the object instance under specific circumstances. Through the appropriate object creation mechanism, the design troubles and program complexity can be reduced.
The schema focuses on how entities (classes or objects) are organized to form a larger structure. inheritance is used to organize interfaces or implementations.
The behavior pattern focuses on algorithm and task allocation between objects. The behavior design pattern identifies the common communication pattern between objects and implements this pattern, in this way, the flexibility of such communication can be improved.
The design patterns differ in granularity and abstraction layers. Here, we classify the patterns based on the two principles:
1. objective criteria, that is, what work the mode is used to accomplish
The creation mode is related to object creation.
A combination of structured mode processing classes or objects
The behavior mode describes how classes or objects interact and how responsibilities are assigned.
2. Range criterion, specifying whether an object is mainly used for a class or an object
The class mode processes the relationships between classes and subclasses. These relationships are established through inheritance and are static, and are determined during compilation;
The object mode processes the relationships between objects. These relationships can change at runtime and are more dynamic.
In a sense, almost all models use inheritance mechanisms.Therefore, the "class mode" only refers to the modes that focus on handling inter-class relationships, and most of the modes belong to the scope of the object mode.
The createtype class mode delays part of object creation to subclass, while the createtype Object Mode delays it to another object.
The structure class mode uses the Inheritance Mechanism to combine classes, while the structure Object Mode describes the object assembly method.
The behavior class mode uses inheritance to describe the algorithm and control flow, while the behavior Object Mode describes how a group of objects collaborate to complete tasks that a single object cannot complete.
The six principles are the criteria for measuring the advantages and disadvantages of the design model. A model may involve multiple principles. These six principles are:
Principle of opening/closing: software entities (classes, modules, functions, etc.) should be open to extensions and closed to modifications.
Rys replacement principle: child types must be able to replace their parent types. That is to say, child classes can be used wherever parent classes can be used.
Dependency inversion principle: A: the high-level module should not depend on the underlying module. Both should depend on abstract B. abstraction should not depend on details, and details should depend on abstraction.
Merging/aggregation Reuse Principle: Try to use merging/aggregation instead of class inheritance.
Dimit rule: if two classes do not need to communicate with each other directly, they should not interact directly. If one class needs to call a method of another class, this call can be forwarded by a third party.
Single Responsibility Principle: For a class, there should be only one reason for its change.
The six principles tell us to use abstraction to build a framework and implement expansion details. In retrospect, a single responsibility tells us that the implementation class should have a single responsibility, while the Li's replacement principle tells us not to undermine the inheritance system, the dependency reversal principle tells us to adopt interface-oriented programming. The synthesis/aggregation Reuse Principle tells us to use class inheritance as little as possible. The dimit rule tells us to reduce coupling, while the open and closed principle is the general principle, it tells us to open the extension and disable the modification.
The work of the design mode is decoupling, improving the code quality, making the program highly cohesive, low coupling, and easy to cope with changes, so that programmers can write better software.