C ++工厂模式 [英] c++ factory pattern

查看:98
本文介绍了C ++工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长的文字对不起,但是我想提出我正在尝试解决的问题以及实际问题.也许我会指出另一种可能更好的方法.

I am sorry for the long text but I would like to present the problem I am trying to solve with this as well as the actual problem. Maybe I will be pointed to another, possibly better approach.

我想创建一个事件触发的处理程序系统.我将有几个处理程序模块.基本思想是,在初始化期间,这些模块将在事件列表中注册它们处理的事件(register(module,eventItGetsCalledFor)).如果触发了事件,则在事件列表中查找该事件,并创建并运行在那里列出的所有模块.为此,所有模块都有一个接口.

I would like to create an eventtriggerred handler system. I will have several handler modules. The basic idea is that during initialization these modules will register for events they handle (register(module,eventItGetsCalledFor)) in a eventlist. In the case an event is triggered, it is looked up in the eventlist and all modules listed there are created and run. To achieve this all the modules have an interface.

我的两个问题是:

  1. 因为我只想在实际事件上实例化模块,所以我需要一种方法来存储指向构造函数的指针(虚拟构造函数"),因为据我所知,没有虚拟构造函数与我想添加到接口的Factory方法.我知道的经典方法是:

  1. Since I only want to instantiate the modules upon the actual event I need either a way to store a pointer to the constructor ("virtual constructor"), since there are no virtual constructors as far as I know I go with a Factory method which I would like to add to the interface. The classic approach I know of would be:

class myInterface {
public:
   ...
   static virtual *myInterface builder() = 0; //is static (pure) virtual possible?
};

class example : public myInterface {
public:
   ...
   static virtual *myInterface builder() { return new example(); }
};

然后,我将必须在事件列表中存储指向builder()函数的指针,并在事件发生时执行这些函数以创建对象.我想知道是否有一种方法可以为每个示例模块编写static virtual *myInterface builder() { return new example(); }. 模板方法就是这样写:

I would then have to store pointers to the builder() functions in the event list and execute the functions in case the event occurs to create the objects. I am wondering if there is a way arround writing static virtual *myInterface builder() { return new example(); } for every example module. A template approach would be to put it like this:

模板 静态虚拟* myInterface builder(){返回新的T(); }

template static virtual *myInterface builder() { return new T(); }

但是我不太喜欢它,因为我必须编写以下内容来注册事件模块:

However I dont like it to much since I would have to write the following to register a module for an event:

注册(eventName,builder())

register(eventName,builder())

考虑到我只想在每个模块中创建一个静态寄存器函数,如下所示:

considering that I would like to create only one static register function in each module like so:

class example: public myInterface {
    static void registerMe {
        register(event1,builder<example>());
        register(event2,builder<example>());
        ...
        register(eventn,builder<example>());
    }
};

如果像register(event)这样的电话就足够了,那就更方便了. 我可以在接口中添加register(event),但是我不想为每个要读取的模板重写注册功能

it would be way more convenient if a call like register(event) would be sufficient. i could add register(event) to the interface, but I dont want to rewrite the register function for every template which would read

void register(event) { getListForEvent(event).pushBack(builder<example>()); }

(builder-template实例化参数各不相同),并且使其本身也不是模板,因为这使我回到了一遍又一遍地编写register<myclass>().

(the builder-template instantiation parameter varies), and making it a template itself is not to good either, since this brings me back to writing register<myclass>() over and over.

那么还有更令人信服的方法吗? (在仔细考虑和编写过程中,我想出了从模板类继承的方法.如果polimorphism适用于具有不同实例化参数的模板类.否则,我必须使用Interface类,将其包装在模板中并从该模板继承).

So is there a more conviniant way? (During the pondering and writing of this I came up with inheriting from a template class. If polimorphism works with template classes with different instantiation parameters. Otherwise I have to use an Interface class, wrap it in a template and Inherit from that )

第二个问题是我从接口类派生了不同的类.但是,它们具有未在接口中定义的成员.我想将一个函数指针存储到一个成员函数中,我该怎么做?该类型必须接受不同的基类,但是其余的函数签名是相同的(请参见下面的示例)

The second question is I have different classes derived from my interface class. However they have members which are not defined in the Interface. I would like to store a functionpointer to a member function, how do I do this? The type has to accept different base classes, but the rest of the function signature is the same (see example below)

class A : public interface {
     void myFunc()
};

class A : public interface {
     void yourFunc()
};

whatType* a = &A::myFunc;
whatType* b = &B::yourFunc;

推荐答案

//可以使用静态(纯)虚拟吗?

//is static (pure) virtual possible?

CRTP(重复出现的模板模式)

template <class Derived>
class myInterface {
public:
    ...
    virtual *myInterface builder() = 0; 

    Derived* buildDerived() 
    { 
         return dynamic_cast<Derived*>(builder());
    }

};

这篇关于C ++工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