批处理与管道-过滤器
三種典型的數(shù)據(jù)流風(fēng)格
§ Batch Sequential (批處理)
§ Pipe-and-Filter (管道-過(guò)濾器)
§ Process Control(過(guò)程控制,3.7)
批處理風(fēng)格:
直觀結(jié)構(gòu)
批處理風(fēng)格-基本定義
§每個(gè)處理步驟是一個(gè)獨(dú)立的程序
§每一步必須在前一步結(jié)束后才能開(kāi)始
§數(shù)據(jù)必須是完整的,以整體的方式傳遞
§典型應(yīng)用:
–傳統(tǒng)的數(shù)據(jù)處理
–程序編譯/CASE(computer aided software engineering)工具
批處理風(fēng)格-基本構(gòu)成
§基本組件:獨(dú)立的應(yīng)用程序
§連接件:某種類型的媒質(zhì)(magnetic tape)
§表達(dá)拓?fù)浣Y(jié)構(gòu):連接件定義了相應(yīng)的數(shù)據(jù)流圖
§每一步驟必須在前一步驟完全結(jié)束之后方能開(kāi)始
程序:#include <iostream>
#include<fstream>
using namespace std;
bool writeFile(constchar* strPath,char strConcern[],int length)
{
??? ofstream outfile;
??? outfile.open(strPath,ios::app);
??? if(!outfile)
??? {
??????? cerr<<"openerror!"<<endl;
??????? return false;
??? }
??? outfile.write(strConcern,length);
??? outfile.close();
??? return true;
}
bool ReadFile(constchar * strPath,char strConcern[],int length)
{
??? ifstream infile;
??? infile.open(strPath,ios::binary);
??? if(!infile)
??? {
??????? cerr<<"openerror"<<endl;
??????? return false;
??? }
??? while(!infile.eof())
??? {
??????? infile.read(strConcern,length);
??? }
??? return true;
}
bool factory1()
{
???? char?str[20]="helloworld";
???? charstrpath[20]="E:\\text1.txt";
???? bool b=writeFile(strpath,str,sizeof(str));
???? if(!b)
???? {
???????? return false;
???? }
???? return true;
}
bool factory2()
{
??? char str[20];
??? char strpath[20]="E:\\text1.txt";
??? bool b=ReadFile(strpath,str,sizeof(str));
??? cout<<str<<endl;
??? cout<<str<<endl;
??? charstrpath1[20]="E:\\text2.txt";
??? boolbw=writeFile(strpath1,str,sizeof(str));
??? bw=writeFile(strpath1,"第二道工序",sizeof(str));
??? if(!b||!bw)
??? {
??????? return false;
??? }
??? return true;
}
int main()
{
??? char strpath[40]="E:\\text2.txt";
??? char str[20];
??? bool b1=factory1();
??? bool b2=factory2();
??? bool b=ReadFile(strpath,str,sizeof(str));
??? if(b&&b1&&b2)
??? {
?
??????? cout<<str<<endl;
??? }
??? //cout << "Hello world!"<< endl;
return 0;
?
管道過(guò)濾器風(fēng)格:
組件:過(guò)濾器,處理數(shù)據(jù)流
–一個(gè)過(guò)濾器封裝了一個(gè)處理步驟
–數(shù)據(jù)源點(diǎn)和數(shù)據(jù)終止點(diǎn)可以看作是特殊的過(guò)濾器過(guò)濾器對(duì)輸入流進(jìn)行處理、轉(zhuǎn)換,處理后的結(jié)果在輸出端流出。§每個(gè)組件都有輸入/輸出集合,組件在輸入處讀取數(shù)
據(jù)流,經(jīng)過(guò)內(nèi)部處理,在輸出處生成數(shù)據(jù)流。
連接件:管道,連接一個(gè)源和一個(gè)目的過(guò)濾器
–轉(zhuǎn)發(fā)數(shù)據(jù)流
–數(shù)據(jù)可能是ASCII字符形成的流連接件位于過(guò)濾器之間,起到信息流的導(dǎo)管的作
用,被稱為管道。接件就象是數(shù)據(jù)流傳輸?shù)墓艿?#xff0c;將一個(gè)過(guò)濾器的輸出傳到另一過(guò)濾器的輸入。
管道過(guò)濾器優(yōu)點(diǎn):由于每個(gè)組件的行為不受其他組件的影響,整個(gè)系統(tǒng)的行為易于理解
§系統(tǒng)中的組件具有良好的隱蔽性和高內(nèi)聚、低耦合的特點(diǎn);
–支持軟件復(fù)用:
?允許設(shè)計(jì)者將整個(gè)系統(tǒng)的輸入/輸出行為看成是
多個(gè)過(guò)濾器的行為的簡(jiǎn)單合成;
?只要提供適合在兩個(gè)過(guò)濾器之間傳送的數(shù)據(jù),任何兩個(gè)過(guò)濾器都可被連
接起來(lái);
–系統(tǒng)維護(hù)和增強(qiáng)系統(tǒng)性能簡(jiǎn)單:
?新的過(guò)濾器可以添加到現(xiàn)有系統(tǒng)中來(lái),
舊的可以被改進(jìn)的過(guò)濾器替換掉;
允許對(duì)一些如吞吐量、死鎖等屬性的分析;
§支持并行執(zhí)行:
–每個(gè)過(guò)濾器是作為一個(gè)單獨(dú)的任務(wù)完成,因此可與
其它任務(wù)并行執(zhí)行。
管道-過(guò)濾器風(fēng)格的缺點(diǎn):
§通常導(dǎo)致進(jìn)程成為批處理的結(jié)構(gòu)
–這是因?yàn)殡m然過(guò)濾器可增量式地處理數(shù)據(jù),但它們
是獨(dú)立的,所以設(shè)計(jì)者必須將每個(gè)過(guò)濾器看成一個(gè)
完整的從輸入到輸出的轉(zhuǎn)換;
§不適合處理交互的應(yīng)用
–當(dāng)需要增量地顯示改變時(shí),這個(gè)問(wèn)題尤為嚴(yán)重;
處理兩個(gè)獨(dú)立但又相關(guān)的數(shù)據(jù)流是可能會(huì)遇到困難
§在數(shù)據(jù)傳輸上沒(méi)有通用的標(biāo)準(zhǔn),每個(gè)過(guò)濾器都增加
了解析和合成數(shù)據(jù)的工作,這樣就導(dǎo)致了系統(tǒng)性能
下降,并增加了編寫(xiě)過(guò)濾器的復(fù)雜性。
–絕大部分處理時(shí)間消耗在格式轉(zhuǎn)換上(需要對(duì)數(shù)據(jù)傳
輸進(jìn)行特定的處理時(shí),會(huì)導(dǎo)致對(duì)于每個(gè)過(guò)濾器的解析
輸入和格式化輸出要做更多的工作,帶來(lái)系統(tǒng)復(fù)雜性
的上升)
過(guò)濾器與批處理比較:
程序:管道-過(guò)濾器軟件體系結(jié)構(gòu)
(1)在dos提示符下輸入下面的命令:
dir | more
使得當(dāng)前目錄列表在屏幕上逐屏顯示。
dir的輸出的是整個(gè)目錄列表,它不出現(xiàn)在屏幕上而是由于符號(hào)“|”的規(guī)定,成為下一個(gè)命令more的輸入,more命令則將其輸入一屏一屏地顯示,成為命令行的輸出。
(2)Java I/O流中的管道流類PipedInputStream和PipedOutputStream可以方便地實(shí)現(xiàn)管道-過(guò)濾器體系結(jié)構(gòu),這兩個(gè)類的實(shí)例對(duì)象要通過(guò)connect方法連接。
下面程序的功能是sender發(fā)送“Hello,receiver! I`m sender”給receiver,然后receiver接受后顯示出來(lái)并且在前面加上“the following is from sender”的信息。管道流內(nèi)部在實(shí)現(xiàn)時(shí)還有大量的對(duì)同步數(shù)據(jù)的處理,管道輸出流和管道輸入流執(zhí)行時(shí)不能互相阻塞,所以一般要開(kāi)啟獨(dú)立線程分別執(zhí)行,順便復(fù)習(xí)了多線程操作。
import java.io.*;
import java.util.*;
public class TestPiped{
public static void main(String [] args){
sender s = new sender();
receiver r = new receiver();
???????PipedOutputStream out = s.getOut();
???????PipedInputStream in = r.getIn();
??? ????try{
????????????in.connect(out);
????????????s.start();
????????????r.start();
???????}catch(Exception e){
????????????e.printStackTrace();
???????}
??? }
}
?
class sender extends Thread {
???PipedOutputStream out = new PipedOutputStream();
??? publicPipedOutputStream getOut(){
????????return out;
??? }
???public void run() {
?? ???????String str = "Hello,receiver ! I`msender\n";
?????????try {
???????????????out.write(str.getBytes());
???????????????out.close();
?? ???????}catch(Exception e) {
???????????????e.printStackTrace();
?????????}
??? }
}
?
class receiver extends Thread {
???PipedInputStream in = new PipedInputStream();
???public PipedInputStream getIn() {
??????return in;
??? }
???public void run(){
???????byte [] buf = new byte[1024];
???????try {
????????????int len = in.read(buf);
????????????System.out.println("the following is from sender:\n"+newString(buf,0,len));
????????????in.close();
???????}catch(Exception e) {
????????????e.printStackTrace();
???????}
? ??}
}
程序的執(zhí)行結(jié)果:
??? thefollowing is from sender:
Hello,receiver ! I`m sender
?
總結(jié)
以上是生活随笔為你收集整理的批处理与管道-过滤器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Arduino旋转编码器
- 下一篇: multisim 高低电平点亮灯证明