Template method pattern

Template method pattern

purpose

Template method: when the steps are determined and fixed, the details of each step are differentiated in multiple subclasses through polymorphic mechanism, which is the effect that template method can achieve;

classification

Template method mode: it belongs to behavior mode in design mode;

Specific examples

Company A has A small game project team - developing stand-alone fighting games (similar to arcade boxing games).

A game project team needs at least three employees with different roles: game planning, game program and game art

  • Game planning: referred to as planning, it is responsible for putting forward various playing methods of the game and determining various values in the game, such as the life value and magic value of characters (enemies).

  • Game program: referred to as program, it needs to cooperate closely with game planning to realize various game functions required by game planning through code.

  • Game Art: character design, prop design, game special effects, etc.

Game planning requirements:

The protagonist of the game is a soldier (the attack power is not strong enough, the life value is relatively large, and the protagonist is resistant to beating). The protagonist breaks through the Customs by constantly coming forward. When he meets the enemy, he will attack, the enemy will also fight back, and the enemy will take the initiative to attack the protagonist when he is close.

Role classification:

  • warrior

    • attribute
      • HP - 1000
      • Magic value - 0
      • Attack power - 200
    • skill
      • "Burning" - using this skill can cause all nearby enemies to lose 500 HP each, but the protagonist himself will also lose 300 HP.
  • Mage (strong attack power, low HP, not resistant to beating)

    • attribute
      • HP: 800
      • Magic value: 200
      • Attack power: 300
    • skill
      • "Burning" - using this skill can cause all nearby enemies to lose 650 HP, but the protagonist will lose 100 magic.

    The performance of warrior and mage's release burning skill is different. This different performance is mainly through the effect in Fighter and Magic subclasses_ Enemy and effect_self virtual function;

code

#include <iostream>

namespace template_method {
class Warror {
public:
	Warror(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
	virtual ~Warror(){}

public:
	void skill_burn() {
		effect_enemy();
		effect_myself();
		play_effect();
	}

private:
	virtual void effect_enemy() {};
	virtual void effect_myself() {};
	void play_effect() {
		std::cout << "Release skill effects" << std::endl;
	}
protected:
	int m_life;
	int m_magic;
	int m_attack;
};

class Fighter : public Warror {
public:
	Fighter(int life, int magic, int attack) : Warror(life, magic, attack)	{}

private:
	virtual void effect_enemy() {
		std::cout << "Lose 500 HP to the enemy" << std::endl;
	}

	virtual void effect_self() {	
		std::cout << "Lose 300 HP" << std::endl;
		m_life -= 300;
	}
};

class Magic : public Warror {
public:
	Magic(int life, int magic, int attack) : Warror(life, magic, attack) {}

private:
	virtual void effect_enemy() {
		std::cout << "Lose 600 HP to the enemy" << std::endl;
	}

	virtual void effect_self() {
		std::cout << "Lose 200 mana points" << std::endl;
		m_magic -= 200;
	}
};
};// end of namespace template_method

int main() {
	template_method::Warror* proler_fighter = new template_method::Fighter(1000, 0, 200);
	proler_fighter->skill_burn();

	template_method::Warror* proler_magic = new template_method::Magic(800, 200, 300);
	proler_magic->skill_burn();

	delete proler_fighter;
	delete proler_magic;

	return 0;
}

reference material:

1. Wang Jianwei: C + + design pattern

2. Cheng Jie: Dahua design mode

Keywords: C++ Design Pattern Back-end Game Development

Added by daedlus on Thu, 20 Jan 2022 19:00:22 +0200