裝飾模式(Decorator)目的是給對象添加職責,其結構圖如下:

假設現在有一個圖書館,存有大量的書或影碟,相對于書或影碟,其本身沒有出借服務,現在就可以借助裝飾模式為其添加出借功能,結構圖如下:

實現代碼:
//Items.h
class Items
{
public:
virtual ~Items();
virtual void Display() = 0;
protected:
Items();
};
//Items.cpp
#include "stdafx.h"
#include "Items.h"
Items::Items()
{
}
Items::~Items()
{
}
//Book.h
#include "Items.h"
#include <string>
class Book : public Items
{
public:
Book();
Book(std::string, int);
virtual ~Book();
void Display();
private:
std::string m_strName;
int m_nNum;
};
//Book.cpp
#include "stdafx.h"
#include "Book.h"
#include <iostream>
using namespace std;
Book::Book()
{
m_strName = "";
m_nNum = 0;
}
Book::Book(string strName, int nNum)
{
m_strName = strName;
m_nNum = nNum;
}
Book::~Book()
{
}
void Book::Display()
{
cout << "書名:" << m_strName << " 數量:" << m_nNum << endl;
}
//Video.cpp
#include "Items.h"
#include <string>
class Video : public Items
{
public:
Video();
Video(std::string, int, int);
virtual ~Video();
void Display();
private:
std::string m_strName;
int m_nNum;
int m_nTime;
};
//Video.cpp
#include "stdafx.h"
#include "Video.h"
#include <iostream>
using namespace std;
Video::Video()
{
m_strName = "";
m_nNum = 0;
m_nTime = 0;
}
Video::Video(string strName, int nNum, int nTime)
{
m_strName = strName;
m_nNum = nNum;
m_nTime = nTime;
}
Video::~Video()
{
}
void Video::Display()
{
cout << "電影名:" << m_strName << " 數量:" << m_nNum<< " 播放時間:" << m_nTime << "分鐘" << endl;
}
//Decorator.h
#include "Items.h"
class Decorator : public Items
{
public:
Decorator();
Decorator(Items*);
virtual ~Decorator();
virtual void Display();
protected:
Items* m_pItem;
};
//Decorator.cpp
#include "stdafx.h"
#include "Decorator.h"
Decorator::Decorator()
{
m_pItem = NULL;
}
Decorator::Decorator(Items* pItem)
{
m_pItem = pItem;
}
Decorator::~Decorator()
{
if(m_pItem != NULL)
{
delete m_pItem;
m_pItem = NULL;
}
}
void Decorator::Display()
{
m_pItem->Display();
}
//Borrowable.h
#include "Decorator.h"
#include <string>
class Borrowable : public Decorator
{
public:
Borrowable();
Borrowable(Items*);
virtual ~Borrowable();
void SetBorrower(std::string);
void Display();
void BorrowItem();
private:
std::string m_strBorrower;
};
//Borrowable.cpp
#include "stdafx.h"
#include "Borrowable.h"
#include <iostream>
using namespace std;
Borrowable::Borrowable()
{
}
Borrowable::Borrowable(Items* pItem) : Decorator(pItem)
{
}
Borrowable::~Borrowable()
{
}
void Borrowable::SetBorrower(string strBorrower)
{
m_strBorrower = strBorrower;
}
void Borrowable::Display()
{
m_pItem->Display();
BorrowItem();
}
void Borrowable::BorrowItem()
{
cout << "借給:" << m_strBorrower << endl;
}
//main.cpp
#include "stdafx.h"
#include "Items.h"
#include "Book.h"
#include "Video.h"
#include "Decorator.h"
#include "Borrowable.h"
int main(int argc, char* argv[])
{
Items* pItem = new Book("深入淺出設計模式", 10);
pItem->Display();
pItem = new Video("反恐24小時", 24, 200);
Borrowable* pBorrow = new Borrowable(pItem);
pBorrow->SetBorrower("張三");
pBorrow->Display();
return 0;
}
上面,我們顯示了一本書的信息,并且將一本影碟借給張三。
最后輸出為:
書名:深入淺出設計模式 數量:10
電影名:反恐24小時 數量:24 播放時間:200分鐘
借給:張三