Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with a design approach

 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummmm... huh?
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry class M is not abstract and is a main class. It was copy paste of Ernest Friedman-Hill code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic