SoFunction
Updated on 2025-04-07

A brief analysis of the .net simple factory model

A technique in programming is even more of an art

The simple factory model uses object-oriented methods to reduce the coupling degree of programs through inheritance, encapsulation, and polymorphism. The design model makes the program more flexible, easy to modify, and easy to reuse.

Here is the server calculator code:

Copy the codeThe code is as follows:

 using System;
 using ;
 using ;
 using ;
 using ;
 namespace DesignModel
 {
     /// <summary>
/// calculator
     /// </summary>
public class Calculator //Creating a calculator base class can accept two parameters. Any algorithm only needs to rewrite the calculation result method.
     {
         private double _numberA;
         private double _numberB;
         public double NumberA
         {
             get { return this._numberA; }
             set { this._numberA = value; }
         }
         public double NumberB
         {
             get { return this._numberB; }
             set { this._numberB = value; }
         }
         public virtual double GetResult()
         {
             double result = 0;
             return result;
         }
     }
     /// <summary>
/// Addition
     /// </summary>
public class Add: Calculator    //Every time you add a calculation method, you only need to add one calculation class and rewrite the base class method.
     {
         public override double GetResult()
         {
             return  NumberA + NumberB;
         }
     }
     /// <summary>
/// Subtraction
     /// </summary>
     public class Sub : Calculator
     {
         public override double GetResult()
         {
             return NumberA + NumberB;
         }
     }
     /// <summary>
/// Calculator factory
     /// </summary>
     public class CalculatorFactory
     {
         public static Calculator GetResult(string oper)
         {
             Calculator calcu = null;
             switch (oper)
             {
                 case "+":
                     calcu = new Add();
                     break;
                 case "-":
                     calcu = new Sub();
                     break;
             }
             return calcu;
         }
     }
 }

Copy the codeThe code is as follows:

  static void Main(string[] args)
         {
("Please enter the number A:");
             string numbera = ();
("Please enter operator:");
             string oper = ();
("Please enter the number B:");
             string numberb = ();
             Calculator c = (oper);
             = (numbera);
             = (numberb);
             (("{0}{1}{2}={3}", numbera, oper, numberb, ()));
             ();
         }

Basic verification has not been added, students who are studying and practicing can add it themselves

28 design patterns are updated subsequently

Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
using ;
namespace DesignModel
{
    /// <summary>
/// calculator
    /// </summary>
public class Calculator //Creating a calculator base class can accept two parameters. Any algorithm only needs to rewrite the calculation result method.
    {
        private double _numberA;
        private double _numberB;
        public double NumberA
        {
            get { return this._numberA; }
            set { this._numberA = value; }
        }
        public double NumberB
        {
            get { return this._numberB; }
            set { this._numberB = value; }
        }
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
    /// <summary>
/// Addition
    /// </summary>
public class Add: Calculator    //Every time you add a calculation method, you only need to add one calculation class and rewrite the base class method.
    {
        public override double GetResult()
        {
            return  NumberA + NumberB;
        }
    }
    /// <summary>
/// Subtraction
    /// </summary>
    public class Sub : Calculator
    {
        public override double GetResult()
        {
            return NumberA + NumberB;
        }
    }
    /// <summary>
/// Calculator factory
    /// </summary>
    public class CalculatorFactory
    {
        public static Calculator GetResult(string oper)
        {
            Calculator calcu = null;
            switch (oper)
            {
                case "+":
                    calcu = new Add();
                    break;
                case "-":
                    calcu = new Sub();
                    break;
            }
            return calcu;
        }
    }
}

This article is about the simple factory model in the .net design model. It is very simple. In the next article, let’s talk about the strategy model.