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: "); 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; Operation = operationfactory. createoperate (stroperate); then. numbera = convert. todouble (strnumbera); random. numberb = convert. todouble (strnumberb); strresult = random. getresult (). tostring (); console. writeline (" The result is: "+ 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 { /// <Summary>      /// Operation class      /// </Summary>      Public   Class Operation { Private   Double _ Numbera = 0; Private   Double _ Numberb = 0; /// <Summary>          /// Number         /// </Summary>          Public   Double Numbera { Get { Return _ Numbera ;} Set {_ Numbera = Value ;}} /// <Summary>          /// Number B          /// </Summary>          Public   Double Numberb {Get { Return _ Numberb ;} Set {_ Numberb = Value ;}} /// <Summary>          /// Obtain the calculation result          /// </Summary>          /// <Returns> </returns>          Public   Virtual   Double Getresult (){ Double Result = 0; Return Result ;} /// <Summary>          /// Check whether the input string is correct          /// </Summary>          /// <Param name = "currentnumber"> </param>          /// <Param name = "inputstring"> </param>          /// <Returns> </returns>          Public   Static   String Checknumberinput ( String Currentnumber, String Inputstring ){ String Result ="  "; If (Inputstring =" . "){ If (Currentnumber. indexof (" . ") <0 ){ If (Currentnumber. Length = 0) Result =" 0 "+ Inputstring; Else Result = currentnumber + inputstring ;}}Else   If (Currentnumber =" 0 ") {Result = inputstring ;} Else {Result = currentnumber + inputstring ;} Return Result ;}} /// <Summary>      /// Addition class      /// </Summary>      Class Operationadd: Operation { Public   Override  Double Getresult (){ Double Result = 0; Result = numbera + numberb; Return Result ;}} /// <Summary>      /// Subtraction class      /// </Summary>      Class Operationsub: Operation { Public   Override   Double Getresult (){ Double Result = 0; Result = numbera-numberb;Return Result ;}} /// <Summary>      /// Multiplication class      /// </Summary>      Class Operationmul: Operation { Public   Override   Double Getresult (){ Double Result = 0; Result = numbera * numberb; Return Result ;}} /// <Summary>      /// Division class     /// </Summary>      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 ;}}/// <Summary>      /// Square      /// </Summary>      Class Operationsqr: Operation { Public   Override   Double Getresult (){ Double Result = 0; Result = numberb * numberb; Return Result ;}} /// <Summary>      /// Square root class      /// </Summary>     Class Operationsqrt: Operation { Public   Override   Double Getresult (){ Double Result = 0; If (Numberb <0) Throw   New Exception (" Negative Numbers cannot open the square root. "); Result = math. SQRT (numberb ); Return Result ;}} /// <Summary>     /// Inverse data type      /// </Summary>      Class Operationreverse: Operation { Public   Override   Double Getresult (){ Double Result = 0; Result =-numberb; Return Result ;}} /// <Summary>      ///Computing Factory      /// </Summary>  Public  Class Operationfactory { Public   Static Operation createoperate ( String Operate) {operation operator = Null ; Switch (Operate ){ Case " + ": {Region = New Operationadd (); Break ;} Case "- ": {Region = New Operationsub (); Break ;} Case " * ": {Region = New Operationmul (); Break ;} Case " / ": {Region = New Operationdiv ();Break ;} Case " Sqr ": {Region = New Operationsqr (); Break ;} Case " SQRT ": {Region = New Operationsqrt (); Break ;} Case " + /- ": {Region = New Operationreverse (); Break ;}} Return Success ;}}}
 
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.