943,747 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2600
  • C++ RSS
Aug 9th, 2009
0

List of Command

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. class GenericCommander;
  3. class ComamndList : public GenericCommander {};
  4. class Command : public GenericCommander {};
  5. This approach works with single command and list of command.
  6.  
  7. Code:
  8.  
  9. class GenericCommand
  10. {
  11. public:
  12. GenericCommand();
  13. virtual ~GenericCommand();
  14.  
  15. virtual void ExecuteSignalCallBack(int) = 0;
  16.  
  17. };
  18.  
  19. template <typename T>
  20. class Command : public GenericCommand
  21. {
  22. public:
  23. typedef boost::shared_ptr<T> objectPtr;
  24.  
  25. private:
  26.  
  27. objectPtr targetObjectCommandCallerPtr;
  28.  
  29. void (T::*MemberFuncPtr)();
  30.  
  31. public:
  32. Command();
  33. explicit Command(objectPtr& );
  34. ~Command();
  35.  
  36. void ExecuteSignalCallBack(int);
  37.  
  38. };
  39.  
  40. template <typename T>
  41. Command<T>::Command() : targetObjectCommandCallerPtr(objectPtr())
  42. {
  43. }
  44.  
  45. // =========================================
  46. template <typename T>
  47. Command<T>::Command(objectPtr& user)
  48. : targetObjectCommandCallerPtr(objectPtr(user))
  49. {
  50. }
  51.  
  52. // =========================================
  53. template <typename T>
  54. Command<T>::~Command()
  55. {
  56. }
  57.  
  58. // =========================================
  59. template <typename T>
  60. void Command<T>::ExecuteSignalCallBack(int choice)
  61. {
  62. (*targetObjectCommandCallerPtr.*MemberFuncPtr)();
  63.  
  64. }
  65.  
  66. template <typename T>
  67. class Command;
  68.  
  69. template <typename T>
  70. class CommandList : public GenericCommand
  71. {
  72. public:
  73. typedef boost::shared_ptr<T> objectPtr;
  74.  
  75. private:
  76. Command<T> cont[15];
  77.  
  78. public:
  79. CommandList();
  80. explicit CommandList(objectPtr&);
  81.  
  82. ~CommandList();
  83.  
  84. void ExecuteSignalCallBack(int);
  85. // void CallCommand(int );
  86.  
  87. };
  88.  
  89. // =========================================
  90. template <typename T>
  91. CommandList<T>::CommandList()
  92. : cont(Command<T>())
  93. {
  94. }
  95.  
  96. // =========================================
  97. template <typename T>
  98. CommandList<T>::CommandList(objectPtr& userTargetObjectPtr)
  99. : cont(Command<T>(userTargetObjectPtr))
  100. {
  101. }
  102.  
  103. // =========================================
  104. template <typename T>
  105. CommandList<T>::~CommandList()
  106. {
  107. }
  108.  
  109. // =========================================
  110. template <typename T>
  111. void CommandList<T>::ExecuteSignalCallBack(int choice)
  112. {
  113. (cont[choice - 1].*MemberFuncPtr)();
  114. }
  115.  
  116. // =========================================
  117.  
  118. *
  119. Not recommended to specialize
  120. Function Template
  121. */
  122.  
  123. // ============ General Command ============
  124. template <typename T>
  125. boost::shared_ptr<GenericCommander>
  126. CommandFactory(boost::shared_ptr<T>& targetObject,
  127. void (T::*MemberFuncPtr)() )
  128. {
  129. // To create one command only
  130. }
  131.  
  132. // =========== Generic Command List =========
  133. template <typename T>
  134. boost::shared_ptr<GenericCommander> CommandListFactory
  135. (boost::shared_ptr<T>& targetObject)
  136. {
  137.  
  138. }
  139.  
  140. // =========== Administrator Command ========
  141. template <>
  142. boost::shared_ptr<GenericCommander> CommandListFactory<Administrator>
  143. (boost::shared_ptr<Administrator>&
  144. targetObject) void
  145. (T::*MemberFuncPtr)() )
  146. {
  147. // Regiter Administrator MF
  148. /*
  149.   &Administrator::Add
  150.   &Administrator::Delete
  151.   &Administrator::Modified
  152.   */
  153. }
  154.  
  155. // ============= HR Command =================
  156. template <>
  157. boost::shared_ptr<GenericCommander> CommandListFactory<HumanResource>
  158. (boost::shared_ptr<HumanResource>&
  159. targetObject) void
  160. (T::*MemberFuncPtr)() )
  161. {
  162. // Regiter HR MF
  163. }
  164.  
  165. // ============== Staff Command ============
  166. template <>
  167. boost::shared_ptr<GenericCommander> CommandListFactory<Staff>
  168. (boost::shared_ptr<Staff>&
  169. targetObject) void
  170. (T::*MemberFuncPtr)() )
  171. {
  172. // Register Staff MF
  173. }

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.
Similar Threads
Reputation Points: 3
Solved Threads: 1
Light Poster
Peter_APIIT is offline Offline
40 posts
since Feb 2008
Aug 9th, 2009
0

Re: List of Command

Read this Template FAQ
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 10th, 2009
0

Re: List of Command

I do reading that but does not help any.
Reputation Points: 3
Solved Threads: 1
Light Poster
Peter_APIIT is offline Offline
40 posts
since Feb 2008
Aug 10th, 2009
0

Re: List of Command

sorry PETER_APIIT but your design has several flaws, describe you requirement a bit, then I will tweak your design to meet your needs.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 11th, 2009
0

Re: List of Command

OK. Can you show me Why i my design was wrong ?

I have a class three class which are Administrator, HR and Staff. Therefore, i would like to create a command pattern that handle the request for each of this class. Each class has several MF.

My approach like this.

class GenericCommand;

// Use to handle single Command
template <typename T>
class command : public GenericCommand;


// Use to handle several command build before it called
template <typename T>
class CommandList: public GenericCommand;


Therefore, i code a three function templates to initialize the command with respective MF for each class.


My final choice wil be using single command but i will create the type on the fly. Another final choice is i have to create three class hierarchy which initialize the respective MF in each command.

I hope my explanation is clear to you.

Please let me know if you have any inquiry.
Last edited by Peter_APIIT; Aug 11th, 2009 at 8:30 am.
Reputation Points: 3
Solved Threads: 1
Light Poster
Peter_APIIT is offline Offline
40 posts
since Feb 2008
Aug 11th, 2009
0

Re: List of Command

I don't think Command Design pattern best matches your need,

what you can do is simply create a Class that holds the map of Objects of type administrator, HR, Staff and value part contains the vector of member funciton pointers. now in each object administrator, HR or Staff create some integral type upon which you'll decide which member function would be executed for selected object. or either call every registered member funciton.

C++ Syntax (Toggle Plain Text)
  1. //something like below.
  2. typedef void (T::*MEMFuncPtr)(int id);
  3. map<T, vector<MEMFuncPtr> > fMap;
check for the Chain of Responsiblity Design pattern. your requirement matches that pattern.

As far as Factory Method is concern its simple man. provide the Create function in every member class and create the abstract factor which would be templatized.
Last edited by Laiq Ahmed; Aug 11th, 2009 at 8:24 am.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 12th, 2009
0

Re: List of Command

Yes, i try this before because i have three classes which are Administrator, HR and Staff.

How can i create one map with hold three different type of MF ?

The fact is there are either one creation of administrator, hr or staff object at run time only. After that i want to issue command for that object instance only.

Therefore, if i uses your approach, i need to create three classes and do some polymorphism which choose what class to execute the command.

Thanks.
Last edited by Peter_APIIT; Aug 12th, 2009 at 4:50 am.
Reputation Points: 3
Solved Threads: 1
Light Poster
Peter_APIIT is offline Offline
40 posts
since Feb 2008
Aug 12th, 2009
0

Re: List of Command

