当需要大批量创建对象有统一的入口,易于代码维护
当发生修改时,仅修改工厂类即可
class Person:
pass
class Worker(Person):
print("I'm worker")
class Player(Person):
print("I'm player")
class teacher(Person):
print("I'm teacher")
class Factory:
def get_person(self,p_type):
if p_type='w':
return Worker()
elif p_type='p':
return Player()
else:
return Teacher()
factory=Factory()
person1=factory_person('w')
person2=factory_person('p')
person3=factory_person('t')