Python设计模式-抽象工厂
Posted 逆向行驶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python设计模式-抽象工厂相关的知识,希望对你有一定的参考价值。
# coding=utf-8
这种方式反倒把事情做复杂了 可取之处在于有了更高层次的抽象
class IEmployee:
def insert_employee(self):
pass
class ICompany:
def insert_company(self):
pass
# mysql版本的数据库访问
class EmployeeMysql(IEmployee):
def insert_employee(self):
print "插入员工mysql数据库"
class CompanyMysql(ICompany):
def insert_company(self):
print "插入公司mysql数据库"
class EmployeeMSSQL(IEmployee):
def insert_employee(self):
print "插入员工sqlserver数据库"
class CompanyMSSQL(ICompany):
def insert_company(self):
print "插入公司sqlserver数据库"
class IFactory:
def insert_employee(self):
pass
def insert_company(self):
pass
class FactoryMySQL(IFactory):
def insert_employee(self):
temp = EmployeeMysql()
return temp
def insert_company(self):
temp = CompanyMysql()
return temp
class FactoryMSSQL(IFactory):
def insert_employee(self):
temp = EmployeeMSSQL()
return temp
def insert_company(self):
temp = CompanyMSSQL()
return temp
if __name__ == "__main__":
factory = FactoryMSSQL()
employee = factory.insert_employee()
insert_company = factory.insert_company()
employee.insert_employee()
insert_company.insert_company()
以上是关于Python设计模式-抽象工厂的主要内容,如果未能解决你的问题,请参考以下文章