When I learned the design model, I first browsed the overall story and then typed the examples by category, rather than the order of the whole book. The first time I came into contact with the design model, I felt deeply touched, just as I mentioned at the beginning of the story: I felt the great wisdom contained in the design evolution, one screen worth remembering in the life of happy and furious programs. Although understanding has not yet reached that realm, when looking at the design model, I can't help but applaud the wise summary of those experts. In the past, I only knew that most of the programming languages we learned were object-oriented or based on object-oriented, but I didn't know how to be object-oriented. I learned UML and may recognize one or two class diagrams, however, it is not clear how these class diagrams are represented in the Code. The benefits of knowing object-oriented are maintenance, reusable, scalable, and flexible, however, little is known about how to achieve the expected results. At this time, the design model came, and he gave us a bright golden Avenue to the confused "dishes. Let us realize that programming is not only finished after the code is run, but also an art. Let's talk about the three factory models that I just learned: simple factory, factory method, and abstract factory model.
Before introducing the above design patterns, let's take a look at several design principles:
① OCP: (Open-Closed Principle, Open-Closed Principle) It is Open for expansion and Closed for changes. In my understanding, if you want to expand a system, you should do so without modifying the existing code.
② DIP (dependency Inversion Principle, Dependence Inversion Principle): programming interfaces rather than implementation. In other words, for different levels of programming, the high-level exposure to low levels should only be an interface, rather than a specific class.
Simple factories, factory methods, and abstract factories all belong to the Creation Mode in the design mode. The main function is to help us extract the instantiation part of the object, thus optimizing the code architecture and enhancing the scalability of the system.
· Simple factory
A factory class in a simple factory generally uses a static method to return different instances by receiving different parameters. But if you want to implement expansion, you need to change the source code. Therefore, this is not in line with the OCP principle. In a simple factory, there is an "abstract product class", which can be a class or an Interface. All required product classes are subclasses of this "abstract product class. A simple factory generally only contains a specific factory class, which generates all product class objects. The switch statement is used internally to create different objects based on different input parameters.
· Factory Method
The factory method is to provide a factory class for each product. You can create different product instances using different factory instances. You can add any product in the same level structure. This makes up for the shortcomings of the simple factory and satisfies the DIP principle.
There is also an abstract product class for the factory method, but he writes the abstract factory class in the simple factory class into an interface, and then writes each product into a factory, it is used to instantiate its own product. If you want to add a product, you can directly compile a factory for the product.
· Abstract Factory
Abstract Factory and factory methods are very similar. They have an abstract product class. Each abstract product class can be derived from multiple specific products. An abstract factory class, multiple factory classes can be derived. You can create multiple product instances for each specific factory class.
It differs from the factory method in the following ways:
The factory method has only one abstract product class, while the abstract factory has multiple;
A factory method can only create one instance of a specific product type, while an abstract factory can create multiple instances.