China has four major inventions: gunpowder, paper, compass, and movable printing. In this place, the printing technology promotes the spread of human culture. We will not talk about this. What is the relationship between the design patterns? First, if there is an error in the typographical text, we can directly replace the error. This reflects the maintainability. second, after the Typographical Printing is complete, we can save these individual items so that they can be used in the next time.
China has four major inventions: gunpowder, paper, compass, and movable printing. In this place, the printing technology promotes the spread of human culture. We will not talk about this. What is the relationship between the design patterns? First, if there is an error in the typographical text, we can directly replace the error. This reflects maintainability. second, after the Typographical Printing is complete, we can save these individual items so that they can be used next time. This reflects reusability. third, we can move them horizontally or vertically during the typographical process, which reflects the flexibility. It is really worthy of being one of the four great inventions.
Why should we introduce object-oriented thinking and design patterns in our code. The introduction of object-oriented encapsulation, inheritance, and polymorphism can reduce the coupling degree of our programs (generally, we try to consider high cohesion and low coupling when designing code ). The introduced design pattern can be flexible in our code changes. There will be no major changes due to customer needs.
Now, go to the topic.What is a simple factory model?For example: our sales department. We need to buy peanuts, melon seeds, and drinks and go directly to the commissary. what we need to say is what we need and what the commissary gives us. At least this peanut, melon seeds, and drinks are separate classes. this is our simple engineering model. If we want to increase the demand for toys, we can purchase toys from the commissary. This is our simple factory model. How can I apply it in code? Let's take the addition, subtraction, subtraction, and division operations as an example:
An operation class is just an abstract class used for inheriting and using other specific classes. our specific classes (Addition class, subtraction class, multiplication Class, division class) when the GetResult method implements the corresponding operation. In simple engineering, the CreateOperation method is actually to create a specific class based on the needs of our client. for example, if I pass "+, create an actual object instance of the addition class and return it to the client. when we need to add a new class (such as the root code algorithm), we only need to inherit the abstract class (Operation class) in the specific GetResult method, you can implement the specific method. We do not need to change the original code. Without knowing it, we use the design principles and the open and closed principles (for extended development and for modification ). As you can see, even if we move from a PC client to a PDA operation, it will not change, but the interface needs to be re-developed. This reflects the reusability of our code.