離弦之Ray

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            55 Posts :: 0 Stories :: 33 Comments :: 0 Trackbacks

          #

          Head first design patterns 一直被束之高閣,總算考完期末考試。然而,我卻又要開始復(fù)習(xí)考研的東西了,整天看數(shù)學(xué)看的十分頭暈,把這本書重新拿來看,權(quán)當(dāng)連環(huán)畫來看。

          以前已經(jīng)看過兩章了,實在太過久遠,只得重溫一下。

          前面的 Introduction 其實是非常有意思的,在這先不贅述了。

          ?

          介紹的第一個 Pattern Strategy Pattern 。

          通過一個 duck 的例子,在講述 Strategy Pattern 的過程中引出了三個 Design Principles

          它們是:

          1.????????????? Identify the aspects of your application that vary and separate them from what stays the same.

          2.????????????? Program to an interface, not an implementation.

          3.????????????? Favor composition over inheritance.

          Duck 例子完美地體現(xiàn)了以上三個 Principles 。

          一開始 Duck 類是這樣的,里面有三個方法: quack(), swim() display(), 其它一些特殊的 Duck 子類繼承這個父類,并重載 display 方法去顯示各種不同種類的 Duck 。

          ?

          現(xiàn)在需求變更,需要讓一些 Duck 能夠有飛的能力,理所當(dāng)然地,在父類中加上了 fly() 這個方法。但一些不能飛的 Duck 類卻同時擁有了 fly() 方法,這顯然是不對的。

          可能有人提出解決的方法是重載那些不需要 fly() 方法的 duck 類的 fly() ,讓這個方法什么也不做,但你有沒有想過如果這么處理,以后再加一些不需要一些方法的子類是不是很繁瑣?

          也許又有人想到了,把 fly() quack() 提取出來,編程 interface 。如果某個子類需要這個能力可以去 implement 這個 interface 。

          有沒有想過后果?每一個子類都要重寫 fly() quack(),OO 的代碼重用的特性蕩然無存。

          ?

          那什么才是最好的解決之道呢???

          書里面給出了答案。

          fly() quack() 兩個功能提取出來這個思路是對的。這里體現(xiàn)了第一個 Principle 。首先聲明 FlyBehavior() QuackBehavior() 兩個 interface ,然后實現(xiàn)各種 fly quack ,比如 FlyWithWings, FlyNoWay ; Quack, Squeak 等等。這里體現(xiàn)了第二個 Principle 。

          ?

          現(xiàn)在的 Duck 父類已經(jīng)變了,里面有兩個私有變量 FlyBehavior fb, QuackBehavior qb 。

          Duck 父類甚至可以直接聲明成 Abstract 類,當(dāng)有子類繼承它的時候,可以在構(gòu)造函數(shù)里給 fb qb 初始化的時候直接賦給它需要的 fly quack 種類。這里體現(xiàn)了第三個 Principle 。

          ?

          小小的一個例子已經(jīng)分析的那么專業(yè),讓我受益匪淺。好書!

          ?

          posted @ 2006-07-06 14:17 離弦之ray的技術(shù)天空 閱讀(313) | 評論 (1)編輯 收藏

          首先要對OSI七層結(jié)構(gòu)和TCP/IP四層結(jié)構(gòu)要清楚。
          OSI七層結(jié)構(gòu)從上到下為
          Application
          Presentation
          Session
          Transport
          Network
          Data Link

          Physical

          TCP/IP四層結(jié)構(gòu)為
          把上面三層合為process層,把下面兩層合為Hardware層得到
          Process
          Transport
          Network
          Hardware


          協(xié)議所提供的各種服務(wù):

          Connection-oriented (virtual circuit) or connectionless– Connection-oriented requires the protocol to establish a logical connection before communicating. Data can then be transferred until the connection is terminated. With the TCP/IP protocol suite, TCP is connection-oriented and UDP is connectionless.


          Connection-oriented的典型代表是TCP,需要在聯(lián)系前首先要建立一條連接。
          connectionless的典型代表是UDP,不需要事先建立連接。


          Sequencing – makes sure that packets are delivered in the same order they were sent.

          保證所有封包以正確的順序分發(fā)

          Error control –handles data corruption and packet loss. Requires the receiver to acknowledge the sender and to discard duplicate the packet if an ACK is lost and the packet is then resent.

          ?

          Flow control – makes sure the sender does not transmit data at a rate higher than the receiver can process the data.

          保證發(fā)送端發(fā)送數(shù)據(jù)的速度不超過接收端接收數(shù)據(jù)的速度。
          ?

          Byte steam or messages – a byte stream is a series of bytes rather than a series of messages. The higher layer protocols must then organize the bytes according to the application requirements.

          ?

          Full-duplex or half-duplex – Full-duplex is bi-directional simultaneously. Half-duplex is uni-directional at a time. The direction varies with time.



          ?
          posted @ 2006-06-20 23:57 離弦之ray的技術(shù)天空 閱讀(197) | 評論 (0)編輯 收藏

          A FIFO is similar to a pipe. A FIFO is a one-way flow of data (First In First Out). FIFOs have a name, so unrelated processes can share the FIFO. FIFO is a named pipe.

          FIFO和PIPE基本差不多,但FIFO是命名的,一些沒有親緣關(guān)系的process能共享它。

          Normally, opening a FIFO for read or write, it blocks until another process opens it for write or read. Write and read必須一一對應(yīng)。

          A read gets as much data as it requests or as much data as the FIFO has, whichever is less.

          A write to a FIFO is atomic, as long as the write does not exceed the capacity of the FIFO. The capacity is at least 4k.


          How to set flags.

          writefd = open (FIFO1, O_WRONLY|O_ONOBLOCK,0);

          但是pipe沒有open函數(shù)

          所以只能這樣設(shè)定

          flags= fcntl (fd, F_GETFL,0);

          flag|=O_NONBLOCK;

          fcntl =(fd,F_SETFL,flags);


          下面的表很重要,要看清下面的前提操作和當(dāng)前操作,主要比較了Blocking和O_NONBLOCK條件下的區(qū)別

          Operation

          Existing opens of pipe or FIFO

          Blocking (default)

          O_NONBLOCK set

          Open FIFO for reading

          FIFO open for writing

          Returns OK

          Returns OK

          FIFO not open for writing

          Blocks until FIFO is opened for writing

          Returns OK

          Open FIFO for writing

          FIFO open for reading

          Returns OK

          Returns OK

          FIFO not open for reading

          Blocks until FIFO is opened for reading

          Returns an error of ENXIO

          Read empty pipe or FIFO

          Pipe or FIFO open for writing

          Blocked until there is data or the pipe or FIFO is closed for writing

          Return an error of EAGAIN

          Pipe or FIFO not open for writing

          Read returns 0 (EOF)

          Read return 0 (EOF)

          Write to pipe or FIFO

          Pipe or FIFO open for reading

          Return ok

          Return ok

          Pipe or FIFO is full

          Blocked until space is available, then write data

          Returns an error of EAGAIN

          Pipe or FIFO not open for reading

          SIGPIPE generated, write process terminated

          Returns an error of EPIPE



          posted @ 2006-06-20 23:42 離弦之ray的技術(shù)天空 閱讀(284) | 評論 (0)編輯 收藏

          父進程和子進程創(chuàng)建雙向管道的實際步驟

          創(chuàng)建管道 1(fd1[0] fd1[1]) 和管道 2(fd2[0] fd2[1])

          Fork

          父進程關(guān)閉管道 1 的讀出端( fd1[0]) [MS1] ?

          父進程關(guān)閉管道 2 的寫入端( fd2[1]

          子進程關(guān)閉管道 1 的寫入端( fd1[1]

          子進程關(guān)閉管道 2 的讀出端( fd2[0]


          下面是示例程序:

          #include “unpipe.h”

          void client(int, int) ,server(int, int);

          int main(int argc, char* argv[])

          {

          ?????? int pipe1[2],pipe[2];

          pid_t childpid;

          Pipe(pipe1);

          Pipe(pipe2);

          ?

          if((childpid=fork())==0)

          {

          ?????? Close(pipe1[1])?;

          ?????? Close(pipe2[0])?;

          ?

          ?????? server(pipe1[0],pipe2[1])?;

          ?????? exit(0)?;

          }

          close(pipe1[0])?;

          close(pipe2[1])?;

          client(pipe2[0],pipe1[1])?;

          ?

          waitpid(childpid,NULL,0)?;

          exit(0)?;

          }

          //////////////////client

          void client(int readfd, int writefd)

          {

          ?????? size_t len;

          ssize_t n;

          char? buff[MAXLINE];

          ?

          fgets(buff,MAXLINET,stdin);

          len = strlen(buff);

          if(buff[len-1]==’\n’)

          ?????? len--;

          write(writefd,buff,len);

          while((n=read(readfd,buff,MAXLINE))>0)

          ??????? write(STDOUT_FILENO,buff,n);

          }

          ///////////////////server

          void server(int readfd, int writefd)

          {

          ?????? int fd;

          ?????? ssize_t n;

          ?????? char buff[MAXLINE+1];

          ?

          ?????? if((n=read(readfd,buff,MAXLINE))==0)

          ????????? err_quit(“end-of –file while reading pathname”);

          ?????? if((fd =open(buff,O_RDONLY)<0)){

          sprintf(buff+n,sizeof(buff)-n,”can’t open, %s\n”,strerror(errno));

          n = strlen(buff);

          write(writefd,buff,n);

          }else{

          ?While((n=read(fd,buff,MAXLINE))>0)

          ??????? Write(writefd,buff,n);

          Close(fd);

          }

          }


          Properties of Pipe:

          1) Pipes do not have a name. For this reason, the processes must share a parent process. This is the main drawback to pipes. However, pipes are treated as file descriptors, so the pipes remain open even after fork and exec.

          2) Pipes do not distinguish between messages; they just read a fixed number of bytes. New line (\n) can be used to separate messages. A structure with a length field can be used for message containing binary data.

          3) Pipes can also be used to get the output of a command or to provide input to a command


          ?[MS1] 換句話說就是父進程對管道 1 只能寫,以下可以依此類推。

          posted @ 2006-06-14 20:14 離弦之ray的技術(shù)天空 閱讀(267) | 評論 (0)編輯 收藏

          要考試?yán)玻贿厪?fù)習(xí),一邊記錄……

          IPC
          有兩個功能 : Synchronization Message Passing ,其中 message Passing 有下面幾種形式 :Pipes, FIFOs, Message Queues, Shared Memory

          ?

          File or Record Locking

          int lockf (int fd, int function, long size) 其中 fd 是文件描述符 (file descripter) ; size 是鎖定的大小 [offset, offset+size] ,如果 size=0, 就表示文件余下的部分。可以用 lseek() 去移動當(dāng)前的 offset 。 而其中的 function 參數(shù)有以下幾種: F_ULOCK, F_LOCK, F_TEST, F_TLOCK

          posted @ 2006-06-14 20:07 離弦之ray的技術(shù)天空 閱讀(288) | 評論 (0)編輯 收藏

          僅列出標(biāo)題
          共11頁: First 上一頁 3 4 5 6 7 8 9 10 11 下一頁 
          主站蜘蛛池模板: 繁昌县| 朝阳区| 琼中| 清水河县| 萝北县| 宣化县| 伽师县| 铜山县| 临邑县| 托克逊县| 五家渠市| 甘德县| 若羌县| 翁牛特旗| 榆社县| 宜兴市| 吉林省| 秭归县| 乐清市| 城步| 罗甸县| 龙门县| 苏尼特右旗| 左权县| 英吉沙县| 四会市| 石楼县| 云龙县| 武陟县| 固原市| 济宁市| 酉阳| 泉州市| 宣汉县| 北京市| 安阳县| 南漳县| 平遥县| 兰考县| 宜良县| 宽甸|