• c++装饰器模式


    前言

    装饰器模式,就是可以对一个对象无限装饰一些东西,而且可以没有顺序。比如一个人可能只会说出他的名字,但是可以让他再说哈哈,可以说完哈哈之后再说哇哇。如何后面又不想装饰了,不需要改类原来的代码,直接在客户端不装饰即可;同样如果增加新的装饰,添加一个具体的装饰类即可,也不需要改变原来的代码。

    代码

    Person.h

    #pragma once
    #include
    #include
    
    class Person // 定义一个接口
    {
    public:
    	virtual void show() = 0;
    };
    
    
    class NormalPerson : public Person // 实现接口的具体类,可能有不同种类的人,他们的show()操作不同
    {
    private:
    	std::string _name;
    
    public:
    	NormalPerson(std::string name):_name(name){ }
    	~NormalPerson() {}
    
    	virtual void show() override {
    		std::cout << "normal person: " << _name << std::endl;
    	}
    };
    
    class PersonDecrator : public Person
    {
    private:
    	Person* _pPerson;
    
    public:
    	PersonDecrator(Person* p_person) :_pPerson(p_person) {}
    
    	virtual void show() override {
    		if (_pPerson != nullptr) {
    			_pPerson->show();
    		}
    		else {			
    			std::cout << "_pPerson is nullptr" << std::endl;
    		}
    	}
    };
    
    // 装饰了haha的person
    class DecratedHaHaPerson : public PersonDecrator
    {
    public:
    	DecratedHaHaPerson(Person* p_person):PersonDecrator(p_person){}
    
    	virtual void show() override
    	{
    		PersonDecrator::show(); // 调用父类方法
    		hahaShow(); // 调用子类独有的haha show() 方法
    	}
    
    	void hahaShow() {
    		std::cout << "hahahahaha" << std::endl;
    	}
    };
    
    // 装饰了wawa的person
    class DecratedWaWaPerson : public PersonDecrator
    {
    public:
    	DecratedWaWaPerson(Person* p_person) :PersonDecrator(p_person) {}
    
    	virtual void show() override
    	{
    		PersonDecrator::show(); // 调用父类方法
    		wawaShow(); // 调用子类独有的haha show() 方法
    	}
    
    	void wawaShow() {
    		std::cout << "wawawawawa" << std::endl;
    	}
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    main.cpp

    #include"Person.h"
    
    int main()
    {
    	Person* pHablee = new NormalPerson("hablee");
    	std::cout << "没有装饰任何的hablee的show: " << std::endl;
    	pHablee->show();
    	std::cout << std::endl;
    
    	// 装饰一下haha
    	pHablee = new DecratedHaHaPerson(pHablee);
    	std::cout << "装饰了haha的hablee的show" << std::endl;
    	pHablee->show();
    	std::cout << std::endl;
    
    	// 再装饰一个wawa
    	pHablee = new DecratedWaWaPerson(pHablee);
    	std::cout << "装饰了haha和wawa的hablee的show" << std::endl;
    	pHablee->show();
    	std::cout << std::endl;
    
    	delete pHablee;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    结果

    在这里插入图片描述

  • 相关阅读:
    桶排序的代码
    基于Python的大区域SPI标准降水指数自动批量化处理
    基于Asp.Net的印刷网站
    Java 基础实战—Bank 项目—实验题目
    UNIapp实现局域网内在线升级
    功能测试进阶自动化测试如何摸清学习方向,少走弯路呢?
    JavaScript知识点补充
    计算机毕业设计Java的商城后台管理系统(源码+系统+mysql数据库+lw文档)
    MicroPython开发板:pyboard快速参考
    五个维度着手MySQL的优化
  • 原文地址:https://blog.csdn.net/sdhdsf132452/article/details/134167012