介紹
? ? ? ? 裝飾模式動(dòng)態(tài)地給一個(gè)對(duì)象增加一些額外的職責(zé),就增加對(duì)象功能來說,裝飾模式比生成子類實(shí)現(xiàn)更為靈活。裝飾模式是一種對(duì)象結(jié)構(gòu)型模式。
實(shí)現(xiàn)
?myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
#include <vector>
class Component { // 抽象構(gòu)件
public:
virtual void display();
};
class ConcreteComponent : public Component { // 具體構(gòu)件
public:
void display() override;
};
class Decorator : public Component { // 抽象裝飾類
public:
Decorator(Component *component);
virtual void display();
private:
Component *m_component;
};
class ConcreteDecorator : public Decorator { // 具體裝飾類
public:
ConcreteDecorator(Component *component);
void display() override;
private:
void addedBehavior();
};
#endif //DESIGNPATTERNS_MYCLASS_H
myclass.cpp
//
// Created by yuwp on 2024/1/12.
//
#include "myclass.h"
void Component::display() {
std::cout << "Component::display()" << std::endl;
}
void ConcreteComponent::display() {
std::cout << "ConcreteComponent::display()" << std::endl;
}
Decorator::Decorator(Component *component) {
m_component = component;
}
void Decorator::display() {
m_component->display();
}
ConcreteDecorator::ConcreteDecorator(Component *component) : Decorator(component) {
}
void ConcreteDecorator::display() {
Decorator::display();
addedBehavior();
}
void ConcreteDecorator::addedBehavior() {
std::cout << "增加新的操作" << std::endl;
}
main.cpp
#include <iostream>
#include <mutex>
#include "myclass.h"
int main() {
Component *component = new ConcreteComponent();
Decorator *decorator = new ConcreteDecorator(component);
decorator->display();
delete component;
delete decorator;
return 0;
}
總結(jié)
優(yōu)點(diǎn)
? ? ? ? 1.?對(duì)于擴(kuò)展一個(gè)對(duì)象的功能,裝飾模式比繼承更加靈活性,不會(huì)導(dǎo)致類的個(gè)數(shù)急劇增加。
? ? ? ? 2.?可以通過一種動(dòng)態(tài)的方式來擴(kuò)展一個(gè)對(duì)象的功能。通過配置文件可以在運(yùn)行時(shí)選擇不同的具體裝飾類,從而實(shí)現(xiàn)不同的行為。
? ? ? ? 3.?可以對(duì)一個(gè)對(duì)象進(jìn)行多次裝飾。通過使用不同的具體裝飾類以及這些裝飾類的排列組合,可以創(chuàng)造出很多不同行為的組合,得到功能更為強(qiáng)大的對(duì)象。
? ? ? ? 4.?具體構(gòu)件類與具體裝飾類可以獨(dú)立變化,用戶可以根據(jù)需要增加新的具體構(gòu)件類和具體裝飾類,原有類庫代碼無須改變,符合開閉原則。
缺點(diǎn)
? ? ? ? 1.?使用裝飾模式進(jìn)行系統(tǒng)設(shè)計(jì)時(shí)將產(chǎn)生很多小對(duì)象。這些對(duì)象的區(qū)別在于它們之間相互連接的方式有所不同,而不是它們的類或者屬性值有所不同。大量小對(duì)象的產(chǎn)生勢必會(huì)占用更多的系統(tǒng)資源,在一定程度上影響程序的性能。
? ? ? ? 2.?裝飾模式提供了一種比繼承更加靈活機(jī)動(dòng)的解決方案,但同時(shí)也意味著比繼承更加易于出錯(cuò),排錯(cuò)也很困難。對(duì)于多次裝飾的對(duì)象,調(diào)試時(shí)尋找錯(cuò)誤可能需要逐級(jí)排查,較為煩瑣。
適用場景
? ? ? ? 1.?在不影響其他對(duì)象的情況下,以動(dòng)態(tài)、透明的方式給單個(gè)對(duì)象添加職責(zé)。
? ? ? ? 2.?當(dāng)不能采用繼承的方式對(duì)系統(tǒng)進(jìn)行擴(kuò)展或者采用繼承不利于系統(tǒng)擴(kuò)展和維護(hù)時(shí)可以使用裝飾模式。不能采用繼承的情況主要有兩類:第1類是系統(tǒng)中存在大量獨(dú)立的擴(kuò)展,為支持每一種擴(kuò)展或者擴(kuò)展之間的組合將產(chǎn)生大量的子類,使得子類數(shù)目呈爆炸性增長;第2類是因?yàn)轭愐讯x為不能被繼承。
練習(xí)
myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
#include <vector>
class SimpleEncrypt { // 具體構(gòu)件類
public:
virtual void encrypt(std::string &in, std::string &out);
};
class EncryptDecorator : public SimpleEncrypt { // 抽象裝飾類
public:
EncryptDecorator(SimpleEncrypt *encypt);
virtual void encrypt(std::string &in, std::string &out) override;
private:
SimpleEncrypt *m_encrypt;
};
class ReverseEncrypt : public EncryptDecorator { // 具體裝飾類
public:
ReverseEncrypt(SimpleEncrypt *encrypt);
void encrypt(std::string &in, std::string &out) override;
private:
void reverseEncrypt(std::string &in, std::string &out);
};
class ModEncrypt : public EncryptDecorator { // 具體裝飾類
public:
ModEncrypt(SimpleEncrypt *encrypt);
void encrypt(std::string &in, std::string &out) override;
private:
void modEncrypt(std::string &in, std::string &out);
};
#endif //DESIGNPATTERNS_MYCLASS_H
myclass.cpp文章來源:http://www.zghlxwxcb.cn/news/detail-806082.html
//
// Created by yuwp on 2024/1/12.
//
#include "myclass.h"
void SimpleEncrypt::encrypt(std::string &in, std::string &out) {
std::cout << "執(zhí)行簡單加密" << std::endl;
out = "簡單加密+" + in;
}
EncryptDecorator::EncryptDecorator(SimpleEncrypt *encypt) {
m_encrypt = encypt;
}
void EncryptDecorator::encrypt(std::string &in, std::string &out) {
m_encrypt->encrypt(in, out);
}
ReverseEncrypt::ReverseEncrypt(SimpleEncrypt *encrypt) : EncryptDecorator(encrypt) {
}
void ReverseEncrypt::encrypt(std::string &in, std::string &out) {
std::string tmp;
EncryptDecorator::encrypt(in, tmp);
reverseEncrypt(tmp, out);
}
void ReverseEncrypt::reverseEncrypt(std::string &in, std::string &out) {
std::cout << "執(zhí)行逆向加密" << std::endl;
out = "逆向加密+" + in;
}
ModEncrypt::ModEncrypt(SimpleEncrypt *encrypt) : EncryptDecorator(encrypt) {
}
void ModEncrypt::encrypt(std::string &in, std::string &out) {
std::string tmp;
EncryptDecorator::encrypt(in, tmp);
modEncrypt(tmp, out);
}
void ModEncrypt::modEncrypt(std::string &in, std::string &out) {
std::cout << "執(zhí)行取模加密" << std::endl;
out = "取模加密+" + in;
}
main.cpp文章來源地址http://www.zghlxwxcb.cn/news/detail-806082.html
#include <iostream>
#include <mutex>
#include "myclass.h"
int main() {
std::string data = "data";
std::string result;
SimpleEncrypt *sim = new SimpleEncrypt();
sim->encrypt(data, result);
std::cout << "result:" << result << std::endl;
EncryptDecorator *res = new ReverseEncrypt(sim);
res->encrypt(data, result);
std::cout << "result:" << result << std::endl;
EncryptDecorator *mod = new ModEncrypt(res);
mod->encrypt(data, result);
std::cout << "result:" << result << std::endl;
delete mod;
delete res;
delete sim;
return 0;
}
到了這里,關(guān)于《設(shè)計(jì)模式的藝術(shù)》筆記 - 裝飾模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!