Using the Command Pattern and Sockets?

K

Ken Adams

Ok, I don't think I understand the Command Pattern exactly but. Basically I
was told the best way to send objects in a Client/Server System was to use
ObjectOutputStream and send Command objects which will have an execute
method on them and then when the object is passes just call the exectute
method for that object. The only problem I have, is how to you get a
refrence to an object to invoke inside the execute method since the object
you want to invoke is not locally but on the server accross the network. And
I want to do this using Sockets instead of using RMI for learning purposes.

Any suggestions?

Thanks a bunch
 
P

Phil Staite

As I understand it, without RMI you have to make sure that both the tx
and rx sides have the same class set. That is, there's no dynamic
loading of classes from the server. Otherwise, on the rx side, it won't
understand the object it is pulling off the io device.

Now, since the rx side won't know what command is coming across, you'll
have to abstract it. That is, create a Command interface with the
Execute() method on it. Then make sure all your command objects
implement that interface.

Most likely, you'll have to pass some object(s) to Execute() for the
commands to work on. ie. a facade to the "system" on the rx side so
that the commands can make use of the services to execute.
 
K

Ken Adams

Phil Staite said:
As I understand it, without RMI you have to make sure that both the tx and
rx sides have the same class set. That is, there's no dynamic loading of
classes from the server. Otherwise, on the rx side, it won't understand
the object it is pulling off the io device.

Now, since the rx side won't know what command is coming across, you'll
have to abstract it. That is, create a Command interface with the
Execute() method on it. Then make sure all your command objects implement
that interface.

Most likely, you'll have to pass some object(s) to Execute() for the
commands to work on. ie. a facade to the "system" on the rx side so that
the commands can make use of the services to execute.
Yeah that makes sense, but my problem involved the last paragraph. So the
client say writes the Command object to the stream, and should it pass in an
object for the server to invoke the execute method against, or does the
server pass the object into the execute method and if so, how do you know
what type of object to pass in, since the Command objects no doubt can act
on different types of objects.
 
T

Tilman Bohn

Yeah that makes sense, but my problem involved the last paragraph. So the
client say writes the Command object to the stream, and should it pass in an
object for the server to invoke the execute method against, or does the
server pass the object into the execute method and if so, how do you know
what type of object to pass in, since the Command objects no doubt can act
on different types of objects.

Ok, I'm not sure if I completely understand your problem, so maybe
this is completely beside the point for you. But anyway:

The normal way is a no-arg execute(), but of course you can change that
if you have a reason. The standard OO expectation would be that the
Command object knows what other resources it needs to talk to to do its
thing. If in your case the caller needs to specify other parameters for
the Command to work with, you have to pass them in in _some_ way. One
of the possible ways is as an arg to the execute() method.

If what you call the client usually specifies other objects for the
Command to use, it should call the relevant methods on the Command
object before it gets sent. And you will then have to take care of that
data in your marshalling and unmarshalling. After all, that's the
whole point -- once you have the Command object, it should be able
to execute(). Everything it needs from the originating side is
supposed to already be encapsulated in it.

Cheers, Tilman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top