有一些业务逻辑,需要管理多个同样类型的对象,并对外提供查询,删除等接口,在这些场合中,可以将被管理的对象称为Entity,管理Entity的类自然就叫做Entity_Manager,当以这样的方式组织层级对象时,很直观,而且项目的风格统一,每个人一旦熟悉了这种方式,理解别人写的Entity_Manager就很轻松。根据以往的项目经验,我自己实现了Entity和Entity_Manager类,代码如下:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* _____ ___ _____ _____ _ _____ _ *
* / ___| / | | _ \ | _ \ | | | ____| | | *
* | | / /| | | |_| | | |_| | | | | |__ | | *
* | | _ / / | | | _ { | _ / | | | __| | | *
* | |_| | / / | | | |_| | | | \ \ | | | |___ | |___ *
* \_____/ /_/ |_| |_____/ |_| \_\ |_| |_____| |_____| *
* *
* gabriel is an angel from the Holy Bible, this engine is named *
* gabriel, means bringing people good news. the goal of gabriel *
* server engine is to help people to develop various online games, *
* welcome you to join in. *
* *
* @author: lichuan *
* @qq: 308831759 *
* @email: 308831759@qq.com *
* @site: www.lichuan.me *
* @github: https://github.com/lichuan/gabriel *
* @date: 2013-11-29 09:00:21 *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef GABRIEL__BASE__ENTITY
#define GABRIEL__BASE__ENTITY
#include <string>
#include <map>
#include <vector>
#include "gabriel/base/common.hpp"
namespace gabriel {
namespace base {
template<typename ID_Type = uint32>
class Entity
{
public:
typedef ID_Type Entity_ID_Type;
typedef std::string Entity_Name_Type;
Entity()
{
m_id = 0;
}
virtual ~Entity()
{
}
void id(ID_Type _id)
{
m_id = _id;
}
ID_Type id() const
{
return m_id;
}
void name(std::string _name)
{
m_name = _name;
}
std::string name() const
{
return m_name;
}
private:
ID_Type m_id;
std::string m_name;
};
//对所有子元素进行回调