-- 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
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.
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.
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.