hmmmmmmmmmm, I've come up with some solution to you problem hope this might help you.
cpp Syntax (Toggle Plain Text)
  1.  
  2. class ICommand {
  3. public:
  4. virtual ~ICommand() = 0 { }
  5. virtual void ExecuteCallBack(int iCommandId) = 0;
  6. };
  7.  
  8. // Command Base Object.
  9. template<class T>
  10. class Command : public ICommand {
  11. typedef void (T::*Delegate)(int a);
  12. vector<Delegate> memFunctions;
  13. public:
  14. Command() { }
  15. void RegisterCallBack(Delegate func) {
  16. memFunctions.push_back(func);
  17. }
  18.  
  19. Delegate GetCallBack(int idx) {
  20. // add checking of constraint.
  21. return memFunctions[idx];
  22. }
  23.  
  24. // you can add functions like
  25. // void executeAllCallBacks() = 0;
  26. virtual void ExecuteCallBack(int iCommandId) = 0;
  27.  
  28. };
  29.  
  30.  
  31. class Administrator : public Command<Administrator> {
  32. protected:
  33.  
  34. virtual void ExecuteCallBack(int iCommandId) {
  35. (this->*GetCallBack(iCommandId))(8);
  36. }
  37.  
  38. Administrator() { }
  39. public:
  40.  
  41.  
  42.  
  43. void Add(int a) {
  44. cout << "Administrator Add"<<endl;
  45. }
  46.  
  47. void Remove(int a ) {
  48. cout << "Administrator Remove"<<endl;
  49. }
  50.  
  51. static Command<Administrator>* GetInstance() {
  52. return new Administrator;
  53. }
  54. };
  55.  
  56.  
  57. class HR : public Command<HR> {
  58. protected:
  59. virtual void ExecuteCallBack(int iCommandId) {
  60. (this->*GetCallBack(iCommandId))(8);
  61. }
  62. HR() { }
  63. public:
  64.  
  65. void Add(int a) {
  66. cout << "HR Add"<<endl;
  67. }
  68.  
  69. void Remove(int a ) {
  70. cout << "HR Remove"<<endl;
  71. }
  72.  
  73. static Command<HR>* GetInstance() {
  74. return new HR;
  75. }
  76. };
  77.  
  78.  
  79.  
  80.  
  81. int main ()
  82. {
  83. Command<Administrator>* obj = Administrator::GetInstance();
  84. obj->RegisterCallBack(&Administrator::Add);
  85. obj->ExecuteCallBack(0);
  86.  
  87. Command<HR>* obj2 = HR::GetInstance();
  88. obj2->RegisterCallBack(&HR::Add);
  89. obj2->ExecuteCallBack(0);
  90.  
  91. // now the interesting Part.
  92. vector<ICommand*> vec;
  93. vec.push_back(obj);
  94. vec.push_back(obj2);
  95.  
  96. for(size_t i=0; i<vec.size(); ++i) {
  97. vec[i]->ExecuteCallBack(0);
  98. }
  99.  
  100. return 0;
  101. }

Hope this help, you can further comment on this as well.
Last edited by Laiq Ahmed; Aug 12th, 2009 at 6:35 am. Reason: don't need the T* _object.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 13th, 2009
0

Re: List of Command

Actually, i have the problem in my mind. By the way, thanks for your help.

Your solution is OOP approach and the approach i did before is Generic Programming approach.

I don't think chain of responsibility is suitable for this design because there are no list of handler that request pass along that handler.

By the way. Thanks. Solved.
Reputation Points: 3
Solved Threads: 1
Light Poster
Peter_APIIT is offline Offline
40 posts
since Feb 2008
Aug 13th, 2009
0

Re: List of Command

Your solution has design flaw and it is consider as classical undoable command.

By the way, thanks for your help.

Sharing is caring.
Last edited by Peter_APIIT; Aug 13th, 2009 at 12:17 pm.
Reputation Points: 3
Solved Threads: 1
Light Poster
Peter_APIIT is offline Offline
40 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem while running graphic programs in turbo c++
Next Thread in C++ Forum Timeline: my assgment-- animal show ticketing system





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC