############ User表 ################## class User(object): def insert(self): pass def select(self): pass class MysqlUser(User): def insert(self): print("MySQL: 给User表增加一条记录") def select(self): print("MySQL: 根据ID查询User表的记录") class OracleUser(User): def insert(self): print("Oracle: 给User表增加一条记录") def select(self): print("Oracle: 根据ID查询User表的记录") ############## Department表 #################### class Department(object): def insert(self): pass def select(self): pass class MysqlDepartment(Department): def insert(self): print("MySQL: 给Department表增加一条记录") def select(self): print("MySQL: 根据ID查询Department表的记录") class OracleDepartment(Department): def insert(self): print("Oracle: 给Department表增加一条记录") def select(self): print("Oracle: 根据ID查询Department表的记录") ########## 抽象工厂类 ################ class Factory: def create_user(self): pass def create_department(self): pass class MysqlFactory(Factory): def create_user(self): return MysqlUser() def create_department(self): return MysqlDepartment() class OracleFactory(Factory): def create_user(self): return OracleUser() def create_department(self): return OracleDepartment() if __name__ == '__main__': factory = OracleFactory() OracleFactory user = factory.create_user() user.insert() user.select() department = factory.create_department() department.insert() department.select() 抽象工厂