Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

Constructing an object with three different member variables

Hi Experts,

I have to provide an API to users who need to send some command to the server.
the commands will be of the form
class API
{
   void sendWrite()
   void sendUpdate()
   void sendRead()
   void sendDelete()
};


I need to construct an object with the following which is the command to be send to a server.  The command is of this form(i.e, three separate lines with the following format)
msg:::write
id:::123
data::dataValue

msg is one of  write, read, delete, update, etc.  
id is not relevant for now.
The dataValue may be a string, int or a json encoded string. I will be setting this data field according the command.

I have another class that actually send this string to the server.

bool cmdSender::sendCommand()
{

}

What I am trying to do is to create the commandMsgs and
do something like
sender->sendCommand(cmd.getString());

So, I need to create a Command class and constuct it as per the request.  What is the better way to do it.

class Command
{
       

}

So, I need to create the Command objects by simply passing

Command *cmd = new Command(write, 123, dataValueString);
then call sendCommand(cmd->getString())  or something like this.  
SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might want to look into using a Factory design pattern. Each type of command has its own class, all derived from a base "Command" class - a "Command_factory" class is built which creates objects of the correct type and returns a "Command" pointer. Another design pattern of use could be the Command pattern.

I recently used the Factory and Command design patterns for a web-update application. I found them to be a little extra work than the simple design I originally envisaged, but the benefits were realised as I added new types of commands to my application without changing the application logic. The command logic was encapsulated in the new command classes I was creating.

Cheers,
  Chris
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial