Abstract Factory mode (Python version)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.