I wrote a command pattern to my class Administrator, HR and Staff.
Each of it has several MF but command only operate on single MF.
Therefore, i got this design.
I got the solution like this. CommandList composes Vector of command.
class GenericCommander;
class ComamndList : public GenericCommander {};
class Command : public GenericCommander {};
This approach works with single command and list of command.
Code:
class GenericCommand
{
public:
GenericCommand();
virtual ~GenericCommand();
virtual void ExecuteSignalCallBack(int) = 0;
};
template <typename T>
class Command : public GenericCommand
{
public:
typedef boost::shared_ptr<T> objectPtr;
private:
objectPtr targetObjectCommandCallerPtr;
void (T::*MemberFuncPtr)();
public:
Command();
explicit Command(objectPtr& );
~Command();
void ExecuteSignalCallBack(int);
};
template <typename T>
Command<T>::Command() : targetObjectCommandCallerPtr(objectPtr())
{
}
// =========================================
template <typename T>
Command<T>::Command(objectPtr& user)
: targetObjectCommandCallerPtr(objectPtr(user))
{
}
// =========================================
template <typename T>
Command<T>::~Command()
{
}
// =========================================
template <typename T>
void Command<T>::ExecuteSignalCallBack(int choice)
{
(*targetObjectCommandCallerPtr.*MemberFuncPtr)();
}
template <typename T>
class Command;
template <typename T>
class CommandList : public GenericCommand
{
public:
typedef boost::shared_ptr<T> objectPtr;
private:
Command<T> cont[15];
public:
CommandList();
explicit CommandList(objectPtr&);
~CommandList();
void ExecuteSignalCallBack(int);
// void CallCommand(int );
};
// =========================================
template <typename T>
CommandList<T>::CommandList()
: cont(Command<T>())
{
}
// =========================================
template <typename T>
CommandList<T>::CommandList(objectPtr& userTargetObjectPtr)
: cont(Command<T>(userTargetObjectPtr))
{
}
// =========================================
template <typename T>
CommandList<T>::~CommandList()
{
}
// =========================================
template <typename T>
void CommandList<T>::ExecuteSignalCallBack(int choice)
{
(cont[choice - 1].*MemberFuncPtr)();
}
// =========================================
*
Not recommended to specialize
Function Template
*/
// ============ General Command ============
template <typename T>
boost::shared_ptr<GenericCommander>
CommandFactory(boost::shared_ptr<T>& targetObject,
void (T::*MemberFuncPtr)() )
{
// To create one command only
}
// =========== Generic Command List =========
template <typename T>
boost::shared_ptr<GenericCommander> CommandListFactory
(boost::shared_ptr<T>& targetObject)
{
}
// =========== Administrator Command ========
template <>
boost::shared_ptr<GenericCommander> CommandListFactory<Administrator>
(boost::shared_ptr<Administrator>&
targetObject) void
(T::*MemberFuncPtr)() )
{
// Regiter Administrator MF
/*
&Administrator::Add
&Administrator::Delete
&Administrator::Modified
*/
}
// ============= HR Command =================
template <>
boost::shared_ptr<GenericCommander> CommandListFactory<HumanResource>
(boost::shared_ptr<HumanResource>&
targetObject) void
(T::*MemberFuncPtr)() )
{
// Regiter HR MF
}
// ============== Staff Command ============
template <>
boost::shared_ptr<GenericCommander> CommandListFactory<Staff>
(boost::shared_ptr<Staff>&
targetObject) void
(T::*MemberFuncPtr)() )
{
// Register Staff MF
}
1. What approach replaces function template specialization ?
2. How to implement the CommandFactory ?
Usage Code:
share_ptr<GenericCommander> GCPtr = CommandFactory(thePtr);
GCPtr->ExecuteSignalCallBack(int);
Thanks for your help.