因为该模式有以下部分组成所以
所以首先来写filter,但是在写filter之前必须有pipe也就是管道,所以先来定义管道
pipe类
package ch03.pipesfilteres;
import java.util.ArrayDeque;
public class Pipe {
private ArrayDeque<Packet> packets = new ArrayDeque<Packet>();
public void write(Packet packet) {
packets.addFirst(packet);
//System.out.println(""+packet+" is written into pipe.");
}
public int size() {
return packets.size();
}
public Packet read() throws EmptyPipeException {
if (size() == 0) {
throw new EmptyPipeException();
}
Packet p = packets.getLast();
packets.pollLast();
return p;
}
}
写完pipe之后,就可以写filter类了
首先是一个抽象类
package ch03.pipesfilteres;
public abstract class Filter {
protected Pipe input;
protected Pipe output;
public Filter(Pipe in, Pipe out) {
input=in;
output=out;
}
public abstract void process();
}
他的实现:
package ch03.pipesfilteres;
public class FilterImpl extends Filter {
public FilterImpl(Pipe in,Pipe out) {
super(in,out);
}
public void customNotify() {
process();
}
@Override
public void process() {
while (input.size() > 0) {
try {
Packet packet=input.read();//
//packet.setContent("");
output.write(packet);
System.out.println("已经将"+packet+"写入到了pipe2");
} catch (EmptyPipeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
写完这个之后就要把pipe放入数据source中,那么source是如何实现的呢?可想而知,其接受pipe
package ch03.pipesfilteres;
public class DataSource extends Filter{
public DataSource(Pipe out) {
super(null,out);
}
@Override
public void process() {
for(int i=1;i<5;i++) {
System.out.println("DataSource: packet("+i+") is created.");
Packet p=new Packet(i);
p.setContent("第"+i+"已经写入pipe");
System.out.println("第"+i+"已经写入pipe");
output.write(p);
}
}
}
filter写完之后就要开始完成sink的编写了,也就是要将经过filer处理过的pipe输入到终点
package ch03.pipesfilteres;
import java.util.ArrayDeque;
public class DataSink extends Filter{
private FilterImpl observer;
protected ArrayDeque<Packet> received = new ArrayDeque<Packet>();
public DataSink(Pipe in) {
super(in,null);
observer = null;
}
public int size() {
return received.size();
}
public Packet nextPacket() {
Packet p = received.getLast();
received.pollLast();
return p;
}
public void customNotify() {
try {
while(input.size() > 0) {
received.addFirst(input.read());
}
} catch (EmptyPipeException e) {
System.err.println(" empty pipe. ");
}
if(observer != null)
observer.customNotify();
}
public void setObserver(FilterImpl observer) {
this.observer = observer;
}
public FilterImpl getObserver() {
return observer;
}
@Override
public void process() {
try {
int i=0;
for(i=0;i<4;i++){
System.out.println(input.read()+"已经进入了数据池");
}
} catch (EmptyPipeException e) {
e.printStackTrace();
}
}
}
这些写完之后就是其他一些类的编写了
package ch03.pipesfilteres;
public class Packet {
//标号
private int pno = -1;
//标号对应的内容
private String content;
public Packet() {
}
public Packet(int no) {
pno = no;
}
public void setContent(String c) {
content=c;
}
public String toString() {
return "Packet(" + pno + "):"+content;
}
}
package ch03.pipesfilteres;
public class EmptyPipeException extends Exception {
}
package ch03.pipesfilteres;
public class NoOutputPipeException extends Exception {
}
最后就可以编写一个测试类来测试了
package ch03.pipesfilteres;
public class TestPipes {
public static void main(String[] args) {
/** Instantiate our pattern blueprint classes. */
Pipe pipe1 = new Pipe();
Pipe pipe2= new Pipe();
DataSource source = new DataSource(pipe1);
source.process();
FilterImpl filter=new FilterImpl(pipe1,pipe2);
filter.process();
DataSink sink = new DataSink(pipe2);
sink.process();
}
}
得到以下的运行结果
DataSource: packet(1) is created.
第1已经写入pipe
DataSource: packet(2) is created.
第2已经写入pipe
DataSource: packet(3) is created.
第3已经写入pipe
DataSource: packet(4) is created.
第4已经写入pipe
已经将Packet(1):第1已经写入pipe写入到了pipe2
已经将Packet(2):第2已经写入pipe写入到了pipe2
已经将Packet(3):第3已经写入pipe写入到了pipe2
已经将Packet(4):第4已经写入pipe写入到了pipe2
Packet(1):第1已经写入pipe已经进入了数据池
Packet(2):第2已经写入pipe已经进入了数据池
Packet(3):第3已经写入pipe已经进入了数据池
Packet(4):第4已经写入pipe已经进入了数据池
目录结构如下:
代码在我的资源中,如果需要请下载