Simple factory mode and factory Mode
In my opinion, the design pattern is for software engineering. It reduces program coupling through design, including encapsulation, inheritance, and polymorphism, so that software engineering is easy to modify and take easily. Although as a manufacturer, there is no need to deal with the client interface architecture, but the design pattern can be found at the beginning of daily code work, unlike the code structure that acm ignores.
Let's talk about the simple factory model this time. After all, this is easier to understand. In the first article of the big talk design model, I read a book and read it back and forth to see the most.
What is a factory and what can be produced in batches according to the mold. What is a model? A model refers to a core knowledge system extracted from the production experience and life experience through abstraction and sublimation. Pattern is actually a methodology for solving a certain type of problems. Summarize the methods for solving a certain type of problem to the theoretical level, that is, the mode.
Therefore, the factory model is to send our "Mold" to the factory, and then provide us with a "product" after the factory is processed ". The core of the simple factory model is the separation of business logic.
The previous code:
class Factory { public static E createObject(E object) { Object ob = null; swtich(object) { case "a": ob = new makeObjectA(); break; case "b": ob = new makeObjectB(); break; } return ob;
The above is a factory class. When we pass in an object, the factory generates an object based on our mold and processes the data based on this object.
public class makeObjectA(){ private E segA; private E segB; public E cal() { return segA*segB; }}
Object ob;ob = Factory.createObject("a");ob.segA = tmpObject;ob.segB = tmpObject;ob.cal();
The general framework is like this. When there are many mold types, the business logic is separated, and the modification and reuse are simpler when the demand changes.
You can also define interfaces to regulate the behavior of objects produced by the factory and ensure the behavior of objects to achieve standardized production.
interface makeObject{ public E cal();}
Then let
makeObjectA implements makeObject{}
A simple factory model that separates business logic from customer logic becomes.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.