官方澳门新永利下载:简单工厂,简单工厂模式

逐步的做笔录,做最有力的团结

  简单工厂格局(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

发表评论

电子邮件地址不会被公开。 必填项已用*标注