KBengine中的设计模式

KBengine那么大的代码量,读起来真的心累,不过认真读总能发现一些有趣的东西,比如设计模式。

1.单例模式:

template <typename T> 
class Singleton
{
protected:
    static T* singleton_;

public:
    Singleton(void)
    {
        assert(!singleton_);
#if defined(_MSC_VER) && _MSC_VER < 1200     
        int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
        singleton_ = (T*)((int)this + offset);
#else
        singleton_ = static_cast< T* >(this);
#endif
    }
    
    
    ~Singleton(void){  assert(singleton_);  singleton_ = 0; }
    
    static T& getSingleton(void) { assert(singleton_);  return (*singleton_); }
    static T* getSingletonPtr(void){ return singleton_; }
};

#define KBE_SINGLETON_INIT( TYPE )                          \
template <>  TYPE * Singleton< TYPE >::singleton_ = 0;  \
    
}

2.简单工厂模式

class DataDownloads;

class DataDownload : public thread::TPTask
{
public:
    DataDownload(PyObjectPtr objptr, 
        const std::string & descr, int16 id);

    virtual ~DataDownload();
    
    virtual bool checkDescr(){ return true; }

    void pDataDownloads(DataDownloads* pDataDownloads){ pDataDownloads_ = pDataDownloads; }
    DataDownloads* pDataDownloads(){ return pDataDownloads_; }

    virtual thread::TPTask::TPTaskState presentMainThread();

    void entityID(ENTITY_ID entityID){ entityID_ = entityID; }
    ENTITY_ID entityID(){ return entityID_; }

    bool send(const Network::MessageHandler& msgHandler, Network::Bundle* pBundle);

    void id(int16 i){ id_ = i; }
    int16 id() const{ return id_; }

    uint32 totalBytes() const{ return totalBytes_; }

    virtual char* getOutStream(){ return stream_; }

    virtual int8 type() = 0;
protected:
    PyObjectPtr objptr_;
    std::string descr_;
    int16 id_;
    DataDownloads* pDataDownloads_;

    bool sentStart_;

    uint32 totalBytes_;

    // 总共发送的字节数
    uint32 totalSentBytes_;
    uint32 remainSent_;
    uint32 currSent_;

    char* stream_;

    ENTITY_ID entityID_;

    bool error_;
};

class StringDataDownload : public DataDownload
{
public:
    StringDataDownload(PyObjectPtr objptr, 
        const std::string & descr, int16 id);

    virtual ~StringDataDownload();

    virtual bool process();

    virtual char* getOutStream();

    virtual int8 type();
};

class FileDataDownload : public DataDownload
{
public:
    FileDataDownload(PyObjectPtr objptr, 
        const std::string & descr, int16 id);

    virtual ~FileDataDownload();

    virtual bool process();

    virtual int8 type();
protected:
    std::string path_;
    
};

class DataDownload;


class DataDownloads
{
public:
    DataDownloads();
    ~DataDownloads();
    
    int16 pushDownload(DataDownload* pdl);

    void onDownloadCompleted(DataDownload* pdl);

    int16 freeID(int16 id);
private:
    std::map<int16, DataDownload*> downloads_;

    std::set< uint16 > usedIDs_;
};

class DataDownloadFactory
{
public:
    enum DataDownloadType
    {
        DATA_DOWNLOAD_STREAM_FILE = 1,
        DATA_DOWNLOAD_STREAM_STRING = 2
    };

    static DataDownload * create(DataDownloadType dltype, PyObjectPtr objptr, 
            const std::string & desc, int16 id);

};

转载于:https://www.cnblogs.com/dingbin1995/p/8607748.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值