使用动态对象创建关闭工厂方法。 [英] Closing a factory method using dynamic object creation.

查看:82
本文介绍了使用动态对象创建关闭工厂方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些麻烦。


问题:


在DSP应用程序中,我们的数据配置文件有所变化点数。

我们有一些离散傅立叶变换,它们针对特定点数的b / b $ b进行了优化。目前,这些DFT类的任何客户都必须打开配置文件中的点数并实现正确的
DFT算法。


开关(大小)

案例2000:

返回新的DFT2000Point();

案例3600:

返回新的DFT3600Point();

//等...


显然这是马屎。


我刚刚创建了一个带有配置文件的工厂类,并返回一个基本的

类指针,指向正确的DFT具体类型。它目前再次通过

执行此操作,切换配置文件中的点数。仍然不好,但

至少客户端不必复制此代码。

困扰我的是这种方法不会改变。每次我们添加一个

新的DFT类时,我们都要修改这个开关。很明显我想避免这个



所以,我该怎么做?


我的第一感觉是,它们是具体的

级和点数之间的明显映射。所以我应该有一张地图:


class DFTFactory

{

public:


Map< int size,const type_info& theType> ;;


// .....

使用create方法:


auto_ptr< DFTBase>创建(int numberOfPoints)

{

// ....

所以我的问题是......如何创建一个实例一个具体的课程来自

它的type_info?

干杯,


4空间

I''ve hit something of a snag.

Problem:

In a DSP application we have data profiles with a varying number of points.
We have a number of Discrete Fourier transforms that are optimised for a
specific number of points. Currently, any client of these DFT classes must
switch on the number of points in the profile and instanciate the correct
DFT algorithm.

switch( size )
case 2000:
return new DFT2000Point();
case 3600:
return new DFT3600Point();
// etc...

Clearly this is horse poo.

I''ve just created a factory class that takes a profile, and returns a base
class pointer to the correct concrete type of DFT. It does this currently by
again, switching on the number of points in the profile. Still not good, but
at least the clients are not having to replicate this code. The thing that
bothers me is that this method is not closed to change. Every time we add a
new DFT class, we have to modify this switch. Clearly I''d like to avoid
this.

So, how do I do it?

My first feeling is that their is an obvious mapping between the concrete
class, and the number of points. So I should perhaps have a map:

class DFTFactory
{
public:

Map<int size, const type_info &theType>;

// .....
With a create method:

auto_ptr<DFTBase> Create( int numberOfPoints )
{
//....
So my question is... how do I create an instance of a concrete class from
its type_info?
Cheers,

4Space

推荐答案




4Space写道:


4Space wrote:




我会这样做:


你的rmap是一个好主意,所以保留它,但不是存储type_info',

为什么不存储指针已存在的物品?


类DFTFactory

{

公开:


无效注册(int Size,DFTBase * pDFT);

DFTBase * LookUp(int Size);


private:

std: :地图< int,DFTBase *> m_TemplateObjects;

};


现在你从一个普通的DFTBase派生你所有的DFT-Engines。

每个DFT-Engine创建在启动一个实例并注册

该实例与DFTFactory。

当时间到了使用转换对象时,工厂

可以根据请求的大小查找其中一个对象。

然后它可以返回指向该对象的指针,或者使用虚拟的
构造函数来创建一个副本来自它,通过调用虚拟

函数克隆():


类DFTBase

{

公开:

....

虚拟DFTBase *克隆()= 0;

....

};


class DFT2000Point:public DFTBase

{

public:

... 。

虚拟DFTBase * Clone(){返回新的DFTBase2000Point(* this); };

....

};


在考虑它时,我相信只有一个DFT对象

(对于每个点大小)应该足够了,所以你还可以在讨论Singleton模式时调查
。但这只是你能决定的事情




-

Karl Heinz Buchegger
kb ****** @ gascad.at


> class DFTFactory
> class DFTFactory
{
public:

void Register(int Size,DFTBase * pDFT);
DFTBase * LookUp(int Size); std :: map< int,DFTBase *> m_TemplateObjects;
};
现在你从一个普通的DFTBase派生所有DFT-Engines。
每个DFT-Engine在启动时创建一个实例并注册
该实例使用DFTFactory。
{
public:

void Register( int Size, DFTBase* pDFT );
DFTBase* LookUp( int Size );

private:
std::map< int, DFTBase* > m_TemplateObjects;
};

Now you derive all your DFT-Engines from a common DFTBase.
Every DFT-Engine creates at startup one instance and registers
that instance with the DFTFactory.




我应该如何处理注册?在

具体类中使用静态初始化来实现自身,然后自行注册?


干杯,


4Space



How should I tackle the registration? Use static initialisation in the
concrete class to instanciate itself and then register itself?

Cheers,

4Space




4Space写道:


4Space wrote:
DFTFactory类
{
公开:

void Register(int Size,DFTBase * pDFT);
DFTBase * LookUp(int Size);

private:
std :: map< int,DFTBase *> m_TemplateObjects;
};
现在你从一个普通的DFTBase派生所有DFT-Engines。
每个DFT-Engine在启动时创建一个实例并注册
该实例使用DFTFactory。
class DFTFactory
{
public:

void Register( int Size, DFTBase* pDFT );
DFTBase* LookUp( int Size );

private:
std::map< int, DFTBase* > m_TemplateObjects;
};

Now you derive all your DFT-Engines from a common DFTBase.
Every DFT-Engine creates at startup one instance and registers
that instance with the DFTFactory.



我该如何处理注册?在
具体类中使用静态初始化来实现自身,然后注册自己?

干杯,



How should I tackle the registration? Use static initialisation in the
concrete class to instanciate itself and then register itself?

Cheers,




例如。一个简单的解决方案


int main()

{

DFT2000Points The2000PointDFT;

DFT2000Points The4000PointDFT;

DFTFactory TheDFTFactory;


TheDFTFactory.Register(2000,& The2000PointDFT);

TheDFTFactory.Register(4000,& The4000PointDFT);


...

}


或正如你所说:创建一个静态实例并让它在ctor中注册



或...


有很多种可能性


-

Karl Heinz Buchegger
kb *** ***@gascad.at


这篇关于使用动态对象创建关闭工厂方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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