bwin必赢棋牌设计模式,简单工厂模式

日渐的做笔录,做最有力的本人

  轻巧工厂方式(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

发表评论

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