我实现了一个可插拔的框架,如下所示。
它有效,但我对Plugin2的实现感到困惑。 似乎它使用普通方法而不是类构造函数。
我可以这样做吗? 有没有缺点?
这是一种正常使用的“模式”吗? 如果是,那么模式名称是什么? '打字'? 我应该避免这样使用吗?
更新:我关注的是以下方法:
def addPlugin(name, plugin)现在参数插件可以是Class或Method。 然后它对插件开发者来说有点模糊。 这是动态编程语言世界的常见情况吗?
class MyFramework(object): _plugins = {} _osType = None @staticmethod def addPlugin(name, plugin): MyFramework._plugins[name]= plugin def __init__(self, osType): self._osType = osType for name, plugin in MyFramework._plugins.items(): setattr(self, name, plugin(self, self._osType)) class Plugin1(object): def __init__(self, owner, osType): self.owner= owner self.osType = osType def hello(self): print 'this is plugin1' def Plugin2(owner, osType): if (osType == "Linux"): return Plugin2Linux(owner) else: return Plugin2Common(owner) class Plugin2Linux(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 Linux version' class Plugin2Common(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 common version' MyFramework.addPlugin("plugin1", Plugin1) MyFramework.addPlugin("plugin2", Plugin2) framework = MyFramework("Linux") plugin1 = getattr(framework, "plugin1") plugin2 = getattr(framework, "plugin2") plugin1.hello() plugin2.hello()I implemented a plugable framework like below.
It works but I'm confused about the implementation of Plugin2. Seems it uses a normal method instead of a class constructor.
Can I do so? Is there any cons?
Is it a normal-used 'pattern'? If it is, what's the pattern name? 'Duck typing'? Should I avoid usage like this?
UPDATE: My concern is for below method:
def addPlugin(name, plugin)Now parameter plugin could be either a Class or a Method. Then it is kind of obscure for the plugin developer. Is this a common case in dynamic programming language world?
class MyFramework(object): _plugins = {} _osType = None @staticmethod def addPlugin(name, plugin): MyFramework._plugins[name]= plugin def __init__(self, osType): self._osType = osType for name, plugin in MyFramework._plugins.items(): setattr(self, name, plugin(self, self._osType)) class Plugin1(object): def __init__(self, owner, osType): self.owner= owner self.osType = osType def hello(self): print 'this is plugin1' def Plugin2(owner, osType): if (osType == "Linux"): return Plugin2Linux(owner) else: return Plugin2Common(owner) class Plugin2Linux(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 Linux version' class Plugin2Common(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 common version' MyFramework.addPlugin("plugin1", Plugin1) MyFramework.addPlugin("plugin2", Plugin2) framework = MyFramework("Linux") plugin1 = getattr(framework, "plugin1") plugin2 = getattr(framework, "plugin2") plugin1.hello() plugin2.hello()最满意答案
这是一种常用的模式,称为工厂功能 。 将接口打开到任何返回对象的callable是一个好主意,无论callable是函数还是类。 它可以让您灵活处理将来想做的事情。
This is an often-used pattern called a factory function. It's a good idea to have your interface open to any callable that returns an object, whether the callable is a function or a class. It keeps you flexible for whatever you want to do in the future.
更多推荐
发布评论