如何最好地解耦两个类,哪一个是单向依赖 我试图用C++来创建一个D&D作战模拟模拟器,因为它是研发的,仿真的各个方面都将严重依赖于“骰子”类及其方法。 我可以在每次其他类需要时实例化一个“Dice”对象 然而,要调用它的方法,这将使一切都变得沉重 耦合,使得以后很难进行更改或扩展
我对工厂、依赖注入和其他类似的方法没有任何实际知识。 因此,我的问题实质上是: 确保“骰子”类保持不变的最佳方法是什么 尽可能与所有其他类解耦? 同时还允许他们在需要时使用“骰子”对象及其方法。 掷骰子.h如何最好地解耦两个类,哪一个是单向依赖 我试图用C++来创建一个D&D作战模拟模拟器,因为它是研发的,仿真的各个方面都将严重依赖于“骰子”类及其方法。 我可以在每次其他类需要时实例化一个“Dice”对象 然而,要调用它的方法,这将使一切都变得沉重 耦合,使得以后很难进行更改或扩展,c++,class,design-patterns,decoupling,C++,Class,Design Patterns,Decoupling,我对工厂、依赖注入和其他类似的方法没有任何实际知识。 因此,我的问题实质上是: 确保“骰子”类保持不变的最佳方法是什么 尽可能与所有其他类解耦? 同时还允许他们在需要时使用“骰子”对象及其方法。 掷骰子.h #ifndef dice_h_ #define dice_h_ #include <stdlib.h> class Dice { private: int maxValue; public: Dice(int maxValue); ~Dice
#ifndef dice_h_
#define dice_h_
#include <stdlib.h>
class Dice
{
private:
int maxValue;
public:
Dice(int maxValue);
~Dice();
int getMaxValue( void ){return maxValue;}
void setMaxValue(int newMaxValue){maxValue = newMaxValue;}
int rollDice();
int rollMultipleDice(int numberOfDiceRolls);
};
#endif
#ifndef actor_h_
#define actor_h_
#include "dice.h"
class Actor
{
private:
unsigned int hp;
unsigned int ac; // Armor Class
unsigned int dmg;
public:
Actor(unsigned int hp, unsigned int ac, unsigned int dmg);
~Actor();
unsigned int getHP( void );
unsigned int getAC( void );
unsigned int getDmg( void );
void setHP( unsigned int newHP);
void setAC( unsigned int newAC);
void setDmg( unsigned int newDmg);
void attack(Actor* target);
bool isHit(Actor target);
};
#endif
Actor.h
#ifndef dice_h_
#define dice_h_
#include <stdlib.h>
class Dice
{
private:
int maxValue;
public:
Dice(int maxValue);
~Dice();
int getMaxValue( void ){return maxValue;}
void setMaxValue(int newMaxValue){maxValue = newMaxValue;}
int rollDice();
int rollMultipleDice(int numberOfDiceRolls);
};
#endif
#ifndef actor_h_
#define actor_h_
#include "dice.h"
class Actor
{
private:
unsigned int hp;
unsigned int ac; // Armor Class
unsigned int dmg;
public:
Actor(unsigned int hp, unsigned int ac, unsigned int dmg);
~Actor();
unsigned int getHP( void );
unsigned int getAC( void );
unsigned int getDmg( void );
void setHP( unsigned int newHP);
void setAC( unsigned int newAC);
void setDmg( unsigned int newDmg);
void attack(Actor* target);
bool isHit(Actor target);
};
#endif
Actor.cpp
#ifndef dice_cpp_
#define dice_cpp_
#include "dice.h"
Dice::Dice(int maxValue){this->maxValue = maxValue;}
Dice::~Dice(){}
int Dice::rollDice()
{
return (rand() % maxValue) + 1;
}
int Dice::rollMultipleDice(int numberOfDiceRolls)
{
int i = numberOfDiceRolls, sum = 0;
while(i-- > 0)
{
sum += rollDice();
}
return sum;
}
#endif
#ifndef actor_cpp_
#define actor_cpp_
#include "actor.h"
Actor::Actor(unsigned int hp, unsigned int ac, unsigned int dmg)
{
this->hp = hp;
this->ac = ac;
this->dmg = dmg;
}
Actor::~Actor(){}
unsigned int Actor::getHP( void ){return hp;}
unsigned int Actor::getAC( void ){return ac;}
unsigned int Actor::getDmg( void ){return dmg;}
void Actor::setHP( unsigned int newHP ){this->hp = newHP;}
void Actor::setAC( unsigned int newAC ){this->ac = newAC;}
void Actor::setDmg( unsigned int newDmg ){this->dmg = newDmg;}
void Actor::attack(Actor* target)
{
Dice damageDice(8);
if (isHit(*target))
{
target->setHP(target->getHP() - damageDice.rollDice());
}
}
// helper function to attack function
// do not use elsewhere
bool Actor::isHit(Actor target)
{
Dice atkDice(20);
return atkDice.rollDice() >= target.getAC();
}
#endif
你在问题中谈到了单例类等等,那么你希望需要使用dice的类使用dice类的同一个实例吗 如果是这样的话,因为Dice类没有任何状态可以保持,所以它可以是一个静态类。然后,我将删除MaxValue,并向RollDice和rollmutice添加一个输入,以获取一个Max值(我猜它应该位于骰子的“边”)。因此,调用Dice::RollDice(3)就是掷一个三面骰子,或者Dice::RollMutiDice(6,3)将掷一个六面骰子3次
还要确保发送rand()类。我想是srand(int)。通常你会浪费时间,所以它是相当随机的你的
骰子
不依赖于演员
,不清楚你想要解耦什么通常,*。cpp
文件不是\include
'd,所以你不需要为它们设置标题保护。@idclev463035818我的意思是,如果我每次需要使用骰子时都实例化它(就像我在Actor::attack中所做的那样)。然后,如果我以后通过添加字段或更改构造函数来更改Dice,我会破坏创建Dice实例的每个类或函数。我不希望所有类都使用同一个实例,因为它们可能需要将Dice与“不同”一起使用很多方面。这是一个有趣的想法,但是我会需要类吗?简单地创建一组函数而不创建类不是更好吗?如果可以使用静态方法,它们只需要位于每个人都可以访问的命名空间中。因此,可以使用所需的静态函数而不是classI一定会看一看,非常感谢您的帮助:D