现在的位置: 首页 > 综合 > 正文

decorator模式—C++

2013年04月29日 ⁄ 综合 ⁄ 共 3896字 ⁄ 字号 评论关闭
感谢下面两篇文章的作者,从第一个例子再看第二个,很清晰!代码略有改动,只是为自己以后看起来方便,请见谅!

原文1:http://blog.csdn.net/dylgsy/article/details/876323

原文2:http://www.cnblogs.com/bastard/archive/2012/02/02/2336150.html  

例子1:// 定义基本接口类,设计模式的通用手法了
class CChildComponent
{
public:
 // 小孩吃饭
 virtual void Eat() = 0;
};

class CChild: public CChildComponent
{
public:
 
 virtual void Eat()
 {
  cout << "我吃饭了。" << endl;
 }
};

// 如果小孩想在吃饭前先吃个苹果,加个修饰类,这个类和小孩类派生于相同父类
class CDecorator: public CChildComponent
{
public:
 CDecorator(CChildComponent *childCom)
 {
  _child = childCom;
 }
 virtual void Eat()
 {
     _child->Eat();
 }

protected:
 CChildComponent *_child;
};

// 具体化修饰类
class CChildDecorator: public CDecorator
{
public:
 CChildDecorator(CChildComponent *childCom):CDecorator(childCom)
 {
 }
 virtual void Eat()
 {
  cout << "我先吃个苹果." << endl;
  CDecorator::Eat();
 }
};

void main()
{

 CChild child;
 CChildDecorator childDec(&child);
 childDec.Eat();
}

结果:我先吃个苹果

            我吃饭了

-----------------------------------------------------------------------------------------------------------

例子2:

#include <iostream.h>
#include <memory.h>

/*----------------------------------------------------------------*/
/* class VisualComponent                                          */
/*----------------------------------------------------------------*/
class VisualComponent
{
public:
    VisualComponent(){}
    virtual void Draw() = 0;
};

/*----------------------------------------------------------------*/
/* class TextView                                                 */
/*----------------------------------------------------------------*/
class TextViewControl : public VisualComponent
{
#define CONTENT_LEN 100
public:
    TextViewControl(char* str, int len)
    {
        memset(m_content,0,CONTENT_LEN);
        memcpy(m_content,str,len);
    }
    virtual void Draw()
    {
        cout<<" TextViewControl Draw con"<<endl;
    }
private:
    char m_content[CONTENT_LEN];
};

/*----------------------------------------------------------------*/
/* class Decorator                                                */
/*----------------------------------------------------------------*/
class Decorator: public VisualComponent
{
public:
    Decorator(VisualComponent* component)
    {
        m_componnet = component;
    }
    virtual void Draw()
    {
        m_componnet->Draw();
        cout<<" I am Decorator "<<endl;
    }
private:
    VisualComponent* m_componnet;
};

/*----------------------------------------------------------------*/
/* class BoarderDecorator                                         */
/*----------------------------------------------------------------*/
class BoarderDecorator: public Decorator
{
public:
    BoarderDecorator(VisualComponent* component, int boarderWidth):Decorator(component)
    {
        m_boarderWidth = boarderWidth;
    }
    virtual void Draw()
    {
        Decorator::Draw();
        cout<<" I am BoarderDecorator "<<endl;
        
        this->DrawBorder();
    }
protected:
    void DrawBorder()
    {
        cout<<" DrawBorder m_boarderWidth = "<<m_boarderWidth<<endl;
    }
private:
    int m_boarderWidth;
};

/*----------------------------------------------------------------*/
/* class ScrollDecorator                                         */
/*----------------------------------------------------------------*/
class ScrollDecorator: public Decorator
{
public:
    ScrollDecorator(VisualComponent* component, int scrollHeight):Decorator(component)
    {
        m_scrollHeight = scrollHeight;
    }
    virtual void Draw()
    {
        Decorator::Draw();
        cout<<" I am ScrollDecorator "<<endl;
        
        this->DrawScroll();
    }
protected:
    void DrawScroll()
    {
        cout<<" DrawScroll m_scrollHeight = "<<m_scrollHeight<<endl;
    }
private:
    int m_scrollHeight;
};

---------main'-----------------------

int main()
{
    char str[] = "TextViewControl";

    TextViewControl* textView = new          TextViewControl(str,strlen(str));
    ScrollDecorator* scroll = new ScrollDecorator(textView,100);
    BoarderDecorator* board = new BoarderDecorator(scroll,200);

    board->Draw();

    return 0;
}

结果:

 TextViewControl Draw con
 I am Decorator
 I am ScrollDecorator
 m_scrollHeight = 100
 I am Decorator
 I am BoarderDecorator
 m_boarderWidth = 200

抱歉!评论已关闭.