Composite

// Entity.h

#ifndef _ENTITY_H

#define _ENTITY_H

class Entity {

public:

virtual void traverse() = 0;

};

#endif

// Group.h

#ifndef _GROUP_H

#define _GROUP_H

#include "Entity.h"

#include <vector>

using namespace std;

class Group : public Entity {

public:

Group(int val);

void traverse();

void addMember(const Entity & myEntity);

private:

vector<Entity> entityList;

};

#endif

說明

    • 建立『樹狀結構』類別。
    • 需要有陣列容器及相關操作。

// Member.h

#ifndef _MEMBER_H

#define _MEMBER_H

#include "Entity.h"

class Member : public Entity {

public:

Member(int val);

void traverse();

};

#endif

1