Simple Factory mode and Factory mode

Source: Internet
Author: User

Before we compare the two modes, we need to learn the open-closed principle. In the previous learning process, heard many times, writing procedures to follow the opening and closing principles.

In the process of doing the project, because of the need to add, often update the software. To update the software, you need to design the code to modify it. In object-oriented program design, classes are often written. If you are adding a requirement, modify the completed class. Then he does not conform to the principle of opening and closing.

To add requirements, you should let the code expand rather than modify it. The opening and closing principles are realized by abstract class and inheritance.

Opening and closing principle: for the extension is open, for the change is closed.

Using the open and closed principle, you can bring great benefits to object-oriented programming, so that code can be maintained, scalable, reusable, and flexible.

First, take a look at the Simple factory model:

Code to implement:

public class Operation//define action class {private Double NUM1 = 0;
        Private double num2 = 0;
            public double Numbera//first operand {get {return num1;}
        set {NUM1 = value;}
            public double Numberb//second operand {get {return num2;}
        set {num2 = value;}
            Public virtual double GetResult ()//Return execution Results {double result = 0;
        return result;
        } public class Operationadd:operation//Additive class, inheriting action class {public override Double GetResult ()
            {double result = 0;
            result = Numbera + Numberb;
        return result; } public class Operationsub:operation//subtraction class, inheriting action class {public override double GetResult
            () {double result;
            result = Numbera-numberb; Return rEsult; } public class Operationmul:operation//multiplication class, inheriting action class {public override double GetResult
            () {double result;
            result = Numbera * NUMBERB;
        return result; } public class Operationdiv:operation//Division class, inheriting action class {public override double GetResult
            () {double result;
            result = Numbera/numberb;
        return result; The public class Operationfactory//Set up a simple factory {public static Operation create
            Operate (String Operate) {Operation oper = null;
                    Switch (operate) {case "+": oper=new Operationadd ();
                Break
                    Case "-": Oper=new operationsub ();
                Break
           Case "*": oper=new Operationmul ();         Break
                    Case "/": Oper =new operationdiv ();
            Break
        return oper;
 }
    }


Through the example above, you will find that in the simple factory model, if you want to increase the demand, the class that must be modified has operationfactory, each need to modify the factory to increase the demand, which is both open to expand, but also open to the modification. This violates the open-closed principle.

In a simple factory, do not need to switch to which class of instances, only to the ' + ' to the factory, the factory automatically gives the corresponding strength. The client only needs to do the operation. According to different examples to achieve different operations, extension needs to modify the factory case statements. So it violates the principle of opening and closing.

For the factory model, according to the different examples, to correspond to different factories. The modification is closed. If you need to expand, you just need to add the appropriate factory class. So there is no need to modify the original factory.

Look at the factory model:

Code to implement:

Class Program {static void Main (string[] args {}} public class Operation
        Define action class {private Double NUM1 = 0;
        Private double num2 = 0;
            public double Numbera//first operand {get {return num1;}
        set {NUM1 = value;}
            public double Numberb//second operand {get {return num2;}
        set {num2 = value;}
            Public virtual double GetResult ()//Get execution Results {double result = 0;
        return result;
        } public class Operationadd:operation//Additive class, inheriting action class {public override Double GetResult ()
            {double result = 0;
            result = Numbera + Numberb;
        return result;
        } public class Operationsub:operation//subtraction class, inheriting action class {public override Double GetResult () {Double result;
            result = Numbera-numberb;
        return result;
        } public class Operationmul:operation//multiplication class, inheriting action class {public override Double GetResult ()
            {double result;
            result = Numbera * NUMBERB;
        return result;
        } public class Operationdiv:operation//Division class, inheriting action class {public override Double GetResult ()
            {double result;
            result = Numbera/numberb;
        return result;
    Interface Ifactory//Set up a factory interface {Operation createoperation ();
            Class Addfactory:ifactory//additive type factory {public Operation createoperation () {
        return new Operationadd ();
            Class Subfactory:ifactory//Subtraction Factory {public Operation createoperation () {
        return new Operationsub (); }
    }
    Class Mulfactory:ifactory//multiplication class factory {public Operation createoperation () {
        return new Operationmul ();
            } class Divfactory:ifactory//Division class factory {public Operation createoperation () {
        return new Operationdiv ();

 }
    }


Comparing the two design patterns, the factory pattern is slightly improved in simple Factory mode, better than simple Factory mode, which makes the code maintainable, scalable, reusable and flexible.

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.