Learning in simple Factory mode
A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.
For a summary of all design patterns, please refer to: http://blog.csdn.net/chr23899/article/details/46999267
For a summary of all design principles, please refer to: http://blog.csdn.net/chr23899/article/details/46999401
first look at the specific
The main consists of two numbers number_x and number_y below are four operations Add (+), Sub (-), Mul (*), DIV (/), and the factory instantiates the specific action type when the user clicks on one of the actions.
The class diagram for the application is as follows:
Specifically, the following code is implemented:
Operation class public class Operation {//defines two operands number_x with number_y private double number_x = 0; Private double number_y = 0; Encapsulates public double number_x {get {return number_x; number_x} set {number_x = value;} }//encapsulates the number_y public double number_y {get {return number_y;} set {number_y = value;} }//define virtual function public virtual double Result () {double number = 0; return number; }}//addition operation public class Operation_add:operation {public override double Result () { Double number = 0; Number = number_x + number_y; return number; }}//subtraction operation public class Operation_sub:operation {public override double Result () { Double number = 0; Number = number_x-number_y; return number; } }//multiplication operation public class Operation_mul:operation {public override double Result () {Dou ble number = 0; Number = number_x * NUMBER_Y; return number; }}//division public class Operation_div:operation {public override double Result () { Double number = 0; if (number_y = = 0) return 0; Number = number_x/number_y; return number; }}//define a factory class to get the specific operation class public class Factoryofoperation {public static operation Gettheoperation (string STR) {operation oper = null; Switch (str) {case "add": Oper = new Operation_add (); Break Case "Sub": Oper = new operation_sub (); Break Case "Mul": oper = new Operation_mul (); Break Case "div": oper = new Operation_div (); Break Default:break; } return oper; } }
SOURCE program: http://download.csdn.net/detail/chr23899/8922383
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design pattern Notes-Simple Factory mode