装饰模式(Decorate Mode) C和C++的实现

装饰模式

装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,这些功能可以再动态的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。

实现方式(UML类图)

实现代码

#include <stdio.h>
 
class Person
{
public:
       Person() : name(0){}
       Person(char* _name) : name(_name){}
      
       virtual void Show()
       {
              printf("装扮的%s",name);
       }
protected:
       char* name;
};
 
class Finery : public Person
{
public:
       Finery() : component(0){}
      
       void Decorate(Person* component)
       {
              this->component = component;
       }
      
       virtual void Show()
       {
              if(component) component->Show();
       }
protected:
       Person* component;
};
 
class TShirts : public Finery
{
public:
       virtual void Show()
       {
              printf("大T恤 ");
              __super::Show();
       }
};
 
class BigTrouser : public Finery
{
public:
       virtual void Show()
       {
              printf("跨裤 ");
              __super::Show();
       }
};
 
class Sneakers : public Finery
{
public:
       virtual void Show()
       {
              printf("破球鞋 ");
              __super::Show();
       }
};
 
class Suit : public Finery
{
public:
       virtual void Show()
       {
              printf("西装 ");
              __super::Show();
       }
};
 
class Tie : public Finery
{
public:
       virtual void Show()
       {
              printf("领带 ");
              __super::Show();
       }
};
 
class LeatherShoes : public Finery
{
public:
       virtual void Show()
       {
              printf("皮鞋 ");
              __super::Show();
       }
};
 
int main()
{
       Person* xc = new Person("小菜");
      
       printf("第一种装扮:\n");
      
       Sneakers* pqx = new Sneakers();
       BigTrouser* kk = new BigTrouser();
       TShirts* dtx = new TShirts();
      
       pqx->Decorate(xc);
       kk->Decorate(pqx);
       dtx->Decorate(kk);
       dtx->Show();
      
       printf("\n第二种装扮:\n");
      
       LeatherShoes* px = new LeatherShoes();
       Tie* ld = new Tie();
       Suit* xz = new Suit();
      
       px->Decorate(xc);
       ld->Decorate(px);
       xz->Decorate(ld);
       xz->Show();
      
       delete xc;
      
       delete pqx;
       delete kk;
       delete dtx;
      
       delete px;
       delete ld;
       delete xz;
       return 0;
}
以上内容转自:http://www.cppblog.com/lwch/archive/2011/05/05/145756.aspx

对比《系统程序员成长计划》中关于装饰模式linux pthread locker C的描述

这里通过Finery这个对象中的

Decorate方法对Person对象进行装饰,扩展了Virtual函数show。

《系统》中的locker.h中的Locker中的locker_lock、locker_unlock、

locker_destroy三个函数类似这里的virtual show,

而第一次实现的pthread_locker则类似这里的Person对象。

但《系统》中实际是没有Finery这一层的,直接pthread_nest_locker中

就对pthread_locker进行了装饰。C++/C的实现区别,细细品味。

如果《系统》的Locker要用C++实现,这里的Person要用C实现,思路?

#ifndef _TYPEDEF_H
#define _TYPEDEF_H

typedef enum Ret
{
	RET_OK,
	RET_FAIL,
	RET_INVALID_PARAMS,
	RET_OOM
};

#define return_if_fail(p) if(!(p)) \
	{ printf("%s:%d Warning: "#p" fail.\n", \
	__func__, __LINE__); return; }

#define return_val_if_fail(p, ret) if(!(p)) \
	{ printf("%s:%d Warning: "#p" fail.\n", \
	__func__, __LINE__); return (ret); }

#endif /*_TYPEDEF_H*/

#include "typedef.h"

#ifndef _LOCKER_H
#define _LOCKER_H

class Locker
{
public:
	virtual Ret lock() = 0;
	virtual Ret unlock() = 0;
};

#endif /*_LOCKER_H*/

#include "locker.h"

#ifndef _LOCKER_THREAD_H
#define _LOCKER_THREAD_H

typedef int (*TaskSelfFunc)(void);

class LockerThread : public Locker
{
private:
	pthread_mutex_t mutex;

public:
	LockerThread()
	{
		pthread_mutex_init(&mutex, NULL);
	}
	virtual Ret lock();
	virtual Ret unlock();
};

class LockerNest : public Locker
{
private:
	LockerThread* p_real_lock;
	int owner;
	int refcount;
	TaskSelfFunc task_self;

public:
	LockerNest(Locker* locker_thread, TaskSelfFunc task_self):
		owner(0), refcount(0), locker_thread(locker_thread),
		task_self(task_self)
	{
		
	}
	virtual Ret lock();
	virtual Ret unlock();
};


#endif /*_LOCKER_PTHREAD_H*/


#include "locker_thread.h"

Ret LockerThread::lock()
{
}

Ret LockerThread::unlock()
{
}

Ret LockerNest::lock()
{
}

Ret LockerNest::unlock()
{
}

Finery 这一层还是要加上,这个对象在C语言版本中该如何实现?



posted @ 2011-08-23 13:04  $逝水无痕$  阅读(240)  评论(0编辑  收藏  举报