工厂设计模式----python版本

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#工厂设计模式
'''
date:2016/8/21
'''

#形状接口
class Shape(object):
   def __init__(self):
      pass
   def draw(self):
      pass
#长方形
class Retangle(Shape):
   def draw(self):
      print "Retangle..."
      return "Retangle..."
#正方形
class Square(Shape):
   def draw(self):
      print "Square..."
      return "Square..."

#获取形状的工厂
class ShapeFactory():
   def getShape(self,name):
      if name == None:
         return None
      elif "Retangle" in name:
         return Retangle()
      elif "Square" in name:
         return Square()
      else:
         return None

if __name__ == '__main__':
   #1.获取工厂
   ShapeFactory=ShapeFactory()
   #2.从工厂中获取对象
   shape=ShapeFactory.getShape("Square")
   #3.运行方法
   shape.draw()

#运行结果:Square...

你可能感兴趣的:(杂类,python,设计模式)