Abstract Factory Model: Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
Advantages: Easy to switch "product series", you only need to change the corresponding factory.
Disadvantages: It is complicated to create a product. You need to add and modify many things.
Optimization 1: To avoid too many logic judgments on the client, a simple factory class can be encapsulated to generate product classes.
Optimization 2: To reduce the logical judgment in a simple factory class, you can useReflectionRead the information of the product category based on the external configuration file.
# Encoding = UTF-8 ## by Panda # Abstract Factory mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # Abstract product A: User table class iuser (): def insert (Self): Pass def getuser (Self ): pass # sqlserver implements userclass sqlserveruser (iuser): def insert (Self): printinfo ("add a record to the user table in SQL Server") def getuser (Self ): printinfo ("get a record of the User table in SQL Server") # userclass accessuser (iuser): def insert (Self) implemented by access ): printinfo ("adding a record to the user table in access") def getuser (Self): printinfo ("getting a user table in access") # Abstract product B: department table class idepartment (): def insert (Self): Pass def getuser (Self): pass # departmentclass sqlserverdepartment (iuser): def insert (Self) implemented by sqlserver ): printinfo ("add a record to the Department table in SQL Server") def getuser (Self): printinfo ("get a record of the Department table in SQL Server ") # departmentclass accessdepartment (iuser): def insert (Self): printinfo ("add a record to the Department table in access") def getuser (Self ): printinfo ("getting a department table record in access") # Abstract Factory class ifacloud (): def createuser (Self): Pass def createdepartment (Self ): pass # SQL Server factory class sqlserverfactory (ifacloud): def createuser (Self): Return sqlserveruser () def createdepartment (Self): Return sqlserverdepartment () # access factory class accessfactory (ifacloud ): def createuser (Self): Return accessuser () def createdepartment (Self): Return accessdepartment () # optimization 1: A simple factory class is used to encapsulate the logical judgment operation class dataaccess (): # DB = "sqlserver" DB = "access" @ staticmethod def createuser (): If (dataaccess. DB = "sqlserver"): Return sqlserveruser () Elif (dataaccess. DB = "access"): Return accessuser () @ staticmethod def createdepartment (): If (dataaccess. DB = "sqlserver"): Return sqlserverdepartment () Elif (dataaccess. DB = "access"): Return accessdepartment () # optimization 2: reflection mechanism, avoid using too many judgments # The following information can be obtained from the configuration file dbtype = 'sqlserver '# 'access' dbtab _ User = 'user' dbtab _ Department = 'department' class dataaccesspro (): # DB = "sqlserver" DB = "access" @ staticmethod def createuser (): funname = dbtype + dbtab_user return eval (funname )() # eval converts the string to the python expression @ staticmethod def createdepartment (): funname = dbtype + dbtab_department return eval (funname) () def clientui (): printinfo ("\ n -------- Abstract Factory method --------") Factory = sqlserverfactory () IU = factory. createuser () IU. insert () IU. getuser () id = factory. createdepartment () ID. insert () ID. getuser () printinfo ("\ n -- Abstract Factory method + simple factory method --") IU = dataaccess. createuser () IU. insert () IU. getuser () id = dataaccess. createdepartment () ID. insert () ID. getuser () printinfo ("\ n-Abstract Factory method + simple factory method + reflection-") IU = dataaccesspro. createuser () IU. insert () IU. getuser () id = dataaccesspro. createdepartment () ID. insert () ID. getuser () returnif _ name _ = '_ main _': clientui ();
Class diagram: