Given the following code:
class AnimalDescriptor {
String species;
boolean hasLegs;
boolean hasTeeth;
boolean hasHair;
public AnimalDescriptor(String species, boolean hasLegs,
boolean hasTeeth, boolean hasHair) {
this.species = species;
this.hasLegs = hasLegs;
this.hasTeeth = hasTeeth;
this.hasHair = hasHair;
}
class Animal {
AnimalDescriptor control;
String name;
int numberOfLegs;
int numberOfTeeth;
String colorOfHair;
}
class doStuff {
public void go() {
AnimalDescriptor dog = new AnimalDescriptor("dog", true, true, true);
AnimalDescriptor pirahna = new AnimalDescriptor("pirahna", false, true, false);
Animal fido = new Animal();
fido.control = dog;
fido.name = "Fido";
fido.numberOfLegs = 4;
fido.numberOfTeeth = 42;
fido.colorOfHair = "black"; //All good here
Animal bob = new Animal();
bob.control = pirahna;
bob.name = "Bob";
bob.numberOfTeeth = 42; //So far so good
bob.numberOfLegs = 2; //This should pass compilation, but fail
//validation at run-time, since it is
//nonsensical
}
Is there a pattern name for what I'm doing here? I've tried searching variations on my question title but haven't gotten anywhere.
Obviously, this is an extremely simple and abbreviated example, AnimalDescriptor should have a bool .validate(Animal a)
method, as well, and Animal should have an .isPropertyAvailable(String propertyName)
method, among other additions. I didn't want to bog everything down with a 500-line minimal-case example.
There's no way that I'm the first or only person to do this, and I'm mainly looking for something I can latch on to for further research into best practices, implementation pitfalls, etc.
EDIT: To provide a little more clarity, in my system, there are ~20 booleans in the "control" class (AnimalDescriptor). I'm tracking ~106 items (Animals) in ~103 categories (AnimalDescriptors) that share a pool of common properties, but each item only has the properties that are designated by its "control" configuration. Plus, the system should be able to expand to, say, 104 categories (AnimalDescriptors) over the next 5 years. We have a functional (if ugly) implementation, so I don't need validation or exception how-tos -- I'm more interested in how this problem has been approached in other domains/applications, benefits of implementation in a functional (as opposed to object-oriented) context, best practices, etc. It seems like there should be a name for this, but I don't know what it is.