• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S: Command Pattern...revisited

 
hatim osman
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi…
It’s been a while, I am almost done with my server, and I’d like to share my design. In a previous thread, Andrew suggested me a brilliant idea that is "Command Pattern". Since I am implementing a Socket based solution, it really proved to be very flexible. Andrew has this to say:

Now if you are developing a sockets solution, you already have at least two operations that you must support over the network - searching, and booking. So you already have to devise some method of encapsulating what the request is along with the relevant data, and sending that to the server. The server must be able to determine what you are requesting, and process it properly and then encapsulate a proper response (which may even be an exception) to send back to the client. And the client has to be able to differentiate between the two responses and handle them appropriately.


However, a word that Andrew had echoed influenced my overall design:

(which may even be an exception)


So here is part of my design:



I have excluded most of my exception handling, and just tried to give an overview of how I am implementing things. Yet, I am not sure if this confirms to the command pattern, or I just made up a pattern of my own . Please your comments, Andrew is this what you meant?

Thanx in advance
Hatim

[Andrew: tidied code]
[ October 17, 2005: Message edited by: Andrew Monkhouse ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hatim, please edit your post so that it does not extend right across the screen and off the other side. Break up the long lines in the quotations and/or code sections. Thanks.
 
hatim osman
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi�
Man, I am too sorry, I even hated reading my own post, it must be one of the ugliest posts ever. I am sorry about this. I�d like to share
my design. In a previous thread, Andrew suggested me a brilliant idea that
is "Command Pattern". Since I am implementing a Socket based solution,
it really proved to be very flexible. Andrew has this to say:

Now if you are developing a sockets solution, you already have at
least two operations that you must support over the network
searching, and booking. So you already have to devise some method
of encapsulating what the request is along with the relevant data,
and sending that to the server. The server must be able to determine
what you are requesting, and process it properly and then encapsulate
a proper response (which may even be an exception) to send back to
the client. And the client has to be able to differentiate between the
two responses and handle them appropriately.



However, a word that Andrew had echoed influenced my overall design:

(which may even be an exception)





So here is part of my design:


public interface ContractorServices {

public ContractorRecord book(int recNo, String id) throws
AlreadyBookedException, ContractorNotFoundException, ServerException;

public Set Search(SerachTerm term) throws ContractorNotFoundException,
ServerException;
}

public interface ContratorCommand implements Serializable{
public Object execute(ContractorServices services);

}

//and I have a book command

public class BookCommand implements ContractorCommand{
int recNo;String id;Connection conn;

public BookCommand(int recNo, String id, Connection c){
//initialize instance variables;} //returns the contractor the record
}

public Contractor book() throws AlreadyBookedException,
RecordNotFoundException, ServerException {

//serialize the command to the server
Object result = conn.send(this);

if (result instanceof Exception) {
//if the server returned an Exception, then throw it
}

return new ContractorRecord(recNo, (Contractor) result);
}

public void execute(ContractorServices services) {
services.book(recNo, id);}}

/*
The Server has a concrete singleton instance of
ContractorServices,�DefaultContractorServices.class�,
that wraps an instances of Data and it spawns a processor thread upon
accepting a connection passing to it the socket and the DefaultContractorServices instance
*/
public class Processor implements Runnable {
ContractorServices myOwnServices;

public Processor(Socket remoteRequest, ContractorServices services ) {

// initialize the instance variables
}

public void run() {

//cast the socket stream to an ObjectInputStreamObject
request = in.read();
if (request instanceof ContractorCommand) {
ContractorCommand command = (ContractorCommand) request;

/*
this call will automatically delegate the call to
the �DefaultContractorServices.class� instance, thus the client
call is silently invoked
*/

Object result = command.execute(myOwnService);

//send back the result
out.write(result);}
}

I have excluded most of my exception handling, and just tried to
give an overview of how I am implementing things. Yet, I am not
sure if this confirms to the command pattern, or I just made up
a pattern of my own. Please your comments, Andrew is this
what you meant?

Thanx in advance
Hatim
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hatim,

It is generally not a good idea to get too bogged down in getting some "perfect" representation of any pattern. Patterns are, by their very nature, abstract representations of what will be implemented. Consider the pattern for a door shown on a building - it generally appears as a quarter circle touching one wall and a vertical or horizontal line joining the other part of the wall and the other edge of the quarter circle - there is no way to tell from that whether you are looking at a glass door, a heavy wooden door, a lightwood wooden door, a screen door ... And yet they could all be real doors.

Having said all that, yes I can see the usage of the command pattern in your initial design.

Normally though, you want the object being transferred to be as lightweight as possible. Many people choose to use a Value Object expressly for this purpose (the Value Object pattern is also referred to as the Transfer Object pattern - you may be able to see why in this context). Since you are transferring your entire BookCommand object, you would probably want to mark much of it as transient so that only the necessary parts get serialized.

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic