首页 > 其他 > 详细

工厂设计模式

时间:2016-10-31 22:04:59      阅读:141      评论:0      收藏:0      [点我收藏+]

技术分享

 

 

这个和简单工厂有区别,简单工厂模式只有一个工厂,工厂方法模式对每一个产品都有相应的工厂

  好处:增加一个运算类(例如N次方类),只需要增加运算类和相对应的工厂,两个类,不需要修改工厂类。

  缺点:增加运算类,会修改客户端代码,工厂方法只是把简单工厂的内部逻辑判断移到了客户端进行。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Operation(object):
    """
    抽象类
    """
def GetResult(self): raise NotImplemented
class Add(Operation): """ """
def GetResult(self, numa, numb): return numa + numb

class OperationFactory(object): """ 工厂类 """
def CreateOperation(self): raise NotImplemented
class AddFactory(OperationFactory): def CreateOperation(self): return Add()
if __name__ == __main__: obj = AddFactory() ret = obj.CreateOperation() result = ret.GetResult(2,5) print(result)

 

工厂设计模式

原文:http://www.cnblogs.com/renfanzi/p/6017211.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!