• 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

An elegant way to cast object types

 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a project that receives an object of the base class. There are several inherted class(es). There are timing issues(this is a packet sniffer) that necessitate collecting the objects as its base class.

Is there a good way to cast the object types. Right now I just have a method for each subclass type that casts it and then parses the data according to the packet type. It works, but is there a more elegant way to go about it?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a couple ideas for you. The first one is to try hard to figure out a way to just use polymorphism. What if a packet could just parse itself?

Failing that, you could look at the oft-reviled Visitor pattern (which I personally have a weak spot for.)

A third idea is a map with Class objects as the keys and "Parser" implementation objects as the values. When you get a packet, you look up a parse using map.get(packet.getClass()) .

Any of these sound good?
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for responding.

I wasn't very specific but a packet is an object, a base class in fact. To parse TCP header information in the Packet object, the base class would have to know about its child.

I will look into the Visitor pattern and your last idea seems off the mark for my project. Once the needed information from a Packet is obtained, the Packet is discarded.

Here is the API that I am using: http://netresearch.ics.uci.edu/kfujii/jpcap/doc/javadoc/index.html
[ February 26, 2008: Message edited by: David McCombs ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David: To parse TCP header information, the base class would have to know about its child.


I think what Ernest was trying to refer to is to use polymorphism to write various overloaded methods for different childs. You can call the same method for every object you receive, the relevant method will be called based on the instance of the object. (Polymorphic functions)
Infact this is what is a Visitor pattern!

David: your last idea seems off the mark for my project. Once the needed information from a Packet is obtained, the Packet is discarded.


Again, what Ernest meant was that in the map you put an instance of java.lang.Class as the key and value as the parser implementation for that class. When you get an object instance call Object.getClass() method on the instance. Then lookup the parser implementation from the map based on the looked up class instance.

In my personal opinion, this case is an absolute fit for the visitor pattern if you can not change the class implementations to introduce a parse() method in either the base class(an abstract method) or use an interface.

[ February 25, 2008: Message edited by: Nitesh Kant ]
[ February 26, 2008: Message edited by: Nitesh Kant ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To parse TCP header information, the base class would have to know about its child.



A base class should not be aware of its subclasses. If it is, then you should consider refactoring your design.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arman Sharif:


A base class should not be aware of its subclasses. If it is, then you should consider refactoring your design.



That was my point and the Packet class and its children are not my design. It is essentially a simple, useful PCAP wrapper.

Thanks for all the replies, it was helpful!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic