设计模式--装饰模式

来源:互联网 发布:ppt软件免费版下载 编辑:程序博客网 时间:2024/06/04 23:58

装饰模式是属于结构型的设计模式。装饰模式的定义:
动态给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。
装饰模式的结构图如下:
结构图
适用性:
1 在不影响其他对象的情况下,以动态,透明的方式给单个对象添加职责
2 处理那些可以撤销的职责
3 当不能采用生成子类的方式进行扩充时。一种情况是,可能有大量的对立的扩展,为支持每一种组合将产生大量的子类,使得子类爆炸性的增长。另一种情况是因为类定义被隐藏,或类定义不能用于生成子类。
协作
Decorator将请求转发给它的Component对象,并有可能在转发请求前后执行以下附加的动作
优点
1 比静态继承更加灵活,使用Decorator可以很容易的重复添加一个特性。如在TextView添加两个边框时,仅需添加两个边框对象。
2 避免在层次架构高层的类有太多的特性
缺点
1 Decorator与Component不一样,从对象的标示观点来看,一个被修饰的组件与这个组件是有区别的,因此使用装饰时不应该依赖对象标识
2 有许多小对象
注意
1 接口的一致性 ,装饰对象的接口与它所装饰的Component的接口是一致的
2 省略抽象的Decorator类
3 保持Component类的简单性
4 改变对象的外壳与对象的内核

// Decorator.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "Person.h"#include "TShirts.h"#include "BigTrouser.h"int _tmain(int argc, _TCHAR* argv[]){    Person* person = new Person("jaime");    BigTrouser* bt = new BigTrouser();    TShirts* ts = new TShirts();    bt->Decorate(person);    ts->Decorate(bt);    ts->show();    return 0;}/************************************************************************          @fileName:Person.h           @function: 装饰模式的需要装饰的对象          @author: jaime          @ver: 1.0.0************************************************************************/#pragma once#include <string>using namespace std;class Person{public:    Person();    Person(string name);    ~Person();    virtual void show();private:    string m_name;};#include "Person.h"#include <iostream>using namespace std;Person::Person(string name):m_name(name){}Person::Person(){}Person::~Person(){}void Person::show(){    cout << "装饰的" << m_name << endl;}/************************************************************************         @fileName:Finery.h          @function: 装饰模式的抽象组件类         @author: jaime         @ver: 1.0.0************************************************************************/#pragma once#include "Person.h"class Finery : public Person{public:    Finery();    ~Finery();    void Decorate(Person* person);    virtual void show();private:protected:    Person* m_person;};#include "Finery.h"Finery::Finery():m_person(nullptr){}Finery::~Finery(){}void Finery::Decorate(Person* person){    m_person = person;}void Finery::show(){    if (m_person != nullptr)    {        m_person->show();    }}/************************************************************************           @fileName:BigTrouser.h            @function: 装饰模式的具体组件类           @author: jaime           @ver: 1.0.0************************************************************************/#include "Finery.h"class BigTrouser : public Finery{public:    BigTrouser();    ~BigTrouser();    virtual void show();private:};#include "BigTrouser.h"#include <iostream>using namespace std;BigTrouser::BigTrouser(){}BigTrouser::~BigTrouser(){}void BigTrouser::show(){    cout << "垮裤" << endl;    Finery::show();}/************************************************************************         @fileName:TShirts.h          @function: 装饰模式的具体组件类         @author: jaime         @ver: 1.0.0************************************************************************/#pragma once#include "Finery.h"class TShirts : public Finery{public:    TShirts();    ~TShirts();    virtual void show();private:};#include "TShirts.h"#include <iostream>using namespace std;TShirts::TShirts(){}TShirts::~TShirts(){}void TShirts::show(){    cout << "大TTShirts" << endl;    Finery::show();}
0 0
原创粉丝点击