aspose file tools*
The moose likes Beginning Java and the fly likes polymorphism and sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "polymorphism and sockets" Watch "polymorphism and sockets" New topic
Author

polymorphism and sockets

John Kilbourne
Ranch Hand

Joined: Aug 22, 2001
Posts: 30
I've lusted after this book for some weeks; I've used it by sitting at the Border's a number of times.
I am writing a basic client-server application in which the client sends different "types" of messages to the server ("Update record", "New record", "Delete record", etc.) I indicate the message type with a char variable;
char 'A' = SHOWALL;
in the header of the message. I send the message as a packet, with an int header giving the packet length, the char as noted giving the message type, then the payload, which could be null (as in a "BYE" message, or SHOWALL), or may contain data (in a DELETE or NEWRECORD message).
Anyway, I have a complex switch statement in the server which parses the message, finding the char COMMAND, and calling a set of methods appropriate to each type
One of my java books suggests that a complex case-switch statement indicates type-checking, and an opportunity to use polymorphism. How can I make my messages into different types (DeleteMessage, ShowAllMessage, UpdateMessage, etc.) either implementing or extending a Message interface (or abstract class), with one method, handleMessage()?
It seems that to construct the Message object, I have to go throught the same complex case-switch statement I described earlier, so I don't see the practical or aesthetic value of using polymorphism. On the other hand, I'd like to learn object oriented metodology, so I can feel like a professional some day.
Thanks in advance.
John
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
Originally posted by John Kilbourne:
I've lusted after this book for some weeks; I've used it by sitting at the Border's a number of times.
I am writing a basic client-server application in which the client sends different "types" of messages to the server ("Update record", "New record", "Delete record", etc.) I indicate the message type with a char variable;
char 'A' = SHOWALL;
in the header of the message. I send the message as a packet, with an int header giving the packet length, the char as noted giving the message type, then the payload, which could be null (as in a "BYE" message, or SHOWALL), or may contain data (in a DELETE or NEWRECORD message).
Anyway, I have a complex switch statement in the server which parses the message, finding the char COMMAND, and calling a set of methods appropriate to each type
One of my java books suggests that a complex case-switch statement indicates type-checking, and an opportunity to use polymorphism. How can I make my messages into different types (DeleteMessage, ShowAllMessage, UpdateMessage, etc.) either implementing or extending a Message interface (or abstract class), with one method, handleMessage()?
It seems that to construct the Message object, I have to go throught the same complex case-switch statement I described earlier, so I don't see the practical or aesthetic value of using polymorphism. On the other hand, I'd like to learn object oriented metodology, so I can feel like a professional some day.
Thanks in advance.
John

Part of the difficulty here arises from the use of raw sockets, which are really an I/O mechanism, to try to implement what is essentially remote procedure call. I'll talk about alternatives in a minute, but first a word on using sockets.


You don't need to use polymorphism to create the objects, as they would normally be created during the operation of your program. Using your example, when the user clicks on a BYE button in your example, the action handler for that button would create a "LogoutMessage" (probably a subclass of Message).
You could use Object Serialization (see Recipe 15.6 in the Java Cookbook) to transfer these objects. But you'd need a series of "if" statements using instanceof to decode the received results.


Another approach is to use RMI (Chapter 22 of the Cookbook). RMI fits well in the OO paradigm; you would define a single interface called, say, MyMessaging, and it would include methods to do all the functions. Then you just call these methods and RMI takes care of sorting out the different function calls and their arguments.


A whole other approach would be to use Java Messaging Service. JMS is a large-scale distributed message processing service,
an example of MOM (Message-Oriented Middleware). JMS handles things like reliable messages. With JMS you can send your messages as text (as you do now) or as Java objects.


-- Ian
------------------
Ian Darwin,
Author of Java Cookbook: Solutions and Examples for Java Developers


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
John Kilbourne
Ranch Hand

Joined: Aug 22, 2001
Posts: 30
Thank you. I am a newbie, so appreciated hearing about Java Messaging. I have to come across an idea a few times before there is a space for it sink in.
What is a remote procedure call? I see that a lot, associated with RMI of course, but don't really understand when it would be used. I was surprised to learn that I am trying to do it myself. Is there a relationship between RMI, CORBA and sockets? I am using words I don't fully underfstand, but I think they are related.
John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism and sockets