Design Pattern 2 (simple factory pattern)

Source: Internet
Author: User

Preface

The simple factory mode returns one of several possible classes based on the provided data or parameters. The common points are a bit like polymorphism in Object-Oriented Programming. A base class has multiple Derived classes, in other callsProgramAccording to the parameters to determine which specific derived class of the base class is returned, the return value is the base class type, because the base class reference can point to the derived class object, in addition, all these derived classes contain functions of the base class. That is to say, the derived classes have the same functions, but the functions may be implemented differently.

The following is a demonstration of the simple factory model,CodeNot too complex. Therefore, you can use the submile text tool.

Usage blogArticleLink.

Simple factory Mode


Assume that A is a base class, and AB and AC are derived from a's derived classes. In the xfactory class, the AB or AC class is returned based on the parameters provided to it, the instance of the class returned is not that important for programmers, because these classes have the same method. What programmers need to do is to directly call the method through the base class reference, you don't have to worry about which derived class is returned, because these classes have the same method, but they are implemented differently. How to decide which derived class to return depends on the factory design. This method can be a very complex function or a simple function.

Next we will use a simple addition, subtraction, multiplication, division calculator to understand and implement the simple factory model.

Step 1:Based on the small example above, I first create a base class

     ///  Operation operations      Public   Class  Operation {  Public   Double Numbera { Get ; Set  ;}  Public   Double Numberb { Get ; Set ;}  Public   Virtual   Double  Getresult (){  Double Result = 0  ;  Return  Result ;}} 

Step 2: Create the derived class AB AC, that is, add, subtract, multiply, and divide the four Derived classes.

     ///  Addition, subtraction, multiplication, and Division  ///  Addition class      Public  Class  Operationadd: Operation {  Public   Override   Double  Getresult (){  Double Result = 0  ; Result = This . Numbera + This  . Numberb;  Return  Result ;}} ///  Subtraction      Public   Class  Operationsub: Operation {  Public   Override   Double  Getresult (){  Double Result = 0  ; Result = This . Numbera- This  . Numberb; Return  Result ;}}  ///   <Summary>      ///  Multiplication  ///   </Summary>      Public   Class  Operationmul: Operation {  Public   Override   Double  Getresult (){ Double Result = 0  ; Result = This . Numbera * This  . Numberb;  Return  Result ;}}  ///   <Summary>      ///  Division  ///   </Summary>      Public  Class  Operationdiv: Operation {  Public   Override   Double  Getresult (){  Double Result = 0  ;  If ( This . Numberb = 0  ){  Throw   New Exception ( "  The divisor cannot be 0.  "  );} Result = This . Numbera/ This  . Numberb;  Return  Result ;}} 

Step 3: Create xfactory to return AB and AC.

     ///  Simple Factory      Public   Class  Operationfactory { Public   Static Operation createoperate ( String  Operate) {operation = Null  ;  Switch  (Operate ){  Case  "  +  "  : Bytes = New Operationadd ();  Break  ;  Case  "  -  "  : Bytes = New  Operationsub ();  Break  ;  Case  "  *  " : Bytes = New  Operationmul ();  Break  ;  Case  "  /  "  : Bytes = New  Operationdiv ();  Break  ;}  Return Success ;}} 

Step 4: start with an addition call:

         Static   Void Main ( String  [] ARGs) {operation = Operationfactory. createoperate ( "  +  "  ); Console. writeline (  "  Enter the first digit:  "  ); Role. numbera =Convert. todouble (console. Readline (); console. writeline (  "  Enter the second digit B:  "  ); Role. numberb = Convert. todouble (console. Readline (); console. writeline (  "  Result: {0}  "  , Response. getresult (); console. Readline ();} 

The running result is

Summary

In this simple factory model, we actually use some object-oriented programming ideas.

Creating operation is actually for the businessEncapsulation.

Then implement addition, subtraction, multiplication, division, and class capitalInheritanceOperation and rewrite the corresponding business logic.

If we add anotherAlgorithmTo use the SQRT algorithm, you only need to add a new class that inherits operation and add a branch to the simple factory class. Here we find that this isEasy to expand (loose coupling) and easy to maintain.

 

Related Article

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.