python 工厂模式
# coding = utf-8
class VerifyModeBase():
def verify(self):
pass
class CodeVerifyMode(VerifyModeBase):
def verify(self):
print "code"
pass
class FullMatchVerifyMode(VerifyModeBase):
def verify(self):
print "full"
pass
class JsonPathVerifyMode(VerifyModeBase):
def verify(self):
print "json"
pass
class VerifyModeFactory(object):
# Create based on class name:
@staticmethod
def factory(type):
#return eval(type + "()")
if type == "Code":
return CodeVerifyMode()
elif type == "JsonPath":
return JsonPathVerifyMode()
else:
return FullMatchVerifyMode()
aaa = VerifyModeFactory.factory("Code")
print aaa.verify()
aaa = VerifyModeFactory.factory("JsonPath")
print aaa.verify()
aaa = VerifyModeFactory.factory("eee")
print aaa.verify()