Python 设计模式–简单工厂模式

慢慢的做记录,做最强大的自己

class Variant2(Entity):
    def Func(self):
     pass
 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;
        }
  • 创建实体类

    class Entity(): #实体

    def Func(self):
        "方法体"
    

         #pass

  • 创建变体类

    class Variant1(Entity):

    def Func(self):
    

         pass

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

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

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

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

  步骤:

创建工厂类,实例化出合适的对象

  

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 results = 0;
            return results;
        }



    }

  简单工厂模式(Factory
Pattern)是一种创建型的设计模式,像工厂一样根据要求生产对象实例。

static void Main(string[] args)
        {
            Operation oper;
            oper = OperationFactory.CreateOperate("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();
            Console.WriteLine(result);
            Console.ReadLine();
        }

  

三层模型基本上如果不是用了特别的方法那么基本都是基于简单工厂模式来处理方法的

factory  = EntityFactory()
variant = factory.create_variant(choice)
print(variant.Func())

看了大话设计模式之后感触很深,发现自己还有很多学习的东西,设计软件并不是一两句代码把功能写完了就行,需要思考的内容有很多

  • 创建工厂类

    class EntityFactory():

    def create_variant(self,choice):
        #根据choice创建不同的variant类
        #函数体
        #return
    

但是简单工厂模式会存在一个问题,后续如果新增方法的话那么还需要继续创建运算类,然后实例化对象,那么这样会增加代码结构的复杂度,当然还是有方法解决的,但是这里就只做简单工厂模式的介绍了,

  使用工厂类:

相关文章

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图