当前位置:编程学习 > C/C++ >>

设计模式(5)-装饰模式(Decorator)

【描述】不修改原代码的结构,通过装饰器给代码增加新功能。

【UML图】

 \

图1 UML图

(1) 原始代码为Component类,提供了operation操作;

(2) 装饰器为Decorator类,提供了扩展的operation功能;

(3) 注意与模板模式(设计模式(1)-模板模式(Template))的区别。

 

【示例代码】

component.h

[html]
#ifndef COMPONENT_H 
#define COMPONENT_H 
 
class Component 

public: 
    Component(); 
 
public: 
    virtual void operation(); 
}; 
 
#endif // COMPONENT_H 
#ifndef COMPONENT_H
#define COMPONENT_H

class Component
{
public:
    Component();

public:
    virtual void operation();
};

#endif // COMPONENT_H
 

component.cpp

[html] view plaincopyprint?#include <QDebug> 
#include "component.h" 
 
Component::Component() 

    qDebug()<<"construct Component"; 

 
void Component::operation() 

    qDebug()<<"Base Function"; 

#include <QDebug>
#include "component.h"

Component::Component()
{
    qDebug()<<"construct Component";
}

void Component::operation()
{
    qDebug()<<"Base Function";
}
 

decorator.h

[html]
#ifndef DECORATOR_H 
#define DECORATOR_H 
 
#include "component.h" 
 
class Decorator : public Component 

public: 
    Decorator(Component component); 
 
private: 
    Component component; 
 
public: 
    void operation(); 
}; 
 
#endif // DECORATOR_H 
#ifndef DECORATOR_H
#define DECORATOR_H

#include "component.h"

class Decorator : public Component
{
public:
    Decorator(Component component);

private:
    Component component;

public:
    void operation();
};

#endif // DECORATOR_H
 

decorator.cpp

[html]
#include <QDebug> 
#include "decorator.h" 
 
Decorator::Decorator(Component component) 

    qDebug()<<"construct Decorator"; 
    this->component = component; 

 
void Decorator::operation() 

    component.operation(); 
    qDebug()<<"Extend Function"; 

#include <QDebug>
#include "decorator.h"

Decorator::Decorator(Component component)
{
    qDebug()<<"construct Decorator";
    this->component = component;
}

void Decorator::operation()
{
    component.operation();
    qDebug()<<"Extend Function";
}
 

main.cpp

[html]
#include "component.h" 
#include "decorator.h" 
 
int main(void) 

    Component component; 
    component.operation(); 
    Decorator decorator(component); 
    decorator.operation(); 
 
    return 0; 

#include "component.h"
#include "decorator.h"

int main(void)
{
    Component component;
    component.operation();
    Decorator decorator(component);
    decorator.operation();

    return 0;
}
 

【运行结果】

[html]
construct Component  
Base Function  
construct Component  
construct Component  
construct Decorator  
Base Function  
Extend Function  
construct Component
Base Function
construct Component
construct Component
construct Decorator
Base Function
Extend Function 

【结果分析】

借助装饰器,在没有改变原始代码的前提下,给代码增加了新功能。

 

【实例剖析】

设计模式(1)-模板模式(Template)一文介绍了一种改进的Qt嵌入式输入法,下面利用装饰模式对代码进行改写。先看UML图:

 

图2

(1) 原始代码为QLineEdit类;

(2) 装饰器为QLineEditWithIM类,提供了installIM()方法。

 

【代码清单】

下面仅贴出修改的部分,详细代码请参考Qt输入法设计(嵌入式)以及设计模式(1)-模板模式(Template)一文。

qlineeditwithim.h

[html]
#ifndef QLINEEDITWITHIM_H 
#define QLINEEDITWITHIM_H 
 
#include <QLineEdit> 
#include "inputmethod.h" 
 
class QLineEditWithIM : public QLineEdit 

public: 
    QLineEditWithIM(QLineEdit *lineEdit); 
    ~QLineEditWithIM(); 
 
private: 
    QLineEdit *lineEdit; 
    InputMethod *im; 
 
public: 
    void installIM(); 
}; 
 
#endif // QLINEEDITWITHIM_H 
#ifndef QLINEEDITWITHIM_H
#define QLINEEDITWITHIM_H

#include <QLineEdit>
#include "inputmethod.h"

class QLineEditWithIM : public QLineEdit
{
public:
    QLineEditWithIM(QLineEdit *lineEdit);
    ~QLineEditWithIM();

private:
    QLineEdit *lineEdit;
    InputMethod *im;

public:
    void installIM();
};

#endif // QLINEEDITWITHIM_H
 

qlineeditwithim.cpp

[html]
#include "qlineeditwithim.h" 
 
QLineEditWithIM::QLineEditWithIM(QLineEdit *lineEdit) 

//#ifdef Q_WS_QWS 
    im = new InputMethod; 
    this->lineEdit = lineEdit; 
//#endif 

 
QLineEditWithIM::~QLineEditWithIM() 

    delete im; 

 
void QLineEditWithIM::installIM() 

//#ifdef Q_WS_QWS 
    installEventFilter(im); 
    connect(im->keyboard,SIGNAL(setvalue(QString)),this,SLOT(setText(QString))); 
//#endif 

#include "qlineeditwithim.h"

QLineEditWithIM::QLineEditWithIM(QLineEdit *lineEdit)
{
//#ifdef Q_WS_QWS
    im = new InputMethod;
    this->lineEdit = lineEdit;
//#endif
}

QLineEditWithIM::~QLineEditWithIM()
{
    delete im;
}

void QLineEditWithIM::installIM()
{
//#ifdef Q_WS_QWS
 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,