混合命令模式,出厂模式和模板在一起... [英] Mixing Command pattern, Factory pattern and templates all together ...
问题描述
我已经在提出类似问题,但我没有真的得到我想要的答案,因为我的问题配制不好,例子是坏的。所以我再给它一个镜头,希望有一个更好的解释和更好的代码。
代码下面已经从不必要的细节剥离,但它的工作原理。
事情是我想使用模板参数扣除,如果可能的话,简化模板函数调用。
我有一个工厂创建命令。要创建命令,我使用类似这样的调用:
mCommandFactory.createCommand<
DoSomeStuff,
ParameterType1,
ParameterType2,
ParameterType3,
ParameterType4
>
(std:string(some description),
parameter1,
parameter2,
parameter3,
parameter4);
您可能已经猜到,parameter1类型是ParameterType1,等等。
现在,如果我们看看命令的定义 - DoSomeStuff-本身:
class DoSomeStuff:public UndoableCommand< ParameterType1,ParameterType2,ParameterType3,ParameterType4>
{
public:
DoSomeStuff(... / *参数,用于进行命令和撤消命令* /);
〜DoSomeStuff()throw();
void executeImpl();
void undoImpl();
protected:
... / *成员,用于进行命令和撤消命令* /
};
如您所见,ParameterTypeN信息已经在DoSomeStuff声明中。
我想知道是否可能以某种方式替换上面的createCommand调用更简单:
mCommandFactory .createCommand< DoSomeStuff>
(std:string(some description),
parameter1,
parameter2,
parameter3,
parameter4);
以下是CommandFactory代码:
class CommandFactory
{
private:
//一些用于初始化此工厂创建的对象的东西
public:
CommandFactory(...):... / *成员初始化* /
{
}
template< class CommandType,类型名称P1,类型名称P2,类型名称P3,类型名称P4>
void createCommand(juce :: String& description,P1 p1,P2 p2,P3 p3,P4 p4)
{
Undoable * cmdPtr = new CommandType(p1,p2,p3,p4) ;
...
// init cmdPtr
(* cmdPtr)();
}
基本上,要点是移动CommandFactory中的复杂性,客户端代码(对createCommand的调用)尽可能简单和短。
有什么想法吗?
不确定我是否正确理解这个问题,但这应该有效。
template< typename ParameterType1,typename ParameterType2,typename ParameterType3,typename ParameterType4>
class UndoableCommand
{
public:
UndoableCommand(ParameterType1 p1,ParameterType2 p2,ParameterType3 p3,ParameterType4 p4)
{}
};
class DoSomeStuff:public UndoableCommand< int,double,std :: string,int>
{
public:
DoSomeStuff(int p1,double p2,std :: string p3,int p4)
:UndoableCommand(p1,p2,p3,p4)
{}
};
class CommandFactory
{
public:
CommandFactory()
{}
template< class CommandType,typename P1 ,类型名P2,类型名P3,类型名P4>
void createCommand(std :: string& description,P1 p1,P2 p2,P3 p3,P4 p4)
{
UndoableCommand< P1,P2,P3,P4& * cmdPtr = new CommandType(p1,p2,p3,p4);
}
};
使用
CommandFactory fact;
fact.createCommand< DoSomeStuff>(std :: string(description),1,2.0,std :: string(3),4);
I already asked a similar question here, however I didn't really get the answer I wanted because my question was poorly formulated and the examples were bad. So I give it another shot, with hopefully a better explanation and better code .
The code bellow has been stripped out of unnecessary details but it works . The thing is I would like to use Template Argument Deduction, if possible, to simplify the templated function call .
I have a factory which creates commands. To create a command, I use a call like that one :
mCommandFactory.createCommand<
DoSomeStuff,
ParameterType1,
ParameterType2,
ParameterType3,
ParameterType4
>
(std:string("some description"),
parameter1,
parameter2,
parameter3,
parameter4);
As you probably have guessed, parameter1 type is ParameterType1, and so on ... .
Now if we look at the definition of the command - DoSomeStuff- itself :
class DoSomeStuff : public UndoableCommand< ParameterType1 , ParameterType2, ParameterType3 , ParameterType4 >
{
public:
DoSomeStuff(... /* arguments which are needed for precessing the command and undoing it*/ );
~DoSomeStuff() throw();
void executeImpl();
void undoImpl();
protected:
... /* members which are needed for precessing the command and undoing it*/
};
As you can see, ParameterTypeN information is already inside DoSomeStuff declaration.
I was wondering if it was possible somehow to replace the createCommand call above by something simpler :
mCommandFactory.createCommand<DoSomeStuff>
(std:string("some description"),
parameter1,
parameter2,
parameter3,
parameter4);
Here is the CommandFactory code :
class CommandFactory
{
private:
// some stuff used to initialize objects created by this factory
public:
CommandFactory(...) : ... /* members initialization */
{
}
template <class CommandType, typename P1, typename P2, typename P3, typename P4>
void createCommand(juce::String& description,P1 p1, P2 p2, P3 p3, P4 p4)
{
Undoable* cmdPtr = new CommandType(p1, p2, p3, p4);
...
// init cmdPtr
(*cmdPtr)();
}
Basically the point would be to move the complexity inside CommandFactory, to keep the "client code" (the call to createCommand) as simple and short as possible.
Any ideas ?
Not sure I understand the question correctly, but this should work.
template<typename ParameterType1 , typename ParameterType2, typename ParameterType3 , typename ParameterType4>
class UndoableCommand
{
public:
UndoableCommand(ParameterType1 p1, ParameterType2 p2, ParameterType3 p3, ParameterType4 p4)
{}
};
class DoSomeStuff : public UndoableCommand< int ,double, std::string , int>
{
public:
DoSomeStuff(int p1, double p2, std::string p3, int p4)
:UndoableCommand(p1,p2,p3,p4)
{}
};
class CommandFactory
{
public:
CommandFactory()
{}
template <class CommandType, typename P1, typename P2, typename P3, typename P4>
void createCommand( std::string& description,P1 p1, P2 p2, P3 p3, P4 p4)
{
UndoableCommand<P1,P2,P3,P4> * cmdPtr = new CommandType(p1, p2, p3, p4);
}
};
Used as such
CommandFactory fact;
fact.createCommand<DoSomeStuff>(std::string("description"),1,2.0,std::string("3"),4);
这篇关于混合命令模式,出厂模式和模板在一起...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!