Decorator(装饰模式)

1、定义:

       动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活。

2、结构图:

      

3、协作角色

    Component 定义一个对象的接口,可以给这些对象动态的增加职责。

    ConcreteComponent  定义一个对象,可以给这些对象添加一些职责。

    Decorator 维持一个Component对象的指针,并定义一个与Component 接口一致的接口。

    Component向组件添加职责

4、适用性

     在不影响其他对象的情况下,以动态,透明的方式给对象添加职责。

     处理那些可以撤销的职责。

5、实例

      linux串行子系统,其层次结构如下,

      (1) 关注UART或者其他硬件的驱动

      (2) tty驱动层,将上层驱动程序和硬件隔离

      (3) 线路规程,以特殊方式格式化从客户或者硬件上得到的数据,有助于复用底层代码

        这个子系统中,线路规程和装饰模式类似

       每个具体的线路规程完成结构体:

   

struct tty_ldisc_ops {
	int	magic;
	char	*name;
	int	num;
	int	flags;
	
	/*
	 * The following routines are called from above.
	 */
	int	(*open)(struct tty_struct *);
	void	(*close)(struct tty_struct *);
	void	(*flush_buffer)(struct tty_struct *tty);
	ssize_t	(*chars_in_buffer)(struct tty_struct *tty);
	ssize_t	(*read)(struct tty_struct * tty, struct file * file,
			unsigned char __user * buf, size_t nr);
	ssize_t	(*write)(struct tty_struct * tty, struct file * file,
			 const unsigned char * buf, size_t nr);	
	int	(*ioctl)(struct tty_struct * tty, struct file * file,
			 unsigned int cmd, unsigned long arg);
	long	(*compat_ioctl)(struct tty_struct * tty, struct file * file,
				unsigned int cmd, unsigned long arg);
	void	(*set_termios)(struct tty_struct *tty, struct ktermios * old);
	unsigned int (*poll)(struct tty_struct *, struct file *,
			     struct poll_table_struct *);
	int	(*hangup)(struct tty_struct *tty);
	
	/*
	 * The following routines are called from below.
	 */
	void	(*receive_buf)(struct tty_struct *, const unsigned char *cp,
			       char *fp, int count);
	void	(*write_wakeup)(struct tty_struct *);

	struct  module *owner;
	
	int refcount;
};


通过tty_register_ldisc注册到tty_ldiscs中,在应用层动态绑定到设备上负责按照指定的协议格式化数据。    

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值