zoukankan      html  css  js  c++  java
  • 关于成员函数的Command模式的简单实现

    看到Fox发表关于成员函数的消息映射的文章,也忍不住发表的一点自己的观点,希望对大家有所帮助。
    其实也就是COMMAND模式的简单实现,看代码吧。
    1. XGUIEventHandlerPointer.h
    namespace XGUI
    {
        
    class EventHandlerSlot
        {
        
    public:
            
    virtual ~EventHandlerSlot() {};
            
    virtual void execute(const EventArgs& args) = 0;
            
    virtual void* getClass() = 0;
        };

        template
    <typename T>
        
    class EventHandlerPointer :
            
    public EventHandlerSlot
        {
        
    public:
            typedef 
    void (T::*EventHandler)(const EventArgs&);
        
    public:
            EventHandlerPointer() :
                d_undefined(
    true),
                d_object(NULL)
            {}
            EventHandlerPointer(EventHandler func, T
    * obj) :
                d_function(func),
                d_object(obj),
                d_undefined(
    false)
            {}
            
    virtual ~EventHandlerPointer() {}

            
    void execute(const EventArgs& args)
            {
                
    if(!d_undefined) 
                    (d_object
    ->*d_function)(args);
            }

            
    void* getClass()
            {
                
    return static_cast<void*>(d_object);
            }

        
    protected:
            EventHandler    d_function;
            T
    *                d_object;
            
    bool            d_undefined;
        };
    }

    2. XGUIWidget.h
     
    namespace XGUI
    {
        
    // 
        class Widget
        {
        
    public:
            template
    <typename T> void addWidgetEventHandler(WidgetEvent EVENT, void (T::*function)(const EventArgs&), T* obj)
            {
                mWidgetEventHandlers[EVENT].push_back(
    new EventHandlerPointer<T>(function,obj));
            }
            
    void addWidgetEventHandler(WidgetEvent EVENT, EventHandlerSlot* function)
                    {
                mWidgetEventHandlers[EVENT].push_back(
    new EventHandlerPointer<T>(function,obj));
            }
                    
    bool Widget::fireWidgetEvent(WidgetEvent EVENT, EventArgs& args)
                    {
                          
    // If there are no User defined event handlers we are done.
                          if(mWidgetEventHandlers[EVENT].empty())
                           
    return false;
                        
                          
    // Execute registered handlers
                          std::vector<EventHandlerSlot*>* userEventHandlers = &(mWidgetEventHandlers[EVENT]);
                          
    for(std::vector<EventHandlerSlot*>::iterator it = userEventHandlers->begin(); it != userEventHandlers->end(); ++it )
                           (
    *it)->execute(args);
                        
                          
    return true;
                 }
            
    // .
        };
    }

    以上只为部分代码。
    我最擅长从零开始创造世界,所以从来不怕失败,它最多也就让我一无所有。
  • 相关阅读:
    软件项目不同角色的职责
    Flex与.NET互操作(四):使用HttpService、URLReqeust和URLLoader加载/传输数据
    Flex与.NET互操作(一):基于Socket的网络连接
    ActionScript 3.0 Step By Step系列(一):工欲其善,先利其器(Flex Builder)
    Flex与.NET互操作(三):基于WebService的数据访问(下)
    编写jQueryUI插件(widget)(转载)
    编写一个简单的widget
    wcf系列学习5天速成——第五天 服务托管(转载)
    解决WCF双工通讯模式中向ChannelFactory提供的InstanceContext包含未实现CallbackContractType的问题
    .NET简谈事务本质论(转载)
  • 原文地址:https://www.cnblogs.com/flying_bat/p/1314403.html
Copyright © 2011-2022 走看看