zoukankan      html  css  js  c++  java
  • 抽象工厂(Abstract Factory)

    模式定义

    提供一个接口, 让该接口负责创建一系列‘相关或者相互依赖的对象’ , 无需指定他们具体的类

    要点总结

    如果没有应对‘多系列对象构建’的需求变化 ,则没有必要使用Abstract Factory模式,这个时候使用简单的工厂完全可以

    ‘系列对象’值的事在某一特定系列下的队形之间有相互依赖,或者作用的关系。不同系列的对象之间不能相互依赖。

    Abstract Factory 模式主要在于应对‘新系列’的需求变动。其缺点在于难以应对‘新对象’的需求变动

    """
    设计模式——抽象工厂模式
    抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类
    """
    import sys
    
    #抽象用户表类
    class User(object):
    
        def get_user(self):
            pass
    
        def insert_user(self):
            pass
    
    #抽象部门表类
    class Department(object):
    
        def get_department(self):
            pass
    
        def insert_department(self):
            pass
    
    
    #操作具体User数据库类-Mysql
    class MysqlUser(User):
    
        def get_user(self):
            print 'MysqlUser get User'
    
        def insert_user(self):
            print 'MysqlUser insert User'
    
    #操作具体Department数据库类-Mysql
    class MysqlDepartment(Department):
    
        def get_department(self):
            print 'MysqlDepartment get department'
    
        def insert_department(self):
            print 'MysqlDepartment insert department'
    
    #操作具体User数据库-Orcal
    class OrcaleUser(User):
    
        def get_user(self):
            print 'OrcalUser get User'
    
        def insert_user(self):
            print 'OrcalUser insert User'
    
    #操作具体Department数据库类-Orcal
    class OrcaleDepartment(Department):
    
        def get_department(self):
            print 'OrcalDepartment get department'
    
        def insert_department(self):
            print 'OrcalDepartment insert department'
    
    #抽象工厂类
    class AbstractFactory(object):
    
        def create_user(self):
            pass
    
        def create_department(self):
            pass
    
    class MysqlFactory(AbstractFactory):
    
        def create_user(self):
            return MysqlUser()
    
        def create_department(self):
            return MysqlDepartment()
    
    class OrcaleFactory(AbstractFactory):
    
        def create_user(self):
            return OrcalUser()
    
        def create_department(self):
            return OrcalDepartment()
    
    if __name__ == "__main__":
    
        db = sys.argv[1]
        myfactory = ''
        if db == 'Mysql':
            myfactory = MysqlFactory()
        elif db == 'Orcal':
            myfactory = OrcaleFactory()
        else:
            print "不支持的数据库类型"
            exit(0)
        user = myfactory.create_user()
        department = myfactory.create_department()
        user.insert_user()
        user.get_user()
        department.insert_department()
        department.get_department()
    

      

  • 相关阅读:
    未能从程序集 C:Program Files (x86)MSBuild14.0inMicrosoft.Data.Entity.Build.Tasks.dll 加载任务“EntityClean”
    asp.net mvc 4 项目升级到 asp.net mvc5
    SQL Server查看所有表大小,所占空间
    0x80072f8a未指定的错误
    vs2012 aps.net4.0/4.5尚未在web服务器上注册
    vsphere 出现“在主机的当前连接状况下不允许执行该操作”
    sql server 发布时提示'dbo.sysmergepublications'无效的解决办法
    sql server更改机器名后更改数据库机器名
    Ajax向后台传入File类型参数
    上传下载Azure Blob里的Excel文件。
  • 原文地址:https://www.cnblogs.com/Dreamxin/p/9941950.html
Copyright © 2011-2022 走看看