Python-如何实现桥接(或适配器)设计模式?

Python-如何实现桥接(或适配器)设计模式?,python,design-patterns,Python,Design Patterns,我正在努力用Python实现桥接设计模式(或适配器之类的替代模式) 我希望能够编写这样的代码,根据提供的URL转储数据库模式: urls = ['sqlite://c:\\temp\\test.db', 'oracle://user:password@tns_name']; for url in urls: db = Database(url); schema = db.schema() 我把类定义为 class Database(): def __init__(sel

我正在努力用Python实现桥接设计模式(或适配器之类的替代模式)

我希望能够编写这样的代码,根据提供的URL转储数据库模式:

urls = ['sqlite://c:\\temp\\test.db', 'oracle://user:password@tns_name'];
for url in urls:
    db = Database(url);
    schema = db.schema()
我把类定义为

class Database():
    def __init__(self, url):
        self.db_type = string.split(self.url, "://")[0]

class Oracle():
    def schema(self):
        # Code to return Oracle schema

class SQLite():
    def schema(self):
        # Code to return SQLite schema
如何将这3个类“粘合”在一起,以便正确执行第一个代码块?我在谷歌上搜索了一下,但一定是过了一天,因为它在我的脑海里没有出现


提前感谢

改用工厂模式:

class Oracle(object):
  ...

class SQLite(object):
  ...

dbkind = dict(sqlite=SQLite, oracle=Oracle)

def Database(url):
  db_type, rest = string.split(self.url, "://", 1)
  return dbkind[db_type](rest)

欢迎您,Dave,但是在这种情况下,为什么不接受答案呢?使用dict(sqlite=sqlite)比使用{'sqlite':sqlite}有什么优势吗?还是仅仅是风格的问题?@statictype,严格来说是风格的问题:我非常喜欢可读的类型名(dict,list),而不是基于标点符号的替代品(另一个例子:用
list(thewidgets)
复制列表,而不是用
thewidgets[:]
)。如果您曾经在电话中与客户机讨论过Perl代码,您可能会更好地理解我的来历;-)。