I'm running into a problem understanding the semantics of Java's interfaces. I am modeling a problem with interfaces before I fill them in with code. I do this by seeking out the nouns in the problem and creating an interface for them, for example:
Every house has at least one person in it. Each house may also contain exactly one bicycle and may contain at least one car.
So, I would create the interfaces IHouse, IPerson, and IVehicle (since we may have different types of houses, peoples, and vehicles in the future).
The problem comes when I get into the adjectives of these nouns, for example:
Each house, bicycle, and car may have a certain color.
Instead of creating the following methods in each vehicle and house class, I create the following interface:
public interface IPainted {
public Paint getPaint();
public void setPaint();
}
and apply it to the vehicle and house interfaces:
public interface IVehicle extends IPainted {
}
Is this the best way of representing adjectives?
Yes. And to put both of your somewhat awkward designations of "noun" and "adjective" in the same bucket, use suffix "able" -- in this case public interface Paintable
. So all the properties of a paintable class that have to do with paintability should be encapsulated within the Paintable
interface which that class implements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With