测者的测试技术手册:Python的抽象工厂类

217 阅读1分钟

抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。属于对象创建型模式。

#!/usr/bin/env python # -*- coding: utf-8 -*-  #__from__  = ' abstract_factory '   
#__author__  = ' Mr.Tester CrissChan '   
#__instruction__  =  '抽象工厂模式,打印纸例子'   
class AbstraceFactroy  ( object ) :     def getSize ( self ) :          return  Size()    def getColor ( self ):           return  Colour() class Size ( AbstraceFactroy ) :      @ staticmethod      def sizeFactory (type):              if type == ' A4 ' :               return A4()         if type == ' A3 ' :            return A3() class A4 ( Size ) :      def __init__ ( self ) :          print ' A4 \'s size : 210 mm × 297 mm ' class A3 ( Size ) :      def  __init__ ( self ) :          print  ' A3 \'s size :  297 mm×420 mm‘  class Colour ( AbstraceFactroy ) :      @ staticmethod      def colorFactory ( type ) :          if type == 'White' :               return White( )          if type == ' Color ' :              return  Color()  class White ( Colour ) :      def __init__ ( self ) :              print  ' This  is a white paper'  class Color ( Colour ) :     def __init__ ( self ) :              print ' This is a color paper ' if __name__ ==  ' __main__ ' :             abstractFactory = AbstraceFactroy ()             types = Size.__subclasses__ ()      for type in types :         size = abstractFactory.getSize().sizeFactory( type.__name__ )         types = Colour.__subclasses__()  for type in types:          colour = abstractFactory.getColor().colorFactory( type.__name__ )