Some of my classes should be treated differently. Which solution is better:
- Introduce new interface to my class hierarchy and check whether the class implements it or not, using RTTI (runtime time identification)
- Add a method which returns boolean value that indicates whether this class should be treated normally or deserves special treatment
The following examples illustrates the above situations:
1.
interface SpecialTreatment {}
class Base {}
class Special extends Base implements SpecialTreatment {}
class Normal extends Base {}
Base reference = new Special();
if(reference instanceof SpecialTreatment)
// do something special
else
// normal class
2.
interface Treatment {
boolean special();
}
class Base {}
class Special extends Base implements Treatment {
boolean special() { return true; }
}
class Normal extends Base implements Treatment {
boolean special() { return false; }
}
Treatment reference = new Special();
if(reference.special() == true)
// do something special
else
// normal class