Reviewing the Old, working six months with the greatest feeling is the lack of basic knowledge of the knowledge base is weak, hereby review the 23 design patterns, and with some typical application scenarios to simulate the use of that model.
Language being used is Microsoft Typescript, with C #, JAVA and other languages in some places there will be some discrepancies.
Simple factory (Simple Factory)
First, it does not belong to the 23 design patterns, but after the Abstract Factory, Factory Method pattern are thus evolved, and will use the factory to create fewer cases of analogy, therefore regarded design mode "parent" of the .
Ado, the first on demand: make a simple calculator (addition, subtraction).
Class Diagram
Operation operation type
class Operation { private _numberA: number; private _numberB: number; constructor(){ } get numberA(): number { return this._numberA; } set numberA(num: number) { this._numberA = num; } get numberB(): number { return this._numberB; } set numberB(num: number) { this._numberB = num; } /** * 得到结果 */ public GetResult(): number { let result: number = 0; return result; } }
OperationAdd, OperationSub, OperationMul, OperationDiv Math class (derived class operation)
/**加类**/ class OperationAdd extends Operation { constructor() { super(); } public GetResult(): number { super.GetResult(); let result: number = 0; result = this.numberA + this.numberB; return result; } } /**减类**/ class OperationSub extends Operation { constructor() { super(); } public GetResult(): number { super.GetResult(); let result: number = 0; result = this.numberA - this.numberB; return result; } } /**乘类**/ class OperationMul extends Operation { constructor() { super(); } public GetResult(): number { super.GetResult(); let result: number = 0; result = this.numberA * this.numberB; return result; } } /**除类**/ class OperationDiv extends Operation { constructor() { super(); } public GetResult(): number { super.GetResult(); let result: number = 0; if (this.numberB == 0) throw (new Error("除数不能为0!")); result = this.numberA / this.numberB; return result; } }
OperationFactory simple calculation factory class (the class is instantiated with the above different conditions)
class OperationFactory { constructor() { } public static createOperate (operate: string): Operation { let oper: Operation = null; switch(operate) { case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/": oper = new OperationDiv(); break; } return oper; } }
Main client test
Oper the let: Operation; Oper = OperationFactory.createOperate ( "/"); // input conditions, in addition to examples of the production of the class oper.numberA =. 5; // input digital A oper.numberB = 0; // input digital B the let RES : Number = oper.GetResult (); // obtained calculation result the console.log (oper.numberA, oper.numberB, RES); // error: the divisor is not 0!
Advantages and disadvantages
Advantages: only need to pass the right parameters, you can obtain the required objects without having to know the details of its creation.
Disadvantages: duty factory class is relatively heavy, needs to be modified to add new products factory class determination logic, contrary to the principle of opening and closing.