C ා design mode III (simple factory class)

Keywords: calculator

Simple factory class of C design pattern

I. Introduction

If a class is compared to a product, a new object is to produce a product.
Now there are several classes that we need to selectively produce. Then we will introduce a concept called factory.

What is a factory?
The factory is the place where all kinds of products can be produced. The production demand is based on the requirements of customers. In the program, simply speaking, you can specify a factory, new, to produce a specified class. Other classes not selected do not use new to prevent resource disclosure. The whole new process is completed in the factory without any interference from customers.

2, Scenarios for simple factory applications

Simple factory is very suitable for the same data, different operations. For example, a calculator.
If you want to add / subtract / multiply / divide two numbers. Because both numbers are the same, and the method can be different. In this case, it is not necessary to create all instances of the four classes by adding, subtracting, multiplying and dividing. You can select the new one through the factory, and then perform relevant calculations.

3, Simple factory model example

//Operation basic specification, which is the parent class to be regarded as the interface of Standard Specification

public class Operation
{
    private double _numberA = 0;
    private double _numberB = 0;

    public double NumberA
    {
        get{ return _numberA;}
        set{ _numberA = value;}
    }

    public double NumberB
    {
        get{ return _numberB;}
        set{ _numberB = value;}
    }

    public virtual double GetResult()
    {
        double result = 0;
        return result;
    }
}

//Specific calculation classes such as addition, subtraction, multiplication and division are all inherited from Operation. Meet certain specifications

class OperationAdd : Operation  //plus
{
    public override double GetResult()
    {
        double result = 0;
        result = NumberA + NumberB;
        return result;
    }
}

class OperationSub : Operation  //reduce
{
    public override double GetResult()
    {
        double result = 0;
        result = NumberA - NumberB;
        return result;
    }
}

class OperationMul : Operation  //ride
{
    public override double GetResult()
    {
        double result = 0;
        result = NumberA * NumberB;
        return result;
    }
}

class OperationDiv : Operation //except
{
    public override double GetResult()
    {
        double result = 0;
        if (NumberB == 0)
            throw new Exception("Divisor cannot be 0.");
        result = NumberA / NumberB;
        return result;
    }
}

class OperationSqr : Operation  //square
{
    public override double GetResult()
    {
        double result = 0;
        result = NumberB * NumberB;
        return result;
    }
}

class OperationSqrt : Operation  //square root
{
    public override double GetResult()
    {
        double result = 0;
        if (NumberB < 0)
            throw new Exception("Negative numbers cannot be square roots.");
        result = Math.Sqrt(NumberB);
        return result;
    }
}

class OperationReverse : Operation //Contrary number
{
    public override double GetResult()
    {
        double result = 0;
        result = -NumberB;
        return result;
    }
}

//Operation factory is mainly used to selectively create a specified class

public class OperationFactory
{
    public static Operation createOperate(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;
                }
            case "sqr":
                {
                    oper = new OperationSqr();
                    break;
                }
            case "sqrt":
                {
                    oper = new OperationSqrt();
                    break;
                }
            case "+/-":
                {
                    oper = new OperationReverse();
                    break;
                }
        }

        return oper;
    }
}

/ / client

//The core of a simple factory is to choose which class to create through the factory. So we can operate according to the selected class
//The classes in the factory conform to certain standards, that is, they are all derived from the same parent class, so they have a unified interface

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.Write("please enter a number A: ");
            string strNumberA = Console.ReadLine();
            Console.Write("Please select operation symbol(+,-,*,/): ");
            string strOperate = Console.ReadLine();
            Console.Write("please enter a number B: ");
            string strNumberB = Console.ReadLine();
            string strResult = "";

            Operation oper;
            oper = OperationFactory.createOperate(strOperate); //Select which class to create as the current one through operation factory
            oper.NumberA = Convert.ToDouble(strNumberA);       //Assign value A of the selected class
            oper.NumberB = Convert.ToDouble(strNumberB);       //Assign value B to the selected class
            strResult = oper.GetResult().ToString();           //Access the method in the selected class to return the operation result

            Console.WriteLine("The result is:" + strResult);

            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Your input is wrong:" + ex.Message);
        }
    }
}

Four, summary

Simple operation factory is to selectively create an object of a specified class, and other unused objects are not created.

In this way, the creation of a class is actually completed in the factory, and the client does not need to interfere with the specific implementation class. Just deal with the factory.

51 original articles published, praised 0, 824 visitors
Private letter follow

Posted by crash58 on Fri, 07 Feb 2020 00:28:38 -0800