C # design mode simple Factory mode

Source: Internet
Author: User
Tags square root

Why use Design patterns?

1, the design pattern is based on the experience of the predecessors summed up, the use of design patterns, is equivalent to standing on the shoulders of predecessors.

2, the design mode makes the program easy to read. People who are familiar with design patterns should be able to easily read programs written using design patterns.

3, design mode can make the program has good scalability, to meet the system design of the open and closed principle. For example, the strategy mode is to encapsulate different algorithms in subclasses, when new algorithms need to be added, just add new subclasses, implement the specified interface, can add new system behavior without changing the existing system source code.

4. Design mode can reduce the coupling degree between class and class in the system. For example, Factory mode, so that dependent classes only need to know the interface implemented by the dependent class or inherited abstract classes, so that the coupling between the dependency class and the dependent class is reduced.

5, design mode can improve the reuse of code. For example, the adapter mode will be able to be existing in the system to meet the new requirements of the function code to be compatible with the new requirements of the interface proposed.

6. Design pattern can provide ready-made solutions for some common problems.

7. Design patterns increase the way code is reused. For example, the adorner pattern, reusing code that already exists in the system without inheriting it.

The current summary of the design pattern has 23 kinds of use widely, we first talk about the simple factory model, light talk definition is difficult to understand the use of, we quoted a case, but we better understand the simple design pattern.

Case: If you write a calculator, you may soon write the following code:

1   Public  class operation 2     {3        public    static  double GetResult (double numbera,double Numberb, String operate) 4  5         {6  7           double result=0; 8  9           switch (operate) {Ten case             ' + ': 12 13              result=numbera+numberb;14             break;16  .... Subtraction, multiplication, division ellipsis (+),           return  result;22    )

interface implementation code omitted;

After writing this operation class we can reuse this method, can be in the web version, mobile phone and so on to write the calculator, but if I want the calculator in addition to a square root function, we just need to add a statement in the switch, it seems very simple, but you add a statement to let subtraction compile together again, So we need to modify the above, separate each method, so we use the Simple Factory mode (simplefactory).

Operation class:

 1 namespace simplefactory 2 {3 public class operation 4 {5 public double Numbera{get;set;} 6 Pub             LIC double Numberb{get;set;} 7 8 public virtual double GetResult () {9 double result = 0;10 Return result;11}12}13 class Operationadd:operation14 {public override double Getresul         T () + {double result = 0;18 result= Numbera + numberb;19 return result;20             }21}22 class Operationsub:operation23 {public override double GetResult () 25 {26     Double result = 0;27 result = numbera-numberb;28 return result;29}30}31 Class Operationmulti:operation32 {public override double GetResult () {double RE Sult = 0;36 result = Numbera * numberb;37 return result;38}39}40 class operation div:operation41 {42        public override Double GetResult () (numberb==0) {THR ow new Exception ("divisor cannot be 0!"); }48 else49 {Double result = 0;51 result = number a/numberb;52 return result;53}54}55}56}

Factory class:

1 namespace simplefactory 2 {3 Public     class Operationfactory 4     {5 public         static Operation Createoperate (Stri Ng operate) {6             operation oper = null, 7             switch (operate) 8             {9 case                 "+":                     oper = new Operationadd (); One                     break;12 case                 "-":                     oper = new Operationsub (), break;15 case                 "*":                     oper = new Operationmulti ();                     break;18 case                 "/":                     oper = new Operationdiv ();                     break;21                 default:22                     break;23             }             return oper;25         }26     }27}


Now, we just need to enter the operator and instantiate the corresponding class to implement the calculator's results:

     Operation oper = new operation ();     Oper = operationfactory.createoperate ("+");      Oper. Numbera = 1;     Oper. Numberb = 2;     Double Result=oper. GetResult ();

If we need to add any new operations, only the subclass can be added, plus branching and changing the interface and the operation class is OK!

Class diagram for simple Factory mode:

C # design mode simple Factory mode

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.