1. Definition of factory pattern
Factory pattern is our most commonly used instantiated object pattern. It is a pattern that replaces new operation with factory method. Because factory mode is equivalent to creating new instance objects, we often need to generate instance objects according to class Class, such as A = new A () factory mode, which is also used to create instance objects, so we can consider whether we need to use factory mode in the future when new. Although doing so may do more work, it will bring more extensibility and fewer modifications to your system. Quantity.
2. Principle of Factory Model
3. An example is given to realize simple calculation of each return result.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Test
{
//Arithmetic class
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;
}
}
//Addition, subtraction, multiplication and division classes
class OperationAdd:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
//subtraction
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
//multiplication
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
//division
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception("The divisor cannot be zero");
result = NumberA / NumberB;
return result;
}
}
//Simple factory
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;
}
return oper;
}
}
public class Program
{
static void Main(string[] args)
{
Console.Write("Select Operational Symbols(+ ,- ,*,/,): ");
string strOperate = Console.ReadLine();
Operation oper;
oper = OperationFactory.createOperate(strOperate);
Console.Write("please enter a number A: ");
oper.NumberA = Convert.ToDouble(Console.ReadLine());
Console.Write("please enter a number B: ");
oper.NumberB = Convert.ToDouble(Console.ReadLine());
double result = oper.GetResult();
Console.WriteLine("The result is:" + result);
Console.ReadLine();
}
}
}
4. advantages
It conforms to the principle of opening and closing, because adding new products does not need to modify the original factory category, just need to add the corresponding factory.
Separate responsibility for use from responsibility for creation.
It conforms to the order of abstraction before concrete design.
5. disadvantages
If there are too many product classes, there will be many more classes, which will make the system very uncomplicated.
If the selection logic is handed over to the user, it will increase the user's dependence on details to a certain extent.
Compared with the simple factory model, it increases the cost of creating the factory itself.
Users need to create the factory class itself. (The creation of the factory class can be delegated to the abstract class through certain methods.)