如何设计一个简单的C ++对象工厂? [英] How to design a simple C++ object factory?

查看:178
本文介绍了如何设计一个简单的C ++对象工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有10-20个类被实例化一次[*]。这里有一个例子:

  class SomeOtherManager; 

类SomeManagerClass {
public:
SomeManagerClass(SomeOtherManager *);
virtual void someMethod1();
virtual void someMethod2();
};

类的实例包含在一个对象中:

  class TheManager {
public:
virtual SomeManagerClass * someManagerClass()const;
virtual SomeOtherManager * someOtherManager()const;
/ **更多对象...最多10-20 * /
};

目前,TheManager使用 new 运算符创建对象。



我的意图是能够使用插件将SomeManagerClass(或任何其他类)实现替换为另一个。为了替换实现,需要两个步骤:


  1. 定义类DerivedSomeManagerClass,继承SomeManagerClass [plugin]

  2. 创建新类(DerivedSomeManagerClass)而不是默认(SomeManagerClass)[application]

我想我需要某些类型的对象工厂,但它应该是相当简单,因为总是只有一个类型(默认实现或用户实现)。



任何想法如何设计一个简单的工厂像我刚才描述的?考虑到未来可能会有更多的类,所以应该很容易扩展。



[*]我不在乎它是否发生多次



编辑:请注意,TheManager中包含两个以上的对象。



一个问题是:TheManager name是什么?

它必须创建的类吗?它必须保持某种指针一种创建类的方式。可能的解决方案是:




  • 为每种类保留一个单独的指针,设置它,但是你已经说你

  • 保留某种类型的表,其中键是枚举或字符串;在这种情况下,setter是一个带参数的单个函数(当然如果键是枚举,你可以使用向量而不是地图)



另一个问题是:这种创建类的方法是什么?很遗憾,我们不能直接存储指向构造函数的指针,但我们可以:




  • 如其他人所指出的,

  • 只需为每个类别添加一个静态create函数;如果他们保持一致的签名,你只需使用他们的指针函数



模板可以帮助避免不必要的代码重复的两种情况。


In my application, there are 10-20 classes that are instantiated once[*]. Here's an example:

class SomeOtherManager;

class SomeManagerClass {
public:
    SomeManagerClass(SomeOtherManager*);
    virtual void someMethod1();
    virtual void someMethod2();
};

Instances of the classes are contained in one object:

class TheManager {
public:
    virtual SomeManagerClass* someManagerClass() const;
    virtual SomeOtherManager* someOtherManager() const;
    /** More objects... up to 10-20 */
};

Currently TheManager uses the new operator in order to create objects.

My intention is to be able to replace, using plugins, the SomeManagerClass (or any other class) implementation with another one. In order to replace the implementation, 2 steps are needed:

  1. Define a class DerivedSomeManagerClass, which inherits SomeManagerClass [plugin]
  2. Create the new class (DerivedSomeManagerClass) instead of the default (SomeManagerClass) [application]

I guess I need some kind of object factory, but it should be fairly simple since there's always only one type to create (the default implementation or the user implementation).

Any idea about how to design a simple factory like I just described? Consider the fact that there might be more classes in the future, so it should be easy to extend.

[*] I don't care if it happens more than once.

Edit: Please note that there are more than two objects that are contained in TheManager.

解决方案

I think there are two separate problems here.

One problem is: how does TheManager name the class that it has to create? It must keep some kind of pointer to "a way to create the class". Possible solutions are:

  • keeping a separate pointer for each kind of class, with a way to set it, but you already said that you don't like this as it violates the DRY principle
  • keeping some sort of table where the key is an enum or a string; in this case the setter is a single function with parameters (of course if the key is an enum you can use a vector instead of a map)

The other problem is: what is this "way to create a class"? Unfortunately we can't store pointers to constructors directly, but we can:

  • create, as others have pointed out, a factory for each class
  • just add a static "create" function for each class; if they keep a consistent signature, you can just use their pointers to functions

Templates can help in avoiding unnecessary code duplication in both cases.

这篇关于如何设计一个简单的C ++对象工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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