The design pattern in my opinion is for software engineering, through the design including encapsulation, inheritance, polymorphism, etc. to reduce the degree of coupling, making software engineering easy to modify and easy to take. Although as a vendor worker, there is no need to face the client to develop a customer interface architecture of the work, but the design pattern in the daily code work is a hand to know there is no, unlike the brush ACM disregard code structure.
This time to talk about the simple factory model, after all this is easier to understand in the big talk design mode first, turn a book to go back and forth to see the most.
何为工厂,能够批量按照模具生产的东西。何为模式,模式是指从生产经验和生活经验中经过抽象和升华提炼出来的核心知识体系。模式(Pattern)其实就是解决某一类问题的方法论。把解决某类问题的方法总结归纳到理论高度,那就是模式。
Therefore, the factory model is to send our "mold" to the factory, factory processing to provide us with a "product." The core of the simple factory model is the separation of business logic.
Previous section of code:
class Factory { publicstaticcreateObjectobject) { null; swtich(object) { case"a": new makeObjectA(); break; case"b": new makeObjectB(); break; } return ob;
Above is a factory class, when we pass an object, factory according to our mold, the output of an object to us, we based on this object processing data.
publicclassmakeObjectA(){ private E segA; private E segB; publiccal() { return segA*segB; }}
Object ob;ob = Factory.createObject("a");ob.segA = tmpObject;ob.segB = tmpObject;ob.cal();
The general framework is this, when the type of mold to separate the business logic, when the need to change the time to modify and reuse easier.
Interfaces can also be defined to standardize the behavior of factory-produced objects and to ensure that the object's behavior is standardized in order to achieve standardised production.
interface makeObject{ publiccal();}
Then let
makeObjectAimplementsmakeObject{}
A simple factory model that separates business and customer logic.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simple Factory mode