5

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?

sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 'best' is subjective... but this is a fine way. – Randy Jan 14 '13 at 21:04
  • 3
    I think it is right, but the setPaint method probably will be void an with a Paint argument – raven1981 Jan 14 '13 at 21:07
  • Just to add, you may find the [Decorator pattern](http://en.wikipedia.org/wiki/Decorator_pattern) interesting while learning about interfaces and abstract classes. – Fallup Jan 14 '13 at 21:21
  • I also found this: http://c2.com/cgi/wiki?InterfacesShouldBeAdjectives – sdasdadas Jan 14 '13 at 21:43
  • 2
    Using `I` in front of interface names http://stackoverflow.com/questions/2814805/java-interfaces-implementation-naming-convention/2814831#2814831 – Steve Kuo Jan 14 '13 at 21:47
  • I can appreciate the argument but some people correctly point out that a Vehicle interface is not exactly a Vehicle (class). Rather, it's a blueprint for a Vehicle. When the English equivalent presents itself (in this case Vehicle works nicely to represent an abstraction of Bicycle and Car) the I seems needless. However, when there is no suitable English word for the abstract (think IPerson and Person) then I feel as though the I may be necessary. – sdasdadas Jan 14 '13 at 21:54
  • @sdasdadas Then perhaps you should put `C` in front of all class names – Steve Kuo Jan 16 '13 at 01:51
  • @SteveKuo That doesn't follow at all. The point that I was making is that sometimes it is impossible to name an interface without an I, because the English (or the domain) language doesn't allow for it. – sdasdadas Jan 16 '13 at 15:33

1 Answers1

2

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.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • 2
    Thank you - the naming conventions works for things such as Paintable, Selectable, Identifiable, etc. but what happens if each Vehicle contains a list of its parts? It can't be named IPartable...that's where I find the *able naming convention goes out the IWindow. – sdasdadas Jan 14 '13 at 21:04
  • 1
    `Vehicle` should be an abstract construct as well, so either an interface or (more suitably) an abstract class. I would then situate the parts-related methods in `Vehicle` and have the concrete implementation either extend `Vehicle` if it is an abstract class or implement both `Paintable` and `Vehicle` if it is an interface. – amphibient Jan 14 '13 at 21:13