Shark and Adapter design pattern

zhaozj2021-02-16  62

Shark and Adapter design pattern

1 Introduction

A P is a very obsessed Programmer. Recently, the company has sent a rich bonus, A P Lead, ready to be a new tide, to the long-lasting Java Tourism for a month.

Ap belt must prepare for the cloth, from the online Down, a very wide range of "Essentials Guide to Tour", then A P Stone I want to bring it, right, right, notebook Still haven't taken it, this is not to bring, there is also the latest downloaded famous workflow engine Shark source code. A P recently worked in the development of workflow system. The company made him study Shark's source code; hey, Shark's source code is too complicated. He looked for many days without a shortest, I was cope.

2.?? Encounter Adapter

Apache hotel in the town of OpenSource in Java, I opened the notebook, I was prepared to send an email to my mother. At this time, Ap is dumb: It turns out that the power outlet of Java is a mouth, but His laptop plug is two, what should I do?

A P-to the hotel lobby to find a waiter, waiter told him that it is necessary to go to Pattern Temple wholesale market to buy a power Adapter.

Ap arrived to the wholesale market, I know what the power adapter is long. It turns out that there is a small box, and there is a head in both sides. It can pick up the power socket of the Jewa, and the other can pick up your notebook. Power Adapter transforms the Power interface of Java into another interface expected to be expected to work with two things that cannot be worked together by the interface mismatch. It's really a good thing, Appeal before, what ?? I want 30J yuan? Just this little thing?

A p is turned, or this price. Oh, too expensive, Ap wants to buy it; but don't buy this how to use a notebook? Hey, it seems that this adapter is really no need. A Phaka bought it down.

3. What is Shark?

After the P came back, I was ready to go out tomorrow, and I first take a look at Shark code. A P open URL http://shark.objectweb.org, browse the web:

"Shark1.0 is a very famous, according to the WFMC specification, the scalable function workflow engine, which uses XPDL to define the process, and also includes the server-side WFMC tool API for active nodes execution API. This Tool Agent API You can serve JavaScript, JDBC Access, EJB Access, Pure Java Classes, Corba Calls, Email, WebService calls. Flow storage and activity instantiation is completed through a definable persistence API. SHARK1.0 uses itself A lightweight ENHYDRA DODS O / R MAPPING as its own standard persistence layer, but it also supports heavyweight J2EE EJB persistence. Each component in SHARK1.0, such as a persistent layer, a matter manager, a scripting engine, The process library can be used in accordance with standard implementation, and can also be expanded and replaced by the module of specific items. So, Shark can be used as a simple library of servlet and swing programs, or running in J2EE containers (support The API of the session bean may use entity beans as a persistence layer), or can be used as a WebService to be accessed. ...........

A P is desperate, and he studied SHARK, he listened to other masters. SHARK1.0 Beta2's function and package structure is quite complex, but due to the use of various design patterns, we have a chapter when we study its source code. Can follow, the idea is very clear. The master also emphasizes that you must take a look at Adapter mode ......... What? Adapter ...? Isn't that spent 30J yuan? Is P. began to interested this Adapter ............

4.?? Adapter mode

Programmer is very smart, Ap is no exception, he quickly painted the following picture:

Figure 1: Adapter mode class map Adaptee is the power interface of Java, it is a bite, is Adaptee's method1 (); A P's notebook's power interface is Target, two, is Method1 () and Method2 ( ClassAdapter is that little thing.

A P Analysis: That small thing has a small generator, providing a Method2 () interface, and a wire, connect Adaptee's Method1 () and Target's method1 () ... Ap still uses programmers Tone analysis: Adaptee class does not have a method2 () method, while the client expects the method; in order to allow the client to use the Adaptee class, we provide an intermediate class inherited Adaptee, connect two APIs; so the class Adapter mode can Convert interface of the adapted class into the interface of the target class.

5. ?? Object's Adapter mode

Programmer is not only very smart, but it is sharp, and Ap is no exception. A P found a question: The voltage provided by the power interface of Java is 200V, and the voltage of each interface of his notebook is 130V, and the method of the picture just is not right ......... A P. I think. I also painted the following picture immediately: ADAPTER mode class of Figure two objects: ObjectAdapter's method1 () After calling adaptee's method1 (), minus 70V; that is, the small box has added one The resistor is a voltage of 70V.

A P-T also uses the tone analysis of the programmer: the Adaptee class does not have a method2 () method, while the client expects the method. In order to enable the client to use this method, you need to provide an ObjectAdapter class, which is packaged in an instance of the Adaptee class, thus connecting two sets of APIs. Therefore, the object's Adapter mode uses the associated relationship to convert the interface of the adapted class into the interface of the target class.

6. Application of the Adapter mode of the class in Shark

Analysis of this, Ap is very happy, this will understand the principles of Adapter, and the P Preparing to see the SHARK source code that has been unknown, look at the master's Adapter how it is in Shark. A PHKEK knows that the business object is defined in the BasebusinessObject interface, and the WFExecutionObject is a business object, inherits BaseBusinessObject, and the process object (WfProcess) and activity object (WFActivity) is a workflow execution Object, inherit WFExecutionObject. A PK knows that the development of the workflow system is of course the process and activities, he is ready to first look at the implementation of the process ............ ..

