【设计模式系列】简单工厂类——计算器设计实例

/*

作者:五岳 
出处:http://www.cnblogs.com/wuyuegb2312 
对于标题未标注为“转载”的文章均为原创,其版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

*/




模式特点:工厂根据条件产生不同功能的类。

程序实例:四则运算计算器,根据用户的输入产生相应的运算类,用这个运算类处理具体的运算。

代码特点:C/C++中的switch...case...分支使用字典的方式代替。

     使用异常机制对除数为0的情况进行处理。


代码

简单工厂模式

class Operation:
    def GetResult(self):
        pass

class OperationAdd(Operation):
    def GetResult(self):
        return self.op1+self.op2


class OperationSub(Operation):
    def GetResult(self):
        return self.op1-self.op2


class OperationMul(Operation):
    def GetResult(self):
        return self.op1*self.op2


class OperationDiv(Operation):
    def GetResult(self):
        try:
            result = self.op1/self.op2
            return result
        except:
            print "error:divided by zero."
            return 0

class OperationUndef(Operation):
    def GetResult(self):
        print "Undefine operation."
        return 0

class OperationFactory:
    operation = {}
    operation["+"] = OperationAdd();
    operation["-"] = OperationSub();
    operation["*"] = OperationMul();
    operation["/"] = OperationDiv();
    def createOperation(self,ch):        
        if ch in self.operation:
            op = self.operation[ch]
        else:
            op = OperationUndef()
        return op

if __name__ == "__main__":
    op = raw_input("operator: ")
    opa = input("a: ")
    opb = input("b: ")
    factory = OperationFactory()
    cal = factory.createOperation(op)
    cal.op1 = opa
    cal.op2 = opb
    print cal.GetResult()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下面向对象程序设计简单工厂模式的应用。 简单工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。该模式允许客户端通过传递类型参数来请求工厂创建所需类型的对象。 在一个简易计算器中,我们可以使用简单工厂模式来创建不同类型的运算对象。首先,我们需要定义一个运算接口,例如: ```java public interface Operation { double getResult(double numberA, double numberB); } ``` 然后,我们可以定义不同类型的运算类,例如: ```java public class AddOperation implements Operation { public double getResult(double numberA, double numberB) { return numberA + numberB; } } public class SubtractOperation implements Operation { public double getResult(double numberA, double numberB) { return numberA - numberB; } } public class MultiplyOperation implements Operation { public double getResult(double numberA, double numberB) { return numberA * numberB; } } public class DivideOperation implements Operation { public double getResult(double numberA, double numberB) { if (numberB == 0) { throw new IllegalArgumentException("除数不能为0"); } return numberA / numberB; } } ``` 最后,我们可以定义一个简单工厂类,根据传入的运算类型参数来创建相应的对象: ```java public class OperationFactory { public static Operation createOperation(String operationType) { Operation operation = null; switch (operationType) { case "+": operation = new AddOperation(); break; case "-": operation = new SubtractOperation(); break; case "*": operation = new MultiplyOperation(); break; case "/": operation = new DivideOperation(); break; default: throw new IllegalArgumentException("不支持的运算类型"); } return operation; } } ``` 使用简单工厂模式,我们可以在客户端代码中通过传递运算类型参数来获取相应的运算对象,例如: ```java public static void main(String[] args) { Operation operation = OperationFactory.createOperation("+"); double result = operation.getResult(3, 4); System.out.println(result); // 输出 7.0 } ``` 这样,我们就可以灵活地创建不同类型的运算对象,实现了代码的可扩展性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值