离弦之Ray

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  55 Posts :: 0 Stories :: 33 Comments :: 0 Trackbacks

#

Head first design patterns 一直被束之高阁,总算考完期末考试。然而,我却又要开始复习考研的东西了,整天看数学看的十分头晕,把这本书重新拿来看,权当连环画来看。

以前已经看过两章了,实在太过久远,只得重温一下。

前面的 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 例子完美地体现了以上三个 Principles

一开始 Duck 类是这样的,里面有三个方法: quack(), swim() display(), 其它一些特殊的 Duck 子类继承这个父类,并重载 display 方法去显示各种不同种类的 Duck

 

现在需求变更,需要让一些 Duck 能够有飞的能力,理所当然地,在父类中加上了 fly() 这个方法。但一些不能飞的 Duck 类却同时拥有了 fly() 方法,这显然是不对的。

可能有人提出解决的方法是重载那些不需要 fly() 方法的 duck 类的 fly() ,让这个方法什么也不做,但你有没有想过如果这么处理,以后再加一些不需要一些方法的子类是不是很繁琐?

也许又有人想到了,把 fly() quack() 提取出来,编程 interface 。如果某个子类需要这个能力可以去 implement 这个 interface

有没有想过后果?每一个子类都要重写 fly() quack(),OO 的代码重用的特性荡然无存。

 

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

书里面给出了答案。

fly() quack() 两个功能提取出来这个思路是对的。这里体现了第一个 Principle 。首先声明 FlyBehavior() QuackBehavior() 两个 interface ,然后实现各种 fly quack ,比如 FlyWithWings, FlyNoWay Quack, Squeak 等等。这里体现了第二个 Principle

 

现在的 Duck 父类已经变了,里面有两个私有变量 FlyBehavior fb, QuackBehavior qb

Duck 父类甚至可以直接声明成 Abstract 类,当有子类继承它的时候,可以在构造函数里给 fb qb 初始化的时候直接赋给它需要的 fly quack 种类。这里体现了第三个 Principle

 

小小的一个例子已经分析的那么专业,让我受益匪浅。好书!

 

posted @ 2006-07-06 14:17 离弦之ray的技术天空 阅读(292) | 评论 (1)编辑 收藏

首先要对OSI七层结构和TCP/IP四层结构要清楚。
OSI七层结构从上到下为
Application
Presentation
Session
Transport
Network
Data Link

Physical

TCP/IP四层结构为
把上面三层合为process层,把下面两层合为Hardware层得到
Process
Transport
Network
Hardware


协议所提供的各种服务:

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,需要在联系前首先要建立一条连接。
connectionless的典型代表是UDP,不需要事先建立连接。


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

保证所有封包以正确的顺序分发

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.

保证发送端发送数据的速度不超过接收端接收数据的速度。
 

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的技术天空 阅读(189) | 评论 (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是命名的,一些没有亲缘关系的process能共享它。

Normally, opening a FIFO for read or write, it blocks until another process opens it for write or read. Write and read必须一一对应。

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函数

所以只能这样设定

flags= fcntl (fd, F_GETFL,0);

flag|=O_NONBLOCK;

fcntl =(fd,F_SETFL,flags);


下面的表很重要,要看清下面的前提操作和当前操作,主要比较了Blocking和O_NONBLOCK条件下的区别

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的技术天空 阅读(233) | 评论 (0)编辑 收藏

父进程和子进程创建双向管道的实际步骤

创建管道 1(fd1[0] fd1[1]) 和管道 2(fd2[0] fd2[1])

Fork

父进程关闭管道 1 的读出端( fd1[0]) [MS1]  

父进程关闭管道 2 的写入端( fd2[1]

子进程关闭管道 1 的写入端( fd1[1]

子进程关闭管道 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的技术天空 阅读(255) | 评论 (0)编辑 收藏

要考试啦,一边复习,一边记录……

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() 去移动当前的 offset 而其中的 function 参数有以下几种: F_ULOCK, F_LOCK, F_TEST, F_TLOCK

posted @ 2006-06-14 20:07 离弦之ray的技术天空 阅读(274) | 评论 (0)编辑 收藏

仅列出标题
共11页: First 上一页 3 4 5 6 7 8 9 10 11 下一页