游戏开发设计模式 桥模式

在这里插入图片描述
在这里插入图片描述
跟装饰器模式一样,设计上属于职责划分,要划清责任,分清主体和拓展,解决的是(随着需求变化,子类数目极具膨胀的问题)

头文件

#pragma once
#include <iostream>
using namespace std;

template<typename T>//这里用了下类的前向声明,不然编译过不去
class My_character;

template<typename T>
class My_weapons {
public:
	virtual void fire() {}
};

template<typename T>
class weapons_gun:public My_weapons<T>{
public:
	virtual void fire() {
		cout << "武器:火枪 对目标造成伤害" << endl;//平时这里是算法
	}
};

template<typename T>
class weapons_knife :public My_weapons<T> {
public:
	virtual void fire() {
		cout << "武器:军刀 对目标造成伤害" << endl;
	}
};

template<typename T>
class weapons_rocket :public My_weapons<T> {
public:
	virtual void fire() {
		cout << "武器:火箭 对目标造成伤害" << endl;
	}
};



template<typename T>
class My_character {
public:
	My_character(My_weapons<T>* Weapon) :wea(Weapon) {}
	My_weapons<T>* wea;
	virtual void attack(My_character<T>* target) {
		wea->fire();
	}
};

template<typename T>
class My_soldier :public My_character<T> {
public:
	My_soldier(My_weapons<T>* Weapon) :My_character<T>(Weapon) {}
};

template<typename T>
class My_enemy :public My_character<T> {
public:
	My_enemy(My_weapons<T>* Weapon) :My_character<T>(Weapon) {}
};

接口

	cout << "测试桥模式" << endl;
	weapons_gun<int>* gun1 = new weapons_gun<int>();
	//auto gun3 = new weapons_gun<int>();
	//My_weapons<int>* gun4 = new weapons_gun<int>();//这两种方法用也可以
	My_soldier<int>* soldier1 = new My_soldier<int>(gun1);
	weapons_knife<int>* knife1 = new weapons_knife<int>();
	My_soldier<int>* soldier2 = new My_soldier<int>(knife1);
	weapons_rocket<int>* rocket1 = new weapons_rocket<int>();
	My_soldier<int>* soldier3 = new My_soldier<int>(rocket1);

	weapons_gun<int>* gun2 = new weapons_gun<int>();
	My_enemy<int>* enemy1 = new My_enemy<int>(gun2);
	weapons_knife<int>* knife2 = new weapons_knife<int>();
	My_enemy<int>* enemy2 = new My_enemy<int>(knife2);
	weapons_rocket<int>* rocket2 = new weapons_rocket<int>();
	My_enemy<int>* enemy3 = new My_enemy<int>(rocket2);

	soldier1->attack(enemy1);
	soldier2->attack(enemy2);
	soldier3->attack(enemy3);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值