c++组合对象管理

本文介绍了使用C++实现的Entity_Manager类,用于管理相同类型的对象并提供查询、删除等功能。Entity_Manager支持按名字和ID两种方式索引对象,通过继承可定制管理不同类型的对象。以任务管理器Task_Manager为例,展示了如何使用该类来管理Task对象,确保迭代时的稳定性,有助于项目代码风格统一和开发者之间的快速理解。
摘要由CSDN通过智能技术生成

有一些业务逻辑,需要管理多个同样类型的对象,并对外提供查询,删除等接口,在这些场合中,可以将被管理的对象称为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;
};

//对所有子元素进行回调
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值