- Game Development Patterns and Best Practices
- John P. Doran Matt Casanova
- 163字
- 2021-07-02 23:43:41
Composition
When using composition, this is a strong HAS-A relationship, and this is when a class contains one or more instances of another class. Unlike aggregation, these instances are not created on their own but, instead, are created in the constructor of the class and then destroyed by its destructor. Put into layman's terms, they can't exist separately from the whole.
In this case, we have created some new properties for the Enemy class, adding in combat skills that it can use, as in the Pokémon series. In this case, for every one enemy, there are four skills that the enemy will be able to have:
class AttackSkill
{
public:
void UseAttack(void);
private:
int damage;
float cooldown;
};
class Enemy
{
public:
void GetHealth(void) const;
void SetHealth(int);
private:
int currentHealth;
int maxHealth;
AttackSkill skill1;
AttackSkill skill2;
AttackSkill skill3;
AttackSkill skill4;
};
The line in the diagram looks similar to aggregation, aside from the fact that the diamond is filled in:

推薦閱讀
- 深度實踐OpenStack:基于Python的OpenStack組件開發(fā)
- 三維圖形化C++趣味編程
- 深入淺出RxJS
- TradeStation交易應(yīng)用實踐:量化方法構(gòu)建贏家策略(原書第2版)
- 零基礎(chǔ)學(xué)單片機C語言程序設(shè)計
- 深入淺出React和Redux
- SQL Server數(shù)據(jù)庫管理與開發(fā)兵書
- C#程序設(shè)計教程(第3版)
- Android群英傳
- Arduino計算機視覺編程
- 3ds Max印象 電視欄目包裝動畫與特效制作
- ABAQUS6.14中文版有限元分析與實例詳解
- JavaEE架構(gòu)與程序設(shè)計
- Android智能手機APP界面設(shè)計實戰(zhàn)教程
- Web前端開發(fā)精品課:HTML5 Canvas開發(fā)詳解