• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

polymorphism and sockets

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
John Kilbourne
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic