Design Patterns C++ Decorator Patterns

Buying a new house (rough house) needs renovation. Renovation of the new house does not change the essence of the house for living, but it makes the house more beautiful and better meets the needs of the home. In software design, we can also use similar techniques to expand (decorate) the functions of the original object (new house) to obtain objects that better meet user needs. This technique is called decorator pattern in design pattern

The decoration mode can add additional new behaviors to an object without changing the object itself. In real life, this situation abounds. For example, for a photo, you can add a frame to it without changing the photo itself, making it It has the function of moisture resistance, and users can add different types of photo frames to it according to their needs, and even a large photo frame can be placed outside a small photo frame

Using the inheritance mechanism is an effective way to add functionality to an existing class. By inheriting an existing class, the subclass can have its own methods and the methods of the parent class, but this method is static, and users cannot control the addition. How and when to behave. For example, we want to add ingredients to milk tea, including pearls, coconuts, and raisins. If we use inheritance, we must inherit all three classes. At this time, the number of classes will increase, and there will be certain problems in multiple inheritance itself. Improper use can lead to different results than expected

Thought definition : Dynamically add some additional responsibilities to an object. In terms of adding object functions, the decoration mode is more flexible than generating subclasses. Its alias can also be called a wrapper

Advantages :

  1. Decorator pattern can provide more flexibility than inheritance
  2. It is possible to extend the functionality of an object in a dynamic way
  3. The specific construction class and specific decoration class can be changed independently, and the original code does not need to be modified when adding a new specific construction class and specific decoration class, which conforms to the principle of opening and closing

Disadvantages :

  1. When using the decoration mode for system design, many small objects and specific decoration classes will be generated. The generation of these small objects and decoration classes will increase the complexity of the system and increase the difficulty of learning and understanding.
  2. Decoration mode is more error-prone than inheritance, and troubleshooting is also very difficult. For objects decorated multiple times, it may be necessary to check step by step to find errors during debugging, which is more cumbersome

Applicable scenarios :

  1. Dynamically and transparently add responsibilities to individual objects without affecting other objects
  2. Functions need to be dynamically added to an object, and these functions can also be revoked dynamically

Code : Add fanatics and black cuts to Nuo Shou

//抽象英雄
class AbstractHero
{
    
    
public:
	virtual void ShowStatus() = 0;
public:
	int mHp;//血量
	int mMp;//蓝量
	int mAt;//攻击力
	int mDf;//防御力
};

//具体英雄--诺手
class NuoShou : public AbstractHero
{
    
    
public:
	NuoShou()
	{
    
    
		mHp = 500;
		mMp = 200;
		mAt = 100;
		mDf = 80;
	}
	virtual void ShowStatus()
	{
    
    
		cout << "血量:" << mHp << endl;
		cout << "蓝量:" << mMp << endl;
		cout << "攻击力:" << mAt << endl;
		cout << "防御力:" << mDf << endl;
	}
};

//英雄添加装备,添加完后还是一个英雄,所以要继承 AbstractHero
class AbstractEquipment :public AbstractHero
{
    
    
public:
	AbstractEquipment(AbstractHero* hero)
	{
    
    
		this->hero = hero;
	}
	virtual void ShowStatus(){
    
    }
protected:
	AbstractHero* hero;
};

//狂徒装备
class KuangtuEquipment : public AbstractEquipment
{
    
    
public:
	KuangtuEquipment(AbstractHero* hero)
		: AbstractEquipment(hero)
	{
    
    }
	//增加额外的功能
	void AddKuangtu()
	{
    
    
		cout << "英雄购买了狂徒之后" << endl;
		this->mHp = this->hero->mHp + 1000;
		this->mMp = this->hero->mMp;
		this->mAt = this->hero->mAt;
		this->mDf = this->hero->mDf;
	}
	virtual void ShowStatus()
	{
    
    
		AddKuangtu();
		cout << "血量:" << mHp << endl;
		cout << "蓝量:" << mMp << endl;
		cout << "攻击力:" << mAt << endl;
		cout << "防御力:" << mDf << endl;
	}
};

//黑切
class HeiqieEquipment : public AbstractEquipment
{
    
    
public:
	HeiqieEquipment(AbstractHero* hero)
		: AbstractEquipment(hero)
	{
    
    }
	//增加额外的功能
	void AddHeiqie()
	{
    
    
		cout << "英雄购买了黑切之后" << endl;
		this->mHp = this->hero->mHp;
		this->mMp = this->hero->mMp;
		this->mAt = this->hero->mAt + 55;
		this->mDf = this->hero->mDf;
	}
	virtual void ShowStatus()
	{
    
    
		AddHeiqie();
		cout << "血量:" << mHp << endl;
		cout << "蓝量:" << mMp << endl;
		cout << "攻击力:" << mAt << endl;
		cout << "防御力:" << mDf << endl;
	}
};

test:

void test()
{
    
    
	AbstractHero* hero = new NuoShou();
	cout << "英雄初始属性" << endl;
	hero->ShowStatus();
	cout << endl;

	hero = new KuangtuEquipment(hero);
	hero->ShowStatus();
	cout << endl;

	hero = new HeiqieEquipment(hero);
	hero->ShowStatus();
}

screenshot:
insert image description here

Guess you like

Origin blog.csdn.net/qq_44443986/article/details/117587828