简单工厂模式

 1 public class Operation
 2     {
 3         private double _numberA = 0;
 4         private double _numberB = 0;
 5 
 6         public double NumberA
 7         {
 8             get { return _numberA; }
 9             set { _numberA = value; }
10         }
11 
12         public double NumberB
13         {
14             get { return _numberB; }
15             set { _numberB = value; }
16         }
17 
18         public virtual double GetResult()
19         {
20             double result = 0;
21             return result;
22         }
23     }
24 
25     class OperationAdd : Operation
26     {
27         public override double GetResult()
28         {
29             double result = 0;
30             result = NumberA + NumberB;
31 
32             return result;
33         }
34     }
35 
36     class OperationSub : Operation
37     {
38         public override double GetResult()
39         {
40             double result = 0;
41             result = NumberA - NumberB;
42 
43             return result;
44         }
45     }
46 
47     class OperationMul : Operation
48     {
49         public override double GetResult()
50         {
51             double result = 0;
52             result = NumberA * NumberB;
53 
54             return result;
55         }
56     }
57 
58     class OperationDiv : Operation
59     {
60         public override double GetResult()
61         {
62             double result = 0;
63             if (Math.Abs(0 - NumberB) < 0.000001)
64             {
65                 throw new Exception("除数不能为0");
66             }
67 
68             result = NumberA / NumberB;
69 
70             return result;
71         }
72     }
View Code
 1 public class OperationFactory
 2     {
 3         public static Operation createOperate(string operate)
 4         {
 5             Operation oper = null;
 6 
 7             switch (operate)
 8             {
 9                 case "+":
10                     oper = new OperationAdd();
11                     break;
12                 case "-":
13                     oper = new OperationSub();
14                     break;
15                 case "*":
16                     oper = new OperationMul();
17                     break;
18                 case "/":
19                     oper = new OperationDiv();
20                     break;
21             }
22 
23             return oper;
24         }
25     }
View Code

转自《大话设计模式》

转载于:https://www.cnblogs.com/yixiu868/p/6551648.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python简单工厂模式是一种创建型设计模式,它提供了一种创建对象的方式,而无需直接暴露对象的创建逻辑。简单工厂模式通过一个工厂类来封装对象的创建过程,客户端只需要通过工厂类来获取所需的对象,而无需关心对象的具体创建细节。 在Python中,实现简单工厂模式通常包括以下几个步骤: 1. 定义一个抽象基类或接口,用于表示所要创建的对象的共同特征。 2. 创建具体的产品类,它们实现了抽象基类或接口,并提供了具体的功能实现。 3. 创建一个工厂类,该类包含一个静态方法或类方法,用于根据客户端的需求创建具体的产品对象。 4. 客户端通过调用工厂类的方法来获取所需的产品对象。 下面是一个简单的Python简单工厂模式的示例: ```python from abc import ABC, abstractmethod # 定义抽象基类 class Product(ABC): @abstractmethod def operation(self): pass # 具体产品类A class ConcreteProductA(Product): def operation(self): return "ConcreteProductA operation" # 具体产品类B class ConcreteProductB(Product): def operation(self): return "ConcreteProductB operation" # 工厂类 class SimpleFactory: @staticmethod def create_product(product_type): if product_type == "A": return ConcreteProductA() elif product_type == "B": return ConcreteProductB() else: raise ValueError("Invalid product type") # 客户端代码 product_a = SimpleFactory.create_product("A") print(product_a.operation()) # 输出:ConcreteProductA operation product_b = SimpleFactory.create_product("B") print(product_b.operation()) # 输出:ConcreteProductB operation ``` 在上述示例中,抽象基类`Product`定义了产品的共同特征,具体产品类`ConcreteProductA`和`ConcreteProductB`分别实现了抽象基类,并提供了具体的功能实现。工厂类`SimpleFactory`包含一个静态方法`create_product`,根据客户端传入的产品类型来创建具体的产品对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值