

技术领域technical field
本发明涉及构建耦合系统的方法。The present invention relates to methods of constructing coupled systems.
背景技术Background technique
现有的观察者模式是解决系统模型之间复杂逻辑关联一种行之有效的方式。简单地说,观察者模式定义了一个一对多的依赖关系,让一个或多个观察者监察一个主题对象。这样主题对象状态上的变化能够通知所有的依赖于此对象的那些观察者,使这些观察者能够自动更新。这里的主题和观察者都是系统的一类模型,作为观察者的模型在逻辑上依赖于作为主题的模型,要根据主题的状态变化而相应的改变自身。The existing observer pattern is an effective way to solve the complex logical association between system models. Simply put, the Observer pattern defines a one-to-many dependency where one or more observers observe a subject object. In this way, changes in the state of the subject object can notify all observers that depend on this object, so that these observers can be automatically updated. Both the subject and the observer here are a type of model of the system. The model as the observer is logically dependent on the model as the subject, and changes itself according to the state change of the subject.
观察者模式中最主要的行为是主题对观察者的通知行为。主题把所有的观察者的引用保存在一个列表里,每个主题都可以有任意数量的观察者。观察者定义并实现了更新(Update)方法。当主题的内容发生变化时,就会依次遍历它所保存的观察者列表,调用它们的更新方法。为方便下文叙述,称主题状态改变并需要通知过观察者的点为发布点,具体到系统程序中往往是在一个具体的函数代码片断里;称依次调用所有观察者更新的方法为主题发布行为。在传统观察者模式的实现中,主题发布行为是由主题对象在发布点进行的,目的是通知观察者其状态改变。The most important behavior in the observer pattern is the notification behavior of the subject to the observer. Subjects keep references to all observers in a list, and each subject can have any number of observers. Observers define and implement an Update method. When the subject's content changes, it traverses the list of observers it holds in turn and calls their update methods. For the convenience of the following description, the point where the subject state changes and needs to be notified to the observer is called the release point, and it is usually in a specific function code fragment in the system program; the method that calls all observers to update in sequence is called the theme release behavior . In the implementation of the traditional observer pattern, the topic publishing behavior is carried out by the topic object at the publishing point, and the purpose is to notify the observer of its state change.
在现有的一种应用中,主题对象(Subject)定义了增加(attach)、移除(detach)观察者(Observer)的方法;还定义了通知所有观察者的方法,即主题发布行为。主题发布行为的实现是依次遍历注册在该主题上的所有观察者,调用它们的更新方法。一般而言,当主题状态发生变化,并且该变化需要通知相应的观察者时,就需要在相应的代码片断(即发布点)中加入主题发布行为的代码。In an existing application, a subject object (Subject) defines methods for adding (attach) and removing (detach) observers (Observer); it also defines a method for notifying all observers, that is, subject publishing behavior. The implementation of the topic publishing behavior is to traverse all the observers registered on the topic in turn and call their update methods. Generally speaking, when the state of the topic changes and the change needs to be notified to the corresponding observer, it is necessary to add the code of the topic publishing behavior in the corresponding code fragment (ie, publishing point).
例如,假设某一主题的foo方法改变了主题状态,需要进行主动发布,这里foo方法是发布点,调用主题发布行为就是进行主题发布。For example, suppose the foo method of a certain topic changes the topic status and needs to be actively published. Here, the foo method is the publishing point, and calling the topic publishing behavior is the topic publishing.
观察者模式较好地解耦了复杂关联的直接相关性,有助于构建松耦合的复杂系统。但由上可知,观察者模式得以奏效的主要原因,是在主题状态改变时,由主题主动进行发布,即在引起主题状态发生变化的发布点处,进行了主题发布,即需要有相关的代码存在。在系统需求明确、变化不大的前提下,是可以满足的。但由于系统的复杂性,各模型间的关联表面上通常不明确,很难在设计或实现系统的最初阶段就完全确定哪些主题的状态改变需要进行监察,而某些模型一开始都未被当作要进行监察的主题对象。而随着系统的演进,各功能模块的细化,需进行监察的模型和需关注的状态才逐步明确。此时需要将普通对象改变为主题对象;需要在主题对象中添加新的发布点。按照观察者模式的实现要求,就需要对普通对象添加作为主题对象所需的额外代码信息(如保存观察者对象引用的列表等);在新出现的发布点,加入主题发布行为。The Observer pattern better decouples the direct dependencies of complex associations and helps to build loosely coupled complex systems. However, it can be seen from the above that the main reason why the observer mode works is that when the status of the topic changes, the topic is actively published, that is, the topic is published at the publishing point that causes the status of the topic to change, that is, the relevant code is required exist. Under the premise of clear system requirements and little change, it can be satisfied. However, due to the complexity of the system, the relationship between the various models is usually not clear on the surface. It is difficult to completely determine which subjects need to be monitored for state changes at the initial stage of system design or implementation, and some models are not considered at the beginning. as the subject matter to be monitored. With the evolution of the system and the refinement of each functional module, the models that need to be monitored and the states that need to be paid attention to are gradually clarified. At this point, the common object needs to be changed into a theme object; a new publishing point needs to be added to the theme object. According to the implementation requirements of the observer mode, it is necessary to add additional code information (such as saving the list of observer object references, etc.) to the common object as the subject object; add the subject publishing behavior at the new publishing point.
Bertrand Meyer在1988年提出著名的开放-封闭原则(The Open-ClosedPrinciple,简称OCP原则)。这一原则指出:软件系统实体(如,类、模块、函数等等)应该是可以扩展的,但是不可修改的。遵循开放-封闭原则设计出的模块具有两个主要的特征,它们是:1、“对于扩展是开放的”2、“对于更改是封闭的”。即修改软件系统时,应只需添加新的代码,而不必改动已经正常运行的代码。Bertrand Meyer proposed the famous Open-Closed Principle (The Open-Closed Principle, OCP principle for short) in 1988. This principle states that software system entities (eg, classes, modules, functions, etc.) should be extensible but not modifiable. Modules designed following the open-closed principle have two main characteristics, they are: 1, "open for extension" 2, "closed for change". That is, when modifying a software system, it should only need to add new codes without changing the codes that are already running normally.
传统观察者模式的实现要求主题具有主动发布其状态变化的能力。因此,被当作主题的模型除了需要实现自身的业务逻辑之外,还需要加入主题发布行为。随着系统的开发和维护,由于模型间的关联关系的改变,必将引起对已可正常运行代码的修改,从而导致重新进行对这部分代码的测试等工作,增加了软件成本。应该认识到,因用户的要求变化或软件系统施用环境的变化,而引起系统需求的变化是实际存在,并随着软件系统应用的日益广泛而更为频繁。能越快响应变化相应作出修改的软件系统,其生命力越强。OCP原则就是保证软件系统良好应变能力的基础。The implementation of the traditional observer pattern requires the subject to have the ability to actively publish its state changes. Therefore, in addition to implementing its own business logic, the model treated as a topic also needs to add topic publishing behavior. With the development and maintenance of the system, due to the change of the relationship between the models, it will inevitably cause the modification of the code that has been running normally, which will lead to the re-testing of this part of the code, which will increase the software cost. It should be recognized that changes in system requirements due to changes in user requirements or changes in the application environment of the software system do exist, and become more frequent as software systems become more widely used. The faster a software system can respond to changes and make corresponding modifications, the stronger its vitality. The OCP principle is the basis for ensuring good adaptability of software systems.
发明内容Contents of the invention
本发明所要解决的技术问题是为了克服现有采用观察者模式的耦合系统中,必须预先定义好发布点,加入对主题发布行为的调用,同时作为主题的对象需添加额外信息(如保存观察者对象引用列表等),实现主题发布行为,不能满足“开放-封闭原则”的技术问题。The technical problem to be solved by the present invention is to overcome that in the existing coupling system adopting the observer mode, the publishing point must be defined in advance, and the call to the topic publishing behavior must be added, and additional information (such as saving the observer) must be added to the object as the topic object reference list, etc.), to implement the topic release behavior, which cannot meet the technical problem of "open-closed principle".
实现本发明所要解决的技术问题而采取的技术方案如下,The technical scheme that realizes the technical problem to be solved by the present invention and takes is as follows,
根据功能,设定相应的接口;According to the function, set the corresponding interface;
设定具体类,实现不同的功能;Set specific classes to achieve different functions;
将相应的接口和具体类关联起来。Associate the corresponding interface with the concrete class.
进一步地,所述构建耦合系统的方法还包括以下步骤:Further, the method for constructing a coupling system also includes the following steps:
特定接口方法被调用时,相关联的发布行为类的标准方法被执行,进行主题状态变化的发布。When a specific interface method is called, the standard method of the associated publishing behavior class is executed to publish the change of the topic state.
进一步地,所述构建耦合系统的方法中将相应的接口和具体类关联起来的步骤采用动态代理的方式。Further, the step of associating corresponding interfaces with concrete classes in the method for constructing a coupled system adopts a dynamic agent.
进一步地,所述构建耦合系统的方法还包括以下步骤:Further, the method for constructing a coupling system also includes the following steps:
当需要改动主题对象和发布点时,先确定发布点,并编写发布行为类,然后利用工具类插入到所确定的接口方法中。When it is necessary to change the subject object and publishing point, first determine the publishing point, and write the publishing behavior class, and then use the tool class to insert into the determined interface method.
进一步地,所述构建耦合系统的方法通过工具类将相应的接口和具体类关联起来。Further, the method for constructing a coupling system associates corresponding interfaces with concrete classes through tool classes.
进一步地,所述构建耦合系统的方法在将相应的接口和具体类关联起来的步骤中,采用二元组图状数据结构型表记录接口与具体实现类的对应关系。进一步地,所述构建耦合系统的方法所述动态代理是一个在耦合系统运行期动态实现一组指定接口的特殊类。Further, in the step of associating corresponding interfaces with concrete classes in the method for constructing a coupling system, a two-tuple graph-like data structure table is used to record the corresponding relationship between interfaces and concrete implementation classes. Further, in the method for constructing a coupled system, the dynamic agent is a special class that dynamically implements a set of specified interfaces during the runtime of the coupled system.
进一步地,所述构建耦合系统的方法还包括以下步骤:Further, the method for constructing a coupling system also includes the following steps:
确定拥有设定接口方法的接口类,并通过检索所述接口-实现二元组图状数据结构型表,得到所关联的具体实现类;Determine the interface class that has the method of setting the interface, and obtain the associated specific implementation class by retrieving the interface-implementation two-tuple graph-shaped data structure table;
检索方法-发布二元组图状数据结构型表中是否存在对应需要调用接口方法的发布行为类,若存在,则调用发布行为类的调用方法,进行主题发布;Retrieval method-whether there is a corresponding publishing behavior class that needs to call the interface method in the publishing two-tuple graph data structure table, if it exists, call the calling method of the publishing behavior class to publish the topic;
调用所述具体类所实现的接口方法。The interface method implemented by the concrete class is invoked.
采用本发明技术方案,在构造耦合系统时,允许开发者在开始设计时,无需刻意确定哪些对象作为主题设计;而在后续开发、甚至软件提交后,因需求变化而进行修改时,仅需要添加新代码,就能指定主题对象,定制发布点,添加主题发布行为,而无需修改已正常工作的原有部分。Adopting the technical solution of the present invention, when constructing the coupling system, developers are allowed to start the design without deliberately determining which objects are used as the theme design; and when the subsequent development or even software submission is modified due to changes in requirements, it is only necessary to add With new code, you can specify topic objects, customize publishing points, and add topic publishing behavior without modifying the original parts that already work normally.
采用本发明技术方案构造的耦合系统,将每个接口的方法,都当作潜在可能的发布点,在需要时,利用动态代理机制,在实现特定接口的具体类方法被实际调用前,动态的加入对主题发布行为的调用。应用本发明加入主题发布行为的调用无需修改被指定为发布点的函数方法,因此很好的符合了“开放-封闭”原则。主题对象也不必预先加入保存观察者对象引用的列表等数据结构。事实上,应用本发明,主题对象和一般的对象没有任何区别。这是因为每个接口的方法都可被当作发布点。实际发布点和相应的对主题发布行为的调用可随着系统的演进,需求的变化而动态加入或去除,这使得软件系统具有快速响应变化的能力。The coupling system constructed by adopting the technical scheme of the present invention regards the method of each interface as a potential release point, and when necessary, uses the dynamic proxy mechanism to dynamically implement the method of the specific class that implements the specific interface before it is actually called. Adds a call to the topic publish action. The application of the present invention to add the call of the topic publishing behavior does not need to modify the function method designated as the publishing point, so it is well in line with the "open-closed" principle. Subject objects also don't have to prepend data structures like lists holding observer object references. In fact, applying the present invention, there is no difference between subject objects and general objects. This is because every method of the interface can be considered as a publishing point. The actual publishing point and the corresponding call to the topic publishing behavior can be dynamically added or removed as the system evolves and the requirements change, which makes the software system have the ability to respond to changes quickly.
同时,采用本发明的技术方案,确定发布点、加入主题发布代码,完全不会影响已经定义好的接口和相关的具体实现类。可灵活的根据需求,动态的确定发布点和加入主题发布功能,无需事先在预设的发布点加入主题发布行为,甚至无需预先确定要设计为主题样式的模型。可在系统演进的过程中,逐步增加发布点和主题发布行为。提高了软件系统响应变化的能力,有助于构建大型复杂的系统。At the same time, by adopting the technical solution of the present invention, determining the release point and adding the topic release code will not affect the defined interface and related specific implementation classes at all. It can flexibly and dynamically determine the publishing point and add the theme publishing function according to the needs, without adding the theme publishing behavior at the preset publishing point in advance, or even pre-determining the model to be designed as a theme style. In the process of system evolution, publishing points and topic publishing behaviors can be gradually increased. It improves the ability of the software system to respond to changes and helps to build large and complex systems.
附图说明Description of drawings
图1示出了本发明提供构建耦合系统的流程图;Fig. 1 shows that the present invention provides the flowchart of constructing coupling system;
图2示出了动态代理调用示意图;Fig. 2 shows a schematic diagram of dynamic proxy invocation;
图3示出了本发明提供的具体实现类方法被呼叫前的发布行为类调用示意图;Fig. 3 shows the schematic diagram of the publishing behavior class call before the specific implementation class method provided by the present invention is called;
图4示出了本发明提供的代理帮助类(ProxyHelper)内部实现调用管理者类(InvocationHandler)接口类的invoke(调用规范中文名称)方法执行流程图。Fig. 4 shows the execution flow chart of the invoke (Chinese name of the call specification) method implemented in the proxy helper class (ProxyHelper) provided by the present invention to invoke the manager class (InvocationHandler) interface class.
具体实施方式Detailed ways
下面将结合附图,举例说明本发明的具体实施方式。The specific implementation manner of the present invention will be illustrated below with reference to the accompanying drawings.
本发明以OCP为指导原则,其实现建立在“针对接口编程,而非实现”这一面向对象编程指导原则和动态代理的语言机制基础上,其包含的步骤如下:The present invention takes OCP as the guiding principle, and its realization is based on the object-oriented programming guiding principle of "programming for the interface, rather than realization" and the language mechanism of dynamic agent, and the steps it includes are as follows:
第一步:确定软件系统各个模型功能,根据功能定义接口。该步骤中的接口主要针对系统的业务逻辑。为方便叙述,本发明的一个优选的实现系统的程序语言为Java语言。在Java中,接口这一概念由关键字“接口”(Interface)表达。定义接口时,应避免从系统的实现角度,而是从各模块所要提供的功能角度考虑。接口应能完整、清晰的表达模块的功能集。Step 1: Determine the function of each model of the software system, and define the interface according to the function. The interface in this step is mainly for the business logic of the system. For convenience of description, a preferred implementation system programming language of the present invention is Java language. In Java, the concept of an interface is expressed by the keyword "Interface". When defining the interface, it should be considered from the perspective of system implementation, but from the perspective of the functions to be provided by each module. The interface should be able to fully and clearly express the function set of the module.
第二步:编写具体类,实现第一步中定义的接口。这里应注意,具体类和接口并不一定是一一对应的关系,根据系统实现的实际情况,可能由多个类分别实现某一接口的一部分,然后通过继承,组合等方式对应到该接口,也可能由一个类完成若干个接口的功能。具体类实现完成后,即可通过后文详细描述的工具类将具体类与对应的接口关联起来,这样对抽象接口的调用最终会被映射到对这些具体类方法的调用。The second step: write a concrete class to implement the interface defined in the first step. It should be noted here that there is not necessarily a one-to-one correspondence between specific classes and interfaces. According to the actual situation of system implementation, multiple classes may respectively implement a part of an interface, and then correspond to the interface through inheritance, combination, etc. It is also possible for a class to complete the functions of several interfaces. After the implementation of the concrete class is completed, the concrete class can be associated with the corresponding interface through the tool class described in detail later, so that the call to the abstract interface will eventually be mapped to the call to these concrete class methods.
第三步:根据系统需求,确定需要进行监察的主题对象和发布点。该步骤和后续步骤可能会被反复进行,直到系统满足用户要求为止。这里的主题对象是第一步中确定的接口,而发布点就是这些接口中的接口方法。在观察者模式中,主题发布行为嵌入在主题对象中,而本发明将发布行为从主题对象剥离,成为单独的实体。称实现了发布行为的类为发布行为类。发布行为类都具有一个标准方法,该方法将会在所关联的发布点(即特定的接口方法)运行前被执行。需要针对具体的主题接口实现发布行为类,也就是实现发布行为类的标准方法中具体的运行内容。然后通过后文详细描述的工具类,将该发布行为类与确定的发布点,即一个确定的接口方法关联起来。其结果是系统运行,特定接口方法(发布点)被调用时,相关联发布行为类的标准方法被执行,进行主题状态变化的发布。Step 3: According to system requirements, determine the subject objects and release points that need to be monitored. This step and subsequent steps may be iterated until the system satisfies the user's requirements. The subject objects here are the interfaces determined in the first step, and the publishing points are the interface methods in these interfaces. In the observer pattern, the topic publishing behavior is embedded in the topic object, but the present invention separates the publishing behavior from the topic object and becomes an independent entity. The class that implements the publishing behavior is called the publishing behavior class. Publishing behavior classes all have a standard method, which will be executed before the associated publishing point (that is, a specific interface method) runs. The publishing behavior class needs to be implemented for the specific theme interface, that is, the specific operation content in the standard method of implementing the publishing behavior class. Then, through the tool class described in detail later, associate the publishing behavior class with a certain publishing point, that is, a certain interface method. As a result, the system is running, and when a specific interface method (publishing point) is called, the standard method of the associated publishing behavior class is executed to publish the topic status change.
第四步:当前述三个步骤完成后,一个可运行的耦合系统已被构建起来,此时可以试运行系统,检测第三步中的主题对象和发布点是否合适。如需要改动主题对象和发布点,则重新回到第三步,进行修改,并再次进行试运行。这样的循环过程,根据系统的复杂,难易程度不同,会进行若干次,系统将会逐渐稳定下来。由于主题对象和发布行为的分离,这样循环修改代价较小,能相对较快的稳定系统。Step 4: After the above three steps are completed, an operational coupling system has been constructed. At this time, the system can be trial run to check whether the subject objects and publishing points in the third step are suitable. If you need to change the subject object and publishing point, go back to the third step, make modifications, and perform a trial run again. Such a cyclic process will be carried out several times according to the complexity and difficulty of the system, and the system will gradually stabilize. Due to the separation of topic objects and publishing behavior, the cost of cyclic modification is relatively small, and the system can be stabilized relatively quickly.
第五步:所构建的耦合系统趋于稳定,交付用户使用。此后系统维护阶段,若由于模型关联关系发生变化,而引起系统修改时,仍可回到第三步骤,再次进行主题对象和发布点的重新定义和发布行为的重新实现,即,确定发布点,并编写发布行为类、利用工具类插入到所确定的接口方法(即发布点)中。Step 5: The constructed coupling system tends to be stable and delivered to users. Afterwards, in the system maintenance phase, if the system is modified due to changes in the model association relationship, you can still go back to the third step to redefine the subject object and publishing point and reimplement the publishing behavior again, that is, determine the publishing point, And write the release behavior class, use the tool class to insert into the determined interface method (that is, the release point).
下面结合图1,详细描述各本发明中的实施方式:Below in conjunction with Fig. 1, describe in detail each embodiment in the present invention:
针对本发明的上述第一步,进一步的具体实施方式为:For above-mentioned first step of the present invention, further specific implementation mode is:
定义系统各个模型功能,定义接口,并编写具体类实现接口。此时采用“针对接口编程”的指导原则,给各模型抽象定义出若干功能集合,具体到Java语言,就是普通的Interface,即接口。通过这些接口,系统的业务逻辑被更为清晰的表达出来。下面介绍“针对接口编程,而非实现”的指导原则:Define the functions of each model of the system, define the interface, and write concrete classes to implement the interface. At this time, the guiding principle of "programming for the interface" is adopted to abstractly define several function sets for each model. Specifically, in the Java language, it is an ordinary Interface, that is, an interface. Through these interfaces, the business logic of the system is expressed more clearly. Here are the guidelines for "programming to the interface, not the implementation":
接口,简单说,就是定义了一组功能的集合,如果某一对象支持该接口,那么它就一定实现了该接口定义的功能集合。如,当前流行的两大面向对象语言(即00语言)语言:C++和Java程序语言都可支持接口这一概念。C++程序语言通常用全部用纯虚函数组成的抽象类表达,例如以下代描述:In short, an interface defines a set of functions. If an object supports the interface, it must implement the set of functions defined by the interface. For example, the two popular object-oriented languages (that is, 00 languages) languages: C++ and Java programming languages can both support the concept of interface. The C++ programming language is usually expressed by an abstract class composed entirely of pure virtual functions, such as the following generation description:
class intf{class intf{
public:public:
void foo1()=0;void foo1()=0;
void foo2()=0;void foo2() = 0;
};};
而Java程序则从语法层次上就有接口这一概念,用关键字“接口”(即Interface)表示,在接口中只能定义方法的形式:如,方法的名称、参数、返回类型,但不定义方法主体。类可以实现一个或若干个接口,类可用“implements”表达。例如以下代描述:The Java program has the concept of interface from the grammatical level, which is represented by the keyword "interface" (that is, Interface). In the interface, only the form of the method can be defined: for example, the name, parameters, and return type of the method, but not Define the method body. A class can implement one or several interfaces, and a class can be expressed with "implements". For example, the following generation description:
Interface intf{Interface intf{
public void foo();public void foo();
}}
Class cls implements intf{Class cls implements intf{
public void foo(){public void foo(){
//foo的一个实现//an implementation of foo
}}
}}
“针对接口编程,而非实现”的OCP原则使得:一个对象可以很容易地被实现了相同接口的的另一个对象所替换。对象间的连接不必硬绑定到一个具体类的对象上,因此增加了灵活性。它表达了这样一个理论:要把对象的行为和对象的实现分离开来。具体说就是,如果一个实体可以有多种实现方式,则在设计实体行为的描述方式时,应当达到这样一个目标:在使用实体的时候,无需详细了解实体行为的实现方式。The OCP principle of "programming to interfaces, not implementations" makes it possible for an object to be easily replaced by another object that implements the same interface. Connections between objects do not have to be hard-bound to objects of a concrete class, thus increasing flexibility. It expresses such a theory: to separate the behavior of the object from the realization of the object. Specifically, if an entity can have multiple implementations, when designing the description of the entity's behavior, it should achieve such a goal: when using the entity, it is not necessary to understand the implementation of the entity's behavior in detail.
事实上,越来越多的软件系统构架都十分强调接口的定义,以期更好地屏蔽实现细节,增加系统灵活性。如Java2企业版本(J2EE-Java 2 Enterprise Edition)的商业Java组件(EJB——Enterprise Java Bean)。In fact, more and more software system architectures place great emphasis on the definition of interfaces in order to better shield implementation details and increase system flexibility. Such as the commercial Java component (EJB - Enterprise Java Bean) of Java2 Enterprise Edition (J2EE-Java 2 Enterprise Edition).
针对本发明的上述第二步,进一步的具体实施方式为:For above-mentioned second step of the present invention, further specific implementation mode is:
编写接口的具体实现类,并将其和对应接口关联,其技术基础是动态代理机制,本发明的一个优选的实现系统的程序语言为Java语言,结合图2介绍如下:Write the concrete realization class of interface, and it is associated with corresponding interface, its technical foundation is dynamic agent mechanism, the programming language of a preferred realization system of the present invention is Java language, introduces as follows in conjunction with Fig. 2:
在设计模式中,代理(Proxy)模式的实质是不直接调用目标对象,而是通过一个代理对象间接的调用目标对象。通过这种方式,代理对象可以在调用目标对象的前后增加额外的功能。代理对象要做的完全透明,所以它必须和目标对象实现同样的接口。代理对象和目标对象需保持相同接口所带来的紧密耦合使得应用代理模式需要付出很大代价。而动态代理则更加灵活。该代理模式中,代理对象不必与所代理的目标对象保持相同的接口,甚至可以用一个动态代理对象代理好几个目标对象。动态代理一般是通过编程语言中的反射机制实现的,例如Java中的动态代理机制便是如此。In the design mode, the essence of the proxy (Proxy) mode is not to directly call the target object, but to indirectly call the target object through a proxy object. In this way, the proxy object can add additional functionality before and after calling the target object. The proxy object is completely transparent, so it must implement the same interface as the target object. The tight coupling brought about by the need for the proxy object and the target object to maintain the same interface makes the application of the proxy pattern very expensive. Dynamic proxies are more flexible. In this proxy mode, the proxy object does not have to maintain the same interface as the proxied target object, and even several target objects can be represented by a dynamic proxy object. Dynamic proxies are generally implemented through reflection mechanisms in programming languages, such as the dynamic proxy mechanism in Java.
对Java而言,动态代理可以看作是一个在运行期实现一组指定接口的特殊类,它具有两个特点For Java, a dynamic proxy can be seen as a special class that implements a set of specified interfaces at runtime. It has two characteristics
●运行期动态生成●Dynamic generation during runtime
●实现一组指定接口,可以直接被造型成指定的对象● Implement a set of specified interfaces, which can be directly modeled into specified objects
动态代理能用来动态地为一组接口产生类型安全的代理类。对动态代理所暴露接口的方法调用将被转换为一种统一的调用方式,然后回调到它的调用处理类(InvocationHandler)上,由调用处理类(InvocationHandler)完成具体的处理。Dynamic proxies can be used to dynamically generate type-safe proxy classes for a set of interfaces. The method call to the interface exposed by the dynamic proxy will be converted into a unified calling method, and then called back to its invocation processing class (InvocationHandler), and the invocation processing class (InvocationHandler) will complete the specific processing.
具体实现上动态代理由Java语言开发工具包(即JDK——Java Development Kit)的java.lang.reflect包中提供的一个代理(Proxy)类产生,它包含一个创建动态代理所用的静态方法,例如以下代码描述:In terms of specific implementation, the dynamic proxy is generated by a proxy (Proxy) class provided in the java.lang.reflect package of the Java Language Development Kit (JDK—Java Development Kit), which contains a static method for creating a dynamic proxy, such as The following code describes:
newProxyInstance()newProxyInstance()
public static Objeet newProxyInstance(ClassLoader loader,public static Objeet newProxyInstance(ClassLoader loader,
Class[]interfaces.Class[] interfaces.
InvocationHandler h)InvocationHandler h)
throws IllegalArgumentExceptionthrows IllegalArgumentException
该方法返回一个具有指定接口的proxy类的实例,这个proxy实例将调用其接口的方法映射到指定的调用处理器(invocation handler)上,如图4所示。This method returns an instance of the proxy class with the specified interface, and this proxy instance maps the method calling its interface to the specified invocation handler, as shown in Figure 4.
参数:parameter:
loader:proxy所在类加载器loader: The class loader where the proxy is located
interfaces:一组需要代理类实现的接口interfaces: a set of interfaces that need to be implemented by the proxy class
h:调用处理器,用于处理对动态代理所实现接口的方法调用。h: Invocation processor, used to process method invocations of interfaces implemented by dynamic proxies.
在创建动态代理的过程中,需要指定响应调用的调用处理器(invocationhandler),这是方法调用的一个集中控制点,所有对动态代理所提供接口的调用都会被转换为对InvocationHandler中调用(invoke)方法的调用,InvocationHandler是JDK中java.lang.reflect包下面的一个接口,在这个接口中只有invoke一个方法,例如以下代描述:In the process of creating a dynamic proxy, you need to specify the invocation handler (invocationhandler) that responds to the call. This is a centralized control point for method calls. All calls to the interface provided by the dynamic proxy will be converted to calls (invoke) in the InvocationHandler. Method call, InvocationHandler is an interface under the java.lang.reflect package in JDK. In this interface, there is only one method, invoke, such as the following generation description:
public Object invoke(Object proxy,Method method,Object[]args)public Object invoke(Object proxy, Method method, Object[] args)
这里JDK将负责完成对动态代理调用的转换,通过回调InvocationHandler的invoke方法将各种调用转换为统一的形式,从接口函数可以看到调用的方法名和参数数组是作为方法参数传递到invoke方法中,invoke方法将处理所有对动态代理的调用。Here JDK will be responsible for completing the conversion of the dynamic proxy call. By calling back the invoke method of InvocationHandler, various calls are converted into a unified form. From the interface function, we can see that the called method name and parameter array are passed to the invoke method as method parameters. The invoke method will handle all calls to the dynamic proxy.
综上,动态代理能够实现指定接口在运行期的动态绑定,对调用者而言它可以将动态代理看作是一个实现了一组指定接口的普通JAVA对象,对动态代理的调用和普通方法调用完全一样。另外动态代理将对其绑定接口的调用都传递给一个统一的处理类InvocationHandler,在InvocationHandler上可以完成集中的控制和处理。In summary, the dynamic proxy can realize the dynamic binding of the specified interface at runtime. For the caller, it can regard the dynamic proxy as an ordinary JAVA object that implements a set of specified interfaces. The call to the dynamic proxy and the common method calls exactly the same. In addition, the dynamic proxy passes all calls to its bound interface to a unified processing class InvocationHandler, and centralized control and processing can be completed on the InvocationHandler.
针对本发明的上述第二步,进一步的具体实施方式为:For above-mentioned second step of the present invention, further specific implementation mode is:
将具体实现类和接口关联,这是通过实现一个工具类——ProxyHelper(代理帮助类)来完成的。该工具类记录接口与具体实现类的对应关系,考虑到一个接口对应多个实现类的情况,可采用用图状数据结构型(即map)数据结构记录对应关系,对这样的<接口、具体实现类对象>二元组Map表,称之为接口-实现Map表。ProxyHelper还向外提供了创建特定接口对象的方法。该方法就是使用创建代理实例(即newProxyInstance,是JDK-Java Development Kit中的标准函数)函数创建代理对象,并传入ProxyHelper内部实现的InvocationHandler作为参数中的处理类对象。Associate the concrete implementation class with the interface, which is accomplished by implementing a tool class——ProxyHelper (proxy helper class). This tool class records the corresponding relationship between the interface and the specific implementation class. Considering that one interface corresponds to multiple implementation classes, a graph data structure (ie map) data structure can be used to record the corresponding relationship. For such <interface, specific Realize class object>Two-tuple Map table, which is called interface-implementation Map table. ProxyHelper also provides methods to create specific interface objects. This method is to use the function of creating a proxy instance (namely newProxyInstance, which is a standard function in JDK-Java Development Kit) to create a proxy object, and pass in the InvocationHandler implemented inside ProxyHelper as the processing class object in the parameter.
ProxyHelper内部所实现的InvocationHandler处理类,它的invoke方法执行流程是:调用得到方法所在的类(即Method.GetDeclaringClass,是JDK-JavaDevelopment Kit中的标准函数)得到拥有该接口方法的接口类,并通过检索接口-实现Map表,得到所关联的具体实现类,再经过后续提及的对可能存在的发布行为类调用后,最终调用具体类所实现的接口方法。The InvocationHandler processing class implemented inside ProxyHelper, its invoke method execution flow is: call the class where the method is located (that is, Method.GetDeclaringClass, which is a standard function in JDK-JavaDevelopment Kit) to get the interface class that has the interface method, and pass Retrieve the interface - implement the Map table, get the associated specific implementation class, and then call the possible publishing behavior class mentioned later, and finally call the interface method implemented by the specific class.
本发明中发布点就是接口方法。The publishing point in the present invention is the interface method.
针对本发明的上述第三步,进一步的具体实施方式为:For above-mentioned 3rd step of the present invention, further specific embodiment is:
从系统 的接口中选择需要进行监察的主题接口对象,并进一步选定该接口的一个或若干个接口方法作为需要监察的发布点。主题发布行为类是实现标准接口拦截器(Interceptor)的具体类。Select the subject interface object that needs to be monitored from the interface of the system, and further select one or several interface methods of the interface as the publishing point that needs to be monitored. The topic publishing behavior class is a concrete class that implements the standard interface Interceptor.
例如以下代码描述:For example, the following code description:
Interceptor接口定义如下:The Interceptor interface is defined as follows:
public interface Interceptor{public interface Interceptor{
public Object invoke(Invocation invocetion);public Object invoke(Invocation invocation);
}}
如图3所示,Interceptor的invoke方法即前面所提到的标准方法,它将会在发布点被调用。As shown in Figure 3, the invoke method of the Interceptor is the standard method mentioned above, and it will be called at the publishing point.
应用本发明要实现该接口,在invoke中编写主题对象状态改变时应该进行的动作,对应传统观察者模式,invoke就相当于观察者(Observer)的更新(Update)方法。其中,invocetion对象保存了invoke中可能会用到的上下文信息。To implement the interface by applying the present invention, the actions that should be performed when the state of the subject object changes are written in invoke. Corresponding to the traditional observer mode, invoke is equivalent to the Update method of the observer (Observer). Among them, the invocation object saves the context information that may be used in invoke.
利用Java反射机制(如Class.getMethod)得到代表接口方法的Java对象(Method),这就指定了作为发布点的接口方法。然后将实现Interceptor的主题发布行为与该发布点关联起来。这也由ProxyHelper实现,即ProxyHelper负责记录这样的发布点-主题发布行为类的关联关系。与前面类似,ProxyHelper用Map型数据结构记录这一对应关系,称这样的<接口方法、发布行为类>二元组Map表为方法-发布Map表。事实上,ProxyHelper也就代替传统观察者模式中作为主题对象要保存观察者列表,并进行主题发布的功能。Use the Java reflection mechanism (such as Class.getMethod) to obtain the Java object (Method) representing the interface method, which specifies the interface method as the publishing point. Then associate the topic publishing behavior that implements the Interceptor with the publishing point. This is also implemented by ProxyHelper, that is, ProxyHelper is responsible for recording such publishing point-topic publishing behavior class associations. Similar to the previous ones, ProxyHelper uses a Map-type data structure to record this correspondence, and calls such a <interface method, publishing behavior class> binary map table as a method-publishing Map table. In fact, ProxyHelper also replaces the function of saving the observer list and publishing the topic as the subject object in the traditional observer mode.
ProxyHelper内部类实现调用处理器接口,具体对InvocationHandler的publicObject invoke(Object proxy,Method method,Object[]args)这一接口方法的实现,如附图4所示,其完整流程如下:The internal class of ProxyHelper implements the call handler interface, specifically the implementation of the interface method publicObject invoke(Object proxy, Method method, Object[]args) of InvocationHandler, as shown in Figure 4, and its complete process is as follows:
第一步,调用Method.GetDeclaringClass得到拥有该接口方法的接口类,并通过检索接口-实现Map表,得到所关联的具体实现类。The first step is to call Method.GetDeclaringClass to get the interface class that has the interface method, and get the associated specific implementation class by retrieving the interface-implementation Map table.
第二步,检索方法-发布Map表中是否存在对应该次调用接口方法(Methodmethod)的发布行为类(Interceptor),如存在,则调用发布行为类的invoke方法,进行主题发布。The second step is to check whether there is an publishing behavior class (Interceptor) corresponding to the calling interface method (Methodmethod) in the method-publishing Map table. If it exists, call the invoke method of the publishing behavior class to publish the topic.
第三步,调用具体类所实现的接口方法(Method.invoke)。即传入接口-实现列表中记录的具体实现类对象,执行具体实现类实现的接口方法。The third step is to call the interface method (Method.invoke) implemented by the concrete class. That is, the specific implementation class object recorded in the interface-implementation list is passed in, and the interface method implemented by the specific implementation class is executed.
综上,辅助工具类ProxyHelper的功能和实现细节如下:In summary, the functions and implementation details of the auxiliary tool class ProxyHelper are as follows:
ProxyHelper负责:1、注册接口和实现该接口具体实现类对象实例。2、记录特定的接口方法所对应的发布行为,具体实施中,主题发布行为一般都是接口Interceptor的具体实现。3、收集所有登记的接口,创建动态代理对象。4、实现了创建动态代理对象所需要的响应调用器(InvocationHandler),在其invoke方法的实现中,完成对发布点的主题发布代码调用。ProxyHelper is responsible for: 1. Registering the interface and implementing the interface to implement the class object instance. 2. Record the publishing behavior corresponding to the specific interface method. In the specific implementation, the topic publishing behavior is generally the specific implementation of the interface Interceptor. 3. Collect all registered interfaces and create dynamic proxy objects. 4. Realize the response caller (InvocationHandler) required to create the dynamic proxy object, and complete the call to the topic publishing code of the publishing point in the implementation of its invoke method.
ProxyHelper中记录了两个二元组Map表:Two tuple Map tables are recorded in ProxyHelper:
一、<接口、具体实现类对象>二元组Map表:称为接口-实现Map表;用于记录接口和实现该接口具体实现类对象实例的对应关系。1. <interface, specific implementation class object> 2-tuple Map table: called the interface-implementation Map table; used to record the corresponding relationship between the interface and the specific implementation class object instance that implements the interface.
ProxyHelper根据接口-实现列表,收集所有登记的接口,并以内部类为调用处理器,调用Java反射包中的Proxy类静态函数newProxyInstance创建动态代理对象。ProxyHelper collects all registered interfaces according to the interface-implementation list, and calls the static function newProxyInstance of the Proxy class in the Java reflection package to create a dynamic proxy object with the internal class as the call processor.
二、<接口方法、主题发布代码>二元组Map表:称为方法-发布Map表;用于记录特定的接口方法所对应的发布行为类。2. <Interface method, topic release code> Two-tuple Map table: called method-release Map table; used to record the release behavior class corresponding to a specific interface method.
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CNB2004100654007ACN100373332C (en) | 2004-11-28 | 2004-11-28 | Methods for building coupled systems |
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CNB2004100654007ACN100373332C (en) | 2004-11-28 | 2004-11-28 | Methods for building coupled systems |
| Publication Number | Publication Date |
|---|---|
| CN1779636Atrue CN1779636A (en) | 2006-05-31 |
| CN100373332C CN100373332C (en) | 2008-03-05 |
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CNB2004100654007AExpired - LifetimeCN100373332C (en) | 2004-11-28 | 2004-11-28 | Methods for building coupled systems |
| Country | Link |
|---|---|
| CN (1) | CN100373332C (en) |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN102523128A (en)* | 2011-12-31 | 2012-06-27 | 浙江万朋网络技术有限公司 | Data synchronism method in remote data transmission |
| CN104580501A (en)* | 2015-01-23 | 2015-04-29 | 广东能龙教育股份有限公司 | A method and system for dynamically publishing Http interface based on reflection mechanism |
| CN108205466A (en)* | 2016-12-20 | 2018-06-26 | 北京京东尚科信息技术有限公司 | The execution method and apparatus of computer function method |
| CN111443945A (en)* | 2019-01-16 | 2020-07-24 | 北大方正集团有限公司 | Component code modification method and device |
| WO2023098241A1 (en)* | 2021-12-03 | 2023-06-08 | 深圳前海微众银行股份有限公司 | Request processing method and apparatus |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| GB0222420D0 (en)* | 2002-09-27 | 2002-11-06 | Ibm | Optimized corba software method invocation |
| US7406695B2 (en)* | 2003-01-17 | 2008-07-29 | Sap Aktiengesellschaft | Automatically upgradeable extension of software |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN102523128A (en)* | 2011-12-31 | 2012-06-27 | 浙江万朋网络技术有限公司 | Data synchronism method in remote data transmission |
| CN102523128B (en)* | 2011-12-31 | 2014-09-24 | 浙江万朋网络技术有限公司 | Data synchronism method in remote data transmission |
| CN104580501A (en)* | 2015-01-23 | 2015-04-29 | 广东能龙教育股份有限公司 | A method and system for dynamically publishing Http interface based on reflection mechanism |
| CN108205466A (en)* | 2016-12-20 | 2018-06-26 | 北京京东尚科信息技术有限公司 | The execution method and apparatus of computer function method |
| CN108205466B (en)* | 2016-12-20 | 2021-04-30 | 北京京东尚科信息技术有限公司 | Method and device for executing computer function method |
| CN111443945A (en)* | 2019-01-16 | 2020-07-24 | 北大方正集团有限公司 | Component code modification method and device |
| CN111443945B (en)* | 2019-01-16 | 2021-08-27 | 北大方正集团有限公司 | Component code modification method and device |
| WO2023098241A1 (en)* | 2021-12-03 | 2023-06-08 | 深圳前海微众银行股份有限公司 | Request processing method and apparatus |
| Publication number | Publication date |
|---|---|
| CN100373332C (en) | 2008-03-05 |
| Publication | Publication Date | Title |
|---|---|---|
| CN1171145C (en) | Request scheduler for automatic software configuration | |
| CN1120416C (en) | Inner appearance editor system, process and method using outer appearnce to proceed software translation | |
| US7539985B2 (en) | Systems and methods for dynamic component versioning | |
| US20030200532A1 (en) | System and method for sharing reusable code base | |
| CN1203404C (en) | Full-delay interlinkage with module-by-module verification | |
| US20090249374A1 (en) | Dynamic template instantiation | |
| US20110239184A1 (en) | Capturing and utilizing specific module dependency information | |
| CN1577265A (en) | Description language for an extensible compiler and tools infrastructure | |
| CN1669020A (en) | Component Models for Real-time System Control | |
| CN1592227A (en) | Method and system for providing life-cycle management of grid services | |
| CN101078995A (en) | Method for accessing information service in component operation support platform | |
| CN1313926C (en) | Template compilation method | |
| CN1641601A (en) | Software unit measuring method | |
| CN103197972A (en) | Method and device for browser plugin management, browser and plugin interactive system | |
| CN1755617A (en) | Entity domain | |
| CN1313925C (en) | Construction member self description packaging method and method of operation | |
| CN112860232B (en) | Component implementation method and device | |
| CN1975669A (en) | Method for service logic mapping onto applied language discribed by XML language | |
| CN1779636A (en) | Methods for building coupled systems | |
| CN1924805A (en) | Method for realizing dynamic clinkbase in wireless binary running environment platform | |
| US8010941B2 (en) | Method and system for dynamically generating enterprise java beans during installation of a service component architecture application | |
| CN1786925A (en) | TTCN-3 testing system basedon C++ mapping and its testing method | |
| CN1245685C (en) | Drive method based on structure operation system dynamic equipment | |
| CN1620655A (en) | Systems and methods for controlling user interface properties with data | |
| CN1170225C (en) | Implementation method of instrument module driver in automatic test system |
| Date | Code | Title | Description |
|---|---|---|---|
| C06 | Publication | ||
| PB01 | Publication | ||
| C10 | Entry into substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| C14 | Grant of patent or utility model | ||
| GR01 | Patent grant | ||
| CX01 | Expiry of patent term | ||
| CX01 | Expiry of patent term | Granted publication date:20080305 |