i am a newbie to the design patterns and here is my question
if we have an abstract class with few classes that implement it and each of this classes has different attributes.
now i have another (Manager class) that holds an array of the abstract class and i want to put a search method in it … how can i do that without casting to the concrete classes ?
i have got 2 ideas:
First One : adding an extra levels of interfaces (ie. rather than casting to concrete class i will be casting to an interface ) which goes with the code to interface not implementation rule…
but this way when i add another class i will have to make an interface for it and i will also have to edit the manager (client) , which doesnt seem very good.
Second Solution:
it look somewhat strange and still needs enhancements but its main goal is to make the manager or any other client works with abstract class without knowing any thing about who extends it or its attributes.
the solutin is as folows :
each new item added will have to overide one interface that enforces it to generate a complete discription of its fields for example a car object will have to return
a hash map having the folowing
field : { fieldType , fieldValue }
example
- model : { text , “ford” }
- manifactureDate : { Date , “12/1/89” }
and each object will have also to implement a method called compareFields that take a
hash map like this and compare it to its field and return true or false.
now by this way i have solved many issues
-for the gui i will only have to make a rendering engine for this hashmap that can display
any item without having to know what its type. (again the gui is another client for the abstract class)
-for the search i can get a hash map that contain the fields the user enters
in the search form and loop on the abstract items and invoke the compare fieldmethod
i still don’t how i will handle the complex object (that have another object as its attributes)
i dont know what kind of pattern is this .. it is just an idea that i thought about.
EDIT : CONCRETE EXAMPLE
if i have an abstract item Class with a car and bus and boat that implements it,,and each of this classes has different attributes….
how can a manager for example traffic manager search for a certain item using the abstract class without casting to car or bus…
really sorry for the long question
Encapsulation
The OO principle of encapsulation states that you object should not expose its state to the outside. If your object epxose it internal information it breaks the encapsulation. What is still OK according to OO design is to pass the search criterion to the object and let it decides whether they match.
You can have an interator on all vehicles and retrieve the ones which match whithout breaking the encapsulation. Particular implementation of vehicles can still be reimplemented as desired.
Visitor
Otherwise, I would suggest you look at the Visitor pattern. The idea then is to traverse all the object and have an extra class handle the treatment for each specific type. This also breaks pure encapsulation (because the object need to expose its data to the visitor), but it’s much elegant.
Meta-programming
The idea of object which are self-describing is another concept, which is called meta-programming. The presentation layer then introspect other object to know how to handle them. This traditionally is considered an advanced OO technique. You could could create custom annotations to describe the field of your class, so that the presentation layer can dynamically render the appropriate label. The same idea is for instance used with hibernate annotations. Meta-programming need to be done carefully otherwise you run into other issue.
Insteanceof
Use
insteanceofis also a form of introspection (because you ask the object for its class) and is usually discouraged. Not because it is wrong in itself, but because it tend to be abused. Whenever possible, rely on traditional OO principles. Usinginstanceofabusively is a code smell.All in all, I would recommend to use a visitor for the search, and to not use meta-programming for the presentation layer and to instead create one simple page per type of vehicle.