大约工厂,设计方式

ca88会员登录中心

慢慢的做笔录,做最强劲的温馨

  轻巧工厂情势(Factory
Pattern)是1种创立型的设计情势,像工厂同样基于要求生育目的实例。

  简单工厂方式(Factory
Pattern)是一种创立型的设计格局,像工厂同样遵照要求生产目标实例。

看了牛皮设计情势之后感触很深,开掘自个儿还有为数不少上学的事物,设计软件并不是壹两句代码把职能写完了就行,需求观念的剧情有大多

  特点:依照差别的口径,工厂实例化出确切的对象。

  特点:依照不相同的基准,工厂实例化出格外的对象。

代码来源参考大话设计格局这本书,这里在博客里记录一下,不容许每回都去翻书,可是在博客里面是老大好找的。

  《大话设计形式》中实例:四则运算总括器

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

榜样为二个回顾工厂形式的总计器,首先先创设一个总计器运算所要求选拔的数据模型,在java之中也叫bean,就是二个空洞的类,这里大家先创制1个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 
 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

  • 成立实体类

    class Entity(): #实体

    def Func(self):
        "方法体"
    

         #pass

  • 始建变体类

    class Variant1(Entity):

    def Func(self):
    

         pass

 

class Variant2(Entity):
    def Func(self):
     pass
class Variant2(Entity):
    def Func(self):
     pass

通过多态,再次来到父类的措施,然后达成总计结果

 

 

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();
        }
  • 成立工厂类

    class EntityFactory():

    def create_variant(self,choice):
        #根据choice创建不同的variant类
        #函数体
        #return
    
  • 始建筑工程厂类

    class EntityFactory():

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

然则简单工厂方式会存在3个主题材料,后续如若新添方法的话那么还索要延续开创运算类,然后实例化对象,那么如此会追加代码结构的复杂度,当然依旧有艺术化解的,可是此地就只做轻巧工厂形式的牵线了,

 

 

三层模型基本上只要不是用了专门的办法那么基本都以依靠轻便工厂格局来拍卖措施的

  使用工厂类:

  使用工厂类:

  

  

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

 

 

发表评论

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

网站地图xml地图