设计方式——轻松工厂

2019-12-06 22:28栏目:bob体育平台
TAG:

日益的做笔录,做最有力的亲善

  轻松工厂形式(Factory Pattern)是生机勃勃种创建型的设计格局,像工厂雷同依据必要生育目的实例。

看了牛皮设计方式之后感触很深,开掘本人还会有众多学学的事物,设计软件并非风华正茂两句代码把效果与利益写完了就能够,要求理念的剧情有超多

  特点:依照不一样的口径,工厂实例化出万分的对象。

代码来源参照他事他说加以考察大话设计形式那本书,这里在博客里记录一下,不容许每一遍都去翻书,可是在博客里面是那多少个好找的。

  《大话设计情势》中实例:四则运算总结器

范例为五个粗略工厂格局的总括器,首先先成立叁个总括器运算所急需利用的数据模型,在java内部也叫bean,就是一个大而无当的类,这里大家先创设叁个Operation类

  代码:

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;
        }



    }
 1 #!/usr/bin/env python
 2 #-*- coding: utf-8 -*-
 3 
 4 class Operation:
 5     def getResult(self):
 6         pass
 7 
 8 class OperationAdd(Operation):
 9     def getResult(self):
10         return self.op1+self.op2
11 
12 class OperationSub(Operation):
13     def getResult(self):
14         return self.op1-self.op2
15     
16 class OperationMul(Operation):
17     def getResult(slef):
18         return self.op1*self.op2
19 
20 class OperationDiv(Operation):
21     def getResult(self):
22         try:
23             return self.op1/float(self.op2)
24         except:
25             print("Error:除数为0!")
26             return 0
27 
28 class OperationOther(Operation):
29     def getResult(self):
30         print("Error:没有定义的运算符!")
31         return 0
32     
33 
34 class OperationFactory:
35     
36     operation = {}
37     operation["+"] = OperationAdd()
38     operation["-"] = OperationSub()
39     operation["*"] = OperationMul()
40     operation["/"] = OperationDiv()
41     
42     def createOperation(self,choice):
43         if choice in self.operation.keys():
44             op = self.operation[choice]
45         else:
46             op = OperationOther()
47         return op
48 
49 
50 
51 if __name__ == "__main__":
52     op = raw_input("请输入运算符:")
53     num_a = input("a:")
54     num_b = input("b:")
55 
56     factory = OperationFactory()
57     cal = factory.createOperation(op)
58 
59     cal.op1 = num_a
60     cal.op2 = num_b
61     
62     print(u"运算结果为:" + str(cal.getResult()))
63 

下一场再成立加减乘除类,以便工厂开展调用

  

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 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

版权声明:本文由bob体育app发布于bob体育平台,转载请注明出处:设计方式——轻松工厂