6.1 Code Reading A P found that when the client calls the process-related interface, the SHARK engine will call a critical interface: WfProcessinternal interface.

A P also found that the functional implementation of the WFProcessInternal interface uses the class's Adapter mode. The WFProcessinternal interface is the Target of this mode, which provides an activateLimitAgent () method to activate the LimitAgent module, and the start () method is also available to start the process. Adaptee in this mode is the WFEXCEUTIONOBJECTIMPL class, which provides an implementation of the actiVAtureImitAgent () method, but it cannot implement the start () method to start the process.

ClassAdapter in this mode is a WfProcessImpl class, and its code is, for example,:

Public Class WfProcessImpl Extends WFExecutionObjectImpl Implements WfProcessinternal

? {

Public void start (SharkTransaction T) {}

..........

}

6.2 Code Analysis

A P Analysis: WfProcessinternal interface is my notebook interface, there are two ports; the wfExceutionObjectImpl class is the power jack of Java, only one mouth; the WfProcessImpl class is the Adapter who wants to spend 30J Yuan to buy.

ApRuke's tone analysis, the WFProcessImpl class inherits the WFExcexCectionObjectImpl class, and implements the WFProcessInternal interface to extend the partial approach not in the WFExceutObjectImpl class. Since the WFProcessImpl class inherits the WFExceutObjectImpl class, it can override the partial method of the WFEXCEUTIONOBJECTIMPL class, which is the Adapter mode of the standard class. A P is also surprisingly found that not only the WFProcessImpl class uses the class's Adapter mode. The implementation of many classes inside the Shark engine uses this mode, such as the WFActivityImpl class implementation process activity, WFASSIGNTEventAuditWrapper class implements EventAudit, WfDataEventAuditWrapper This mode is adopted by EventAudit, WFStateEventAuditWrapper class implementation of data. It can be said that the Adapter mode of the class is the skeleton of the architecture that the Shark engine is modified, and it is understood that the mode can soon understand the principle of modification of the class implementation in the SHARK engine.

7. Application of the object's Adapter mode in Shark

7.1 code reading

A P quickly found that the WfProcess interface is the interface that Shark is provided to the outside world, which provides all the methods required for the outside world.

The WFProcessImpl class is an implementation class for the method to be called inside the SHARK engine. It only provides the method that needs to be used inside, and does not fully implement the WfProcess interface.

In this case, the system provides a WFProcessWrapper class, and its code example is, for example:

Public Class WfProcessWrapper Implements WfProcess {

?? // This method is used to associate wfProcessImpl classes

Private Static WfProcessinternal GetProcessImpl

????? ??????? SharkTransaction T, String Procid, THROWS BaseException {

????? wfProcessinternal Proc = Sharkutilities.getProcess (t, procid);

????? if (proc == null) throw new baseexception ("Process Does Not Exist);

????? RETURN PROC;

??}

?? ..................................................................

}

7.2 Code Analysis

A P Analysis: Here, the WfProcessWrapper class does not have a member object of a WFProcessImpl class, but there is a getProcessImpl (), and their effect is exactly the same.

A P-Tong's tone analysis, here, the WfProcess interface is our Target, the WfProcessImpl class is our Adaptee, the WfProcessWrapper class is our ObjectAdapter. The WFProcessWrapper class calls the WFProcessImpl class implementation by associating the WFProcessImpl class, and can add the new way you need, which is the Adapter mode of the standard object.

Similarly, A P found that not only the WFProcessWrapper class uses an object's Adapter mode. There are many other classes in the system that use this mode, such as WFACTIVITYWRAPPER implementation of the functional packaging of the event, WFASSIGNMGRWrapper implementation of the task, WfProcessMgrWrapper implementation Process Manager's function packaging, WFResourceWrapper implements features for resources, and more. It can be said that the ADAPTER mode of the object is the skeleton of the architecture that the Shark engine is provided to the outside world, understands the mode, soon understand the principle of the Shark engine to provide the external call class.

8. ?? A P Diary

A P is very excited, he immediately wrote the following AP diary immediately: 1) ????? In the following situation, the Adapter mode should be used: The system has a class, when the function is expanded, the interface interface Not fully compliant with the needs of the system, but we don't want to completely discard the class. 2) ????? The Adapter mode of the class is the skeleton of the architecture of the SHARK engine, and understands the class's Adapter mode, soon understand the principle of modification of the type implementation in the Shark engine.

3) ????? The Adapter mode of the object is the skeleton of the architecture that the Shark engine is implemented to the outside world, understands the Adapter mode of the object, and can quickly understand the principle of the Shark engine to provide the external call class.

4) ????? Understand the ADAPTER mode with overall grasp of Shark, this is the relationship between Shark and Adapter mode.

?

http://blog.9cbs.net/hongbo781202/?

转载请注明原文地址:https://www.9cbs.com/read-18372.html

New Post(0)