#!/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...

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