Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Binary datafile and object creation according to binary tag ?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary datafile and object creation according to binary tag ?

    Dear all,

    I am new in the world of Java, so apologies my ignorance.

    I've developped a c++ code for scientific visualization (with opengl). My goal would be to convert this soft from C++ to java, because I am tired to maintain it on 3 different operating systems (Windows, Linux and MacOS). However, before trying to convert such a complex program, I would like to make sure it can be converted with no dommages for the current users.

    The heart of the code is the reading of a (custom) binary file containing all the objects to be displayed in a very compact format (that I don't want to describe here). But the main thing is that while reading this binary file, I am creating objects from different class (depending on the first byfe of the chunks of data that I am reading), all these objects inherits from a common base class.
    In order to initialize the objects using the "right" class, is to do a switch of the first byte and depending on the value of that byte, I initialize my object using the constructor of the right class.
    What I would like to know is:
    1) can I use a similar strategy in java?
    2) is there a better strategy that I could use ?
    --> typically I would like to have a code that find itself which constructor/object to use. That would avoid me to systematically have to add a new entry inside my "switch". At the end, I'd like to have these class as pluggins and to allow the users to define a new type of object (a new class) without having to change any part of the software code except for their simple pluggin.

    Any suggestion for a way to do this properly?

    Thanks in advance for your help,
    Best regards,
    Loic


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Binary datafile and object creation according to binary tag ?

    Should I understand correctly, there are probably several ways to accomplish this. One way to consider is to create a factory class - from the main perspective of the program it can pass the byte value to the factory and get the newly created object in return. From the plugin perspective, this factory class can read the plugins at runtime and see which is available. Depending upon how these classes are instantiated, this factory class could have a simple HashMap, keyed with the byte and valued to a) an interface whose definition returns a new object of said class or b) a Class object which can be used to instantiate the new class via reflection - this avoids the use of a always extending switch statement and is much more dynamic when it comes to loading things dynamically at runtime.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary datafile and object creation according to binary tag ?

    very interesting strategy...
    just a question... how the factory can know which are the pluggins to load? and how to actually load them?
    can you point me to some documentation? or give me some keyword to google for?

    Thanks,
    Loic

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Binary datafile and object creation according to binary tag ?

    Quote Originally Posted by loicus View Post
    very interesting strategy...
    just a question... how the factory can know which are the pluggins to load? and how to actually load them?
    can you point me to some documentation? or give me some keyword to google for?

    Thanks,
    Loic
    Its all really dependent upon a lot of factors - the above is just one strategy, and it can get a lot more complex.

    How I have implemented a plugin architecture in the past, is to have a plugin folder in the same directory as as the main jar...the plugin folder is read at startup, all classes added to the classpath, and the corresponding Class object (see link below) representing the class stored in memory. When a class is needed, it is instantiated using the newInstance() method (this would all be encapsulated within a Factory class). The requirement here is that all plugin classes must extend a Plugin class (or implements a Plugin interface), so it can then be cast to the appropriate type and the methods which are needed abide by a given contract (aka they must implement a given number of methods that are required by the application)

    Class (Java Platform SE 6)
    Trail: The Reflection API (The Java™ Tutorials)

    If you google terms like "java load jar class at runtime reflection" you might find some code snippets and such to help you decide if this is the route you wish to pursue.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary datafile and object creation according to binary tag ?

    thank you!

Similar Threads

  1. Binary Search Help
    By OwsumEmam in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 31st, 2011, 11:01 PM
  2. Binary Search Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2011, 04:03 PM
  3. Looping object creation
    By Charlie in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2010, 11:12 AM
  4. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM