File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes help with a design approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help with a design approach" Watch "help with a design approach" New topic
Author

help with a design approach

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

what I want to achieve?

-- Lets say I have a parent class P and 3 classes A, B and C - all the three classes have different method implementations.
-- I have a file of type .sample format which can be represented by the class "P" and is parsed to populate objects of type A or B or C in the parent class P.
Please note that at each point in time a file can be parsed to P with either A or B or C, all three cannot coexist and the decision to populate the objects will be read from the files first character.

I want to decide at runtime by the first character which object to populate. I want a design approach to go ahead with this implementation, will it be simillar to the List - ArrayList and linkedlist pattern? and can I use generics ? but then I have different methods implementations in each class what method signatures do I write in my interface... I am stuck... : (
any suggestions is appreciated thanks


Time is what we want the most, but what we use the worst. -- William Penn
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't understand your last few sentences at all, but up until then, it seemed like a good application of a Factory pattern. A static method decides which child class to create; the child classes don't have public constructors, so the client has to call the static method to get any instance of them.



[Jess in Action][AskingGoodQuestions]
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

And Voila! its done, its exactly what I wanted... thanks : )... and yes I am following the factory pattern here. Anyways please dont mind about the last few sentences, I was panicking : )

and a quick question, after I do this how do I know later in my code that the reference is of type A / B ? you aren't telling me to do an instanceof check are you ?
thanks again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The client code should not need to know whether it's got an A, B, or C; if it does, then there's a flaw in the design and you probably need to make changes elsewhere. Any time you're writing code that needs to know the difference between subclasses, consider how to "turn it inside out" and move the code into different implementations of another abstract method on P.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Recently I was going through factory patterns in Head first design pattern book. I found that creating instance of a class using switch case is not a factory pattern as against the general misconception. So P will have method d(), e() and f() and one getter-setter to set the instance of child class. This is d(), which has got different implementations in child classes A, B and C. Now from a class say M, you are reading the file extract to decide which class to call.

So code may look like

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ummmm... huh?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
sorry class M is not abstract and is a main class. It was copy paste of Ernest Friedman-Hill code.
 
Consider Paul's rocket mass heater.
 
subject: help with a design approach