来自:http://blog.csdn.net/u012581604/article/details/77233011
概念
基于组件的开发(Component-Based Development,简称CBD)是一种软件开发范型。它是现今软件复用理论实用化的研究热点,在组件对象模型的支持下,通过复用已有的构件,软件开发者可以“即插即用”地快速构造应用软件。
优点
框架描述
之前有这么一个有趣的笑话。说一个人一大早起来想吃火锅,但是他又不想出门,于是他想了个主意,他给A打电话说:今天请大家吃火锅,别的东西都有了,就差一份羊肉了,来的时候带着。完了给B打电话说:今天请大家吃火锅,别的东西都有的,火锅料忘了买了,来的时候捎上……,他用这样的方法将所有的菜凑够,足不出户,就能吃火锅,而且想吃啥就吃啥。
这个例子中,这个在家里想吃火锅并且挨个给大家打电话的人便是插件式框架中的总框架,本身不提供任何的功能,角色就是总指挥。而小A,小B这些朋友则是各个组件,自己只负责自己的部分,但是每一个组件都无法单独执行,只能在总框架中执行。组件为整个开发提供基本的功能,组件之间的通信也都是通过总框架来实现的,这就是整个插件式框架。
实现
相信点开看这篇文章的都是有一定Python基础并且遇到类似于插件式开发需求,从而来看一份有用的代码,再将其拷贝走的。话不多说,上代码吧。
目录结构
++PluginFrame
– main.py
– PluginManager.py
++ Plugins
-- Plugin1.py
-- Plugin2.py
-- Plugin3.py
-- Plugin4.py
import os
import sys
from imp import find_module
from imp import load_module
class PluginManager(type):
__PluginPath = 'Plugins'
def __init__(self,name,bases,dict):
if not hasattr(self,'AllPlugins'):
self.__AllPlugins = {}
else:
self.RegisterAllPlugin(self)
@staticmethod
def SetPluginPath(path):
if os.path.isdir(path):
PluginManager.__PluginPath = path
else:
print '%s is not a valid path' % path
@staticmethod
def LoadAllPlugin():
pluginPath = PluginManager.__PluginPath
if not os.path.isdir(pluginPath):
raise EnvironmentError,'%s is not a directory' % pluginPath
items = os.listdir(pluginPath)
for item in items:
if os.path.isdir(os.path.join(pluginPath,item)):
PluginManager.__PluginPath = os.path.join(pluginPath,item)
PluginManager.LoadAllPlugin()
else:
if item.endswith('.py') and item != '__init__.py':
moduleName = item[:-3]
if moduleName not in sys.modules:
fileHandle, filePath,dect = find_module(moduleName,[pluginPath])
try:
moduleObj = load_module(moduleName,fileHandle,filePath,dect)
finally:
if fileHandle : fileHandle.close()
@property
def AllPlugins(self):
return self.__AllPlugins
def RegisterAllPlugin(self,aPlugin):
pluginName = '.'.join([aPlugin.__module__,aPlugin.__name__])
pluginObj = aPlugin()
self.__AllPlugins[pluginName] = pluginObj
def UnregisterPlugin(self,pLuginName):
if pluginName in self.__AllPlugins:
pluginObj = self.__AllPlugins[pluginName]
del pluginObj
def GetPluginObject(self, pluginName = None):
if pluginName is None:
return self.__AllPlugins.values()
else:
result = self.__AllPlugins[pluginName] if pluginName in self.__AllPlugins else None
return result
@staticmethod
def GetPluginByName(pluginName):
if pluginName is None:
return None
else:
for SingleModel in __ALLMODEL__:
plugin = SingleModel.GetPluginObject(pluginName)
if plugin:
return plugin
class Model_Component(object):
__metaclass__ = PluginManager
def Start(self):
print 'Please write the Start() function'
def ChangeLanguage(self,language):
print 'Please write the ChangeLanguage() function'
class Model_MenuObj(object):
__metaclass__ = PluginManager
def Start(self):
print 'Please write the Start() function'
def ChangeLanguage(self,language):
print 'Please write the ChangeLanguage() function'
class Model_ToolBarObj(object):
__metaclass__ = PluginManager
def Start(self):
print 'Please write the Start() function'
def ChangeLanguage(self,language):
print 'Please write the ChangeLanguage() function'
class Model_ParamPanelObj(object):
__metaclass__ = PluginManager
def Start(self):
print 'Please write the Start() function'
def ChangeLanguage(self,language):
print 'Please write the ChangeLanguage() function'
__ALLMODEL__ = (Model_ParamPanelObj,Model_ToolBarObj,Model_MenuObj,Model_Component)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
from PluginManager import Model_MenuObj
class Plugin1(Model_MenuObj):
def __init__(self):
pass
def Start(self):
print "I am plugin1 , I am a menu!"
from PluginManager import Model_ToolBarObj
class Plugin2(Model_ToolBarObj):
def __init__(self):
pass
def Start(self):
print "I am plugin2 , I am a ToolBar!"
from PluginManager import Model_ParamPanelObj
class Plugin3(Model_ParamPanelObj):
def __init__(self):
pass
def Start(self):
print "I am plugin3 , I am a ParamPanel!"
from PluginManager import Model_Component
class Plugin4(Model_Component):
def __init__(self):
pass
def Start(self):
print "I am plugin4 , I am a Component!"
import sys
from PluginManager import PluginManager
from PluginManager import __ALLMODEL__
if __name__ == '__main__':
PluginManager.LoadAllPlugin()
for SingleModel in __ALLMODEL__:
plugins = SingleModel.GetPluginObject()
for item in plugins:
item.Start()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
输出
I am plugin3 , I am a ParamPanel!
I am plugin2 , I am a ToolBar!
I am plugin1 , I am a menu!
I am plugin4 , I am a Component!