zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    模式说明

    抽象工厂模式提供一个接口,用于创建相关或者依赖对象的家族,而不需要明确指定具体类。

    抽象工厂允许客户端使用抽象的接口来创建一组相关的产品,而不需要关系实际产出的具体产品是什么。这样一来,客户就可以从具体的产品中被解耦。

    和工厂方法主要区别于,抽象工厂内要像像定义中说的一样,‘创建一组相关的产品’。

    感觉像是(不知道这样理解对否):简单工厂是一个工厂生产多个产品;工厂方法是拆分成子工厂,分别生产各自产品;抽象工厂整合工厂方法和简单工厂,随着子工厂规模变大,也可以生产多个类似产品。

    模式结构图

    程序示例

    说明:

    一个工资类,两个派生类(国内工资和国外工资);

    一个社保类,两个派生类(国内社保和国外社保);

    一个工厂类,两个派生类(分别生产国内工资、社保和国外工资、社保)

    代码:

     1 class ISalary(object):
     2     _tax = 0
     3     _salary = 0
     4     _bonus = 0
     5     def __init__(self,salary,bonus,tax):
     6         self._bonus=bonus
     7         self._salary=salary
     8         self._tax=tax
     9 
    10     def GetTax(self):
    11         return self._tax
    12     def GetBonus(self):
    13         return self._bonus
    14     def GetSalary(self):
    15         return self._salary
    16     def CalcuteTax(self):
    17         pass
    18 
    19 class ChineseSalary(ISalary):
    20     def CalcuteTax(self):
    21         return (self.GetSalary()+self.GetBonus()-3500)*self.GetTax()
    22     
    23 class ForeignSalary(ISalary):
    24     def CalcuteTax(self):
    25         return (self.GetSalary()+self.GetBonus()-4000)*self.GetTax()
    26 
    27 class ISocialSecurity(object):
    28     def __init__(self):
    29         self._social = 0
    30     def getsocial(self):
    31         return self._social
    32 
    33 class ChineseSocialSecurity(ISocialSecurity):
    34     def __init__(self):
    35         self._social = 2000
    36 
    37 class ForeignSocialSecurity(ISocialSecurity):
    38     def __init__(self):
    39         self._social = 2200
    40 
    41 class IFactory(object):
    42     def CreateSalary(self,salary,bonus,tax):
    43         return ISalary(salary,bonus,tax)
    44     def CreateSocialSecurity(self):
    45         return ISocialSecurity()
    46 
    47 class ChineseFactory(IFactory):
    48     def CreateSalary(self,salary,bonus,tax):
    49         return ChineseSalary(salary,bonus,tax)
    50     def CreateSocialSecurity(self):
    51         return ChineseSocialSecurity()
    52 
    53 class ForeignFactory(IFactory):
    54     def CreateSalary(self,salary,bonus,tax):
    55         return ForeignSalary(salary,bonus,tax)
    56     def CreateSocialSecurity(self):
    57         return ForeignSocialSecurity()
    58 
    59 if __name__=='__main__':
    60     chFac = ChineseFactory()
    61     salary = chFac.CreateSalary(10000,8000,0.2)
    62     social = chFac.CreateSocialSecurity()
    63     print 'chinese tax :'+ str(salary.CalcuteTax())
    64     print 'chinese social security:'+ str(social.getsocial())
    65 
    66     forFac = ForeignFactory()
    67     salary = forFac.CreateSalary(10000,8000,0.2)
    68     social = forFac.CreateSocialSecurity()
    69     print 'foreigner tax :'+ str(salary.CalcuteTax())
    70     print 'foreigner social security:'+ str(social.getsocial())

    运行结果:

    参考来源:

    http://www.cnblogs.com/chenssy/p/3679190.html

    http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html

    http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html

  • 相关阅读:
    hdu 2680:Choose the best route(Dijkstra , SPFA)
    Gym
    Gym
    Gym
    常用的相似度计算
    kafka 配置
    Kafka-broker配置说明
    kafka 安装以及测试
    adaboost算法
    solr scheme配置简介
  • 原文地址:https://www.cnblogs.com/cotton/p/3930754.html
Copyright © 2011-2022 走看看