Piping mode--pipeline and valve

Source: Internet
Author: User

In a complex large-scale system, if there is an object or data flow that needs to be handled in a complex way, we can choose to do these complex logical processing in a large component, but it is simply rude. Perhaps in some cases this simple and rude way will bring some trouble, such as I want to change some of the processing logic, I want to add some processing logic to the process, I want to reduce some of the processing logic in the process, there are some seemingly simple changes to let us, in addition to the entire component changes. The entire system appears to be free of any extensibility and reusability.
Whether there is a pattern can be the entire process of detailed division, divided into each of the small modules are independent and responsible for a logic processing, these logic processing small modules in order to connect, before the module output as the input of the latter module, the output of the last module is the final processing results. As a result, modifying logic is only modified for a module, and adding or reducing processing logic can also be refined to a module's granularity, and each module can be reused, which is greatly enhanced by reusability. This pattern is the pipeline pattern to be discussed in this section.
As the name implies, the pipeline mode is like a pipe to connect multiple objects, the overall look like a number of valves nested in the pipeline, and the processing logic placed on the valve, such as the need to deal with the object into the pipeline, respectively through the valve one, valve two, valve three, valve four, Each valve carries out some logical processing of incoming objects, which are processed from the end of the pipeline after a layer of processing, and the object is the target object that has been processed.

Since the pipeline mode is so useful, we want to be able to properly consider the use in the program, in order to achieve this pattern requires multiple object collaboration, can refer to the following class diagram, Valve interface defines the method of call valves, as the valve and the valve using a single-linked table structure to connect so need to provide next operation, Implement a valve to expand it; The pipeline interface defines a method for piping operation of a valve, including obtaining the first valve, acquiring the base valve, adding a valve, and so on, the piping needs to be expanded.

Look down how to implement a pipeline pattern simply:
① Valve interface
Public interface Valve {
Public Valve getNext ();
public void Setnext (valve valve);
public void Invoke (String handling);
}
Ii Pipe interface
Public interface Pipeline {
Public Valve GetFirst ();
Public Valve getbasic ();
public void Setbasic (valve valve);
public void addvalve (valve valve);
}
③ Basic valve, processing logic is simply to replace the "AA" in the incoming string with "BB"
public class Basicvalve implements Valve {
protected Valve next = null;
Public Valve GetNext () {
return next;
}
public void Invoke (String handling) {
Handling=handling.replaceall ("AA", "BB");
System.out.println ("After the foundation valve finishes:" + handling);
}
public void Setnext (valve valve) {
This.next = valve;
}
}
④ The second valve, replacing "11" in the incoming string with "22"
public class Secondvalve implements Valve {
protected Valve next = null;
Public Valve GetNext () {
return next;
}
public void Invoke (String handling) {
Handling = Handling.replaceall ("11", "22");
System.out.println ("Second valve finishes:" + handling);
GetNext (). Invoke (handling);
}
public void Setnext (valve valve) {
This.next = valve;
}
}
⑤ The third valve, replacing "zz" in the incoming string with "yy"
public class Thirdvalve implements Valve {
protected Valve next = null;
Public Valve GetNext () {
return next;
}
public void Invoke (String handling) {
Handling = Handling.replaceall ("zz", "yy");
System.out.println ("Third valve finishes:" + handling);
GetNext (). Invoke (handling);
}
public void Setnext (valve valve) {
This.next = valve;
}
}
⑥ Pipeline, our general operation is to set the basic valve through setbasic, and then add the other valves in order, the Order of execution is: first to add the first execution, the final execution of the basic valve.
public class Standardpipeline implements Pipeline {
protected Valve first = null;
protected Valve basic = null;
public void addvalve (valve valve) {
if (first = = null) {
first = valve;
Valve.setnext (Basic);
} else {
Valve current = first;
while (current! = null) {
if (current.getnext () = = Basic) {
Current.setnext (valve);
Valve.setnext (Basic);
Break
}
Current = Current.getnext ();
}
}
}
Public Valve Getbasic () {
return basic;
}
Public Valve GetFirst () {
return first;
}
public void Setbasic (valve valve) {
This.basic = valve;
}
}
⑦ Test class
public class Main {
public static void Main (string[] args) {
String handling= "Aabb1122zzyy";
Standardpipeline pipeline = new Standardpipeline ();
Basicvalve basicvalve = new Basicvalve ();
Secondvalve secondvalve = new Secondvalve ();
Thirdvalve thirdvalve = new Thirdvalve ();
Pipeline.setbasic (Basicvalve);
Pipeline.addvalve (Secondvalve);
Pipeline.addvalve (Thirdvalve);
Pipeline.getfirst (). Invoke (handling);
}
}
The results of the output are as follows:
Second valve after finishing: Aabb2222zzyy
Third valve after finishing: aabb2222yyyy
After the basic valve finishes: BBBB2222YYYY
This is the piping mode, in which one or more valves are connected in the pipe, and each valve is responsible for a part of the logic processing, and the data flows downward in the specified order. This mode decomposes the logic processing task, it is convenient to install and disassemble a task unit, which improves the expansibility, reusability, maneuverability and flexibility of the process.

Piping mode--pipeline and valve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.