Cai Xue design mode factory design mode simple factory pattern

Source: Internet
Author: User
The factory design mode of the design mode.

The factory model has the following forms:

  • Simple factory Mode
  • Factory method
  • Abstract Factory Mode

Advantages:
The factory class contains the necessary judgment logic to determine when to create a product class instance. The client can avoid the responsibility of directly creating product objects, instead of simply "consuming" products. The simple factory model achieves division of responsibility through this approach.

Disadvantages:
When the product has a complex multi-layer hierarchical structure, the factory class only has its own, and should not change, is the disadvantage of the model. Because the factory class integrates the creation logic of all products, the entire system will be affected once it fails to work normally.

At the same time, it is difficult to expand the system. Once a new product is added, the factory logic has to be modified, which may cause the factory logic to be too complex.

In addition, the simple factory mode usually uses the static factory method, which makes it impossible to inherit from sub-classes, and the factory role cannot form a hierarchy based on inheritance. The above description is from: C # design mode (4)-simple factory pattern

Foreground call:

Class program {static void main (string [] ARGs) {try {console. write ("Enter the number A:"); string strnumbera = console. readline (); console. write ("select the operator number (+,-, *,/):"); string stroperate = console. readline (); console. write ("Enter the number B:"); string strnumberb = console. readline (); string strresult = ""; Operation operation; operator = operationfactory. createoperate (stroperate); then. numbera = convert. todouble (strnumbera); random. numberb = convert. todouble (strnumberb); strresult = random. getresult (). tostring (); console. writeline ("Result:" + strresult); console. readline ();} catch (exception ex) {console. writeline ("your input is incorrect:" + ex. message );}}}

 

Operation and Operation Factory:

Using system; using system. Collections. Generic; using system. Text; namespace operationlibrary {////// Operation class // public class operation {private double _ numbera = 0; private double _ numberb = 0 ;////// Number a // public double numbera {get {return _ numbera;} set {_ numbera = value ;}}////// Number B // public double numberb {get {return _ numberb;} set {_ numberb = value ;}}////// Obtain the calculation result //////
 Public Virtual double getresult () {double result = 0; return result ;}////// Check whether the input string is correct ////////////
 Public static string checknumberinput (string currentnumber, string inputstring) {string result = ""; if (inputstring = ". ") {If (currentnumber. indexof (". ") if (currentnumber. length = 0) Result = "0" + inputstring; else result = currentnumber + inputstring ;}} else if (currentnumber = "0") {result = inputstring ;} else {result = currentnumber + inputstring;} return result ;}}////// Addition class // class operationadd: Operation {public override double getresult () {double result = 0; Result = numbera + numberb; return result ;}}////// Subtraction class // class operationsub: Operation {public override double getresult () {double result = 0; Result = numbera-numberb; return result ;}}////// Multiplication class // class operationmul: Operation {public override double getresult () {double result = 0; Result = numbera * numberb; return result ;}}////// Division class // class operationdiv: Operation {public override double getresult () {double result = 0; If (numberb = 0) throw new exception ("the divisor cannot be 0. "); Result = numbera/numberb; return result ;}}////// Square class // class operationsqr: Operation {public override double getresult () {double result = 0; Result = numberb * numberb; return result ;}}////// Square root class // class operationsqrt: Operation {public override double getresult () {double result = 0; If (numberb throw new exception ("negative numbers cannot open the square root. "); Result = math. SQRT (numberb); return result ;}}////// Inverse number class // class operationreverse: Operation {public override double getresult () {double result = 0; Result =-numberb; return result ;}}//////Computing Factory/// Public class operationfactory {public static operation createoperate (string operate) {operation operator = NULL; Switch (operate) {Case "+": {operator = new operationadd (); break;} case "-": {condition = new operationsub (); break;} case "*": {condition = new operationmul (); break;} case "/": {operator = new operationdiv (); break;} case "sqr": {operator = new operationsqr (); break;} case "SQRT": {operator = new operationsqrt (); break;} case "+/-": {response = new operationreverse (); break ;}} return response ;}}}
Similar structure.

Reference: C # design mode (4)-simple factory pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.