Design Model-introduction and factory model (flat management mode VS professional manager mode), vs Professional Manager
This article will give a brief explanation of the design model, especially the concise analysis of the factory model:
I. Classification of design patterns
There are five creation modes: Factory method mode, abstract factory mode, Singleton mode, builder mode, and prototype mode.
There are seven structural modes: adapter mode, decorator mode, proxy mode, appearance mode, bridging mode, combination mode, and meta mode.
11 behavior models: rule mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, command mode, memorandum mode, state mode, visitor mode, intermediary mode, interpreter mode.
There are actually two types: Parallel hair mode and Thread Pool Mode.
Ii. six principles of the design model
1. Open Close Principle (Open Close Principle:
When the program needs to be expanded, you cannot modify the original code to achieve a hot swapping effect. Using Interfaces and abstract classes makes the program scalable and easy to maintain and upgrade.
2. If Liskov Substitution Principle has a base class, it can be a subclass:
Rishi replacement principle: any base class can appear, and sub-classes can certainly appear. The basis of inheritance reuse can be reused only when the primary class can replace the base class and the functions of the software unit are not affected, the category class can also add new behaviors based on the base class. The Li's replacement principle is a supplement to the "open-closed" principle. The key step for implementing the "open-close" principle is abstraction. The inheritance relationship between the base class and sub-classes is the specific implementation of abstraction. Therefore, the Li's replacement principle is the standard for the specific steps to implement abstraction. One of the basic principles of object-oriented design.
3. Dependency Inversion Principle dependency Abstraction
This is the basis of the open/closed principle. The specific content: For interface programming, it depends on abstraction rather than on specifics.
4. Interface Segregation Principle reduces dependency and Coupling
Using multiple isolated interfaces is better than using a single interface. To reduce the coupling between classes, the design mode is actually a software design idea. Starting from a large software architecture, it is easy to upgrade and maintain.
5. Demeter Principle: less coupling between entities
What is the principle of least understanding? That is to say, an entity should interact with other entities as little as possible so that the system function modules are relatively independent.
6. Composite Reuse Principle)
The principle is to use synthesis/aggregation as much as possible, rather than inheritance.
Iii. Introduction to the factory model-the concept of the flat management model VS the professional manager model factory model. since it refers to the factory model, we will describe the factory production products. The following is a story about entrepreneurs, let's look at the development process of his flat management model and professional manager model:
Figure 1. Flat management first, A boss started his own business and started A small workshop to produce A/B/C/D products. Like many startups, the boss directly manages R & D and production of several products. This is called flat management. But slowly, start-ups have survived, and there are more products, the boss alone. At this time, the boss has an international vision and hired a professional manager team for management, so that he can easily, second, you can focus on the future direction of the team, and the specific management practices will be handed over to the professional manager team with a higher business level. But !!! How can a professional manager be "derailed ?! As a result, you have signed production management standards with professional managers. You can only follow these standards. Otherwise, your team will be withdrawn. As a result, the simple factory model becomes operational, and the boss has become a big winner in his life. For example:
Figure 2. Professional Manager model (simple factory model) Now, let's look back at the factory model, which is easy to understand and clear. There are three main types: 1. Simple Factory; 2. Factory Method; 3. Abstract Factory ). Simple factory model as described above,
It consists of three roles (for the relationship, see the class diagram below ):
1. Factory roles: this is the core of this model and contains some commercial logic and judgment logic. In java, it is often implemented by a specific class. Second, it serves as a professional manager to help users generate and maintain specific product objects in the pipeline, so that users do not have to create and maintain objects themselves;
2. Abstract Product role: it is generally the parent class or implemented interface inherited by a specific product. It is implemented by interfaces or abstract classes in java. Second, its role is the standard maker of the product assembly line, so that the boss can safely hand over the production line to the Manager for management;
3. Specific product role: the object created by the factory class is the instance of this role. It is implemented by a specific class in java. A specific pipeline is a product pipeline established by a professional manager following the "standard" of abstract product roles under the boss's command, and created and maintained by the factory role;
Figure 3. Simple factory pattern class diagram
The simple factory mode should be well understood based on the above example. The following shows the java code implementation example:
// Product assembly line interface public interface Product {public void produceS ();} // A public class ProductA implements Product {public void produceS () {System. out. println ("My products A") ;}// <span style = "font-family: Arial, Helvetica, sans-serif; "> specific assembly line production Product B </span> public class ProductB implements Product {public void produceS () {System. out. println ("My production B Products") ;}// factory class (Professional Manager) public class ProductCreator {public Product createProduct (String type) {if ("". equals (type) {return new ProductA ();} if ("B ". equals (type) {return new ProductB ();} else return null;} public static void main (String [] args) {ProductCreator creator = new ProductCreator (); // The boss employs a new professional manager team creator. createProduct (""). produceS (); // The Boss issues a command to the professional manager, which is then implemented by the manager team. The boss does not care about the underlying creator. createProduct ("B "). produceS ();}}
Learning reference: