<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    離弦之Ray

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

    #

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

    以前已經(jīng)看過(guò)兩章了,實(shí)在太過(guò)久遠(yuǎn),只得重溫一下。

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

    ?

    介紹的第一個(gè) Pattern Strategy Pattern

    通過(guò)一個(gè) duck 的例子,在講述 Strategy Pattern 的過(guò)程中引出了三個(gè) 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)了以上三個(gè) Principles

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

    ?

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

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

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

    有沒(méi)有想過(guò)后果?每一個(gè)子類都要重寫 fly() quack(),OO 的代碼重用的特性蕩然無(wú)存。

    ?

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

    書里面給出了答案。

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

    ?

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

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

    ?

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

    ?

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

    首先要對(duì)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ù)的速度不超過(guò)接收端接收數(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ù)天空 閱讀(193) | 評(píng)論 (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是命名的,一些沒(méi)有親緣關(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必須一一對(duì)應(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沒(méi)有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ù)天空 閱讀(277) | 評(píng)論 (0)編輯 收藏

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

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

    Fork

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

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

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

    子進(jìn)程關(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] 換句話說(shuō)就是父進(jìn)程對(duì)管道 1 只能寫,以下可以依此類推。

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

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

    IPC
    有兩個(gè)功能 : 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)當(dāng)前的 offset 而其中的 function 參數(shù)有以下幾種: F_ULOCK, F_LOCK, F_TEST, F_TLOCK

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

    僅列出標(biāo)題
    共11頁(yè): First 上一頁(yè) 3 4 5 6 7 8 9 10 11 下一頁(yè) 
    主站蜘蛛池模板: 日本一道一区二区免费看| 日本人成在线视频免费播放| 亚洲人成色4444在线观看| 亚洲国产亚洲片在线观看播放| 亚洲天堂一区二区三区四区| 亚洲成综合人影院在院播放| 亚洲成人网在线播放| 亚洲人成黄网在线观看| 亚洲av极品无码专区在线观看| 国产成人精品日本亚洲专| 亚洲精品美女网站| 亚洲欧美精品午睡沙发| 爱爱帝国亚洲一区二区三区| 污视频网站免费在线观看| 亚洲免费一区二区| 性无码免费一区二区三区在线| 91成人在线免费观看| 100000免费啪啪18免进| 夫妻免费无码V看片| 又粗又大又硬又爽的免费视频| 亚洲成年人啊啊aa在线观看| 久久久青草青青国产亚洲免观| 亚洲国产a∨无码中文777| 亚洲丝袜美腿视频| 亚洲精品第一国产综合野| 欧美色欧美亚洲另类二区| 亚洲一区二区三区免费| 久久99热精品免费观看牛牛| 国产又黄又爽又猛免费app| 男人的天堂亚洲一区二区三区 | 精品国产sm捆绑最大网免费站 | 偷自拍亚洲视频在线观看| eeuss影院免费92242部| 日本免费大黄在线观看| 女人与禽交视频免费看| 亚洲一区二区三区免费| 亚洲精品自拍视频| 野花视频在线官网免费1| 久久青草精品38国产免费| 午夜免费福利影院| 亚洲国产另类久久久精品小说|