Why do most system architects insist on first programming to an interface?

后端 未结 16 2308
离开以前
离开以前 2020-12-05 00:32

Almost every Java book I read talks about using the interface as a way to share state and behaviour between objects that when first \"constructed\" did not seem to share a r

16条回答
  •  情歌与酒
    2020-12-05 01:24

    Programming to an interface means respecting the "contract" created by using that interface

    This is the single most misunderstood thing about interfaces.

    There is no way to enforce any such contract with interfaces. Interfaces, by definition, cannot specify any behaviour at all. Classes are where behaviour happens.

    This mistaken belief is so widespread as to be considered the conventional wisdom by many people. It is, however, wrong.

    So this statement in the OP

    Almost every Java book I read talks about using the interface as a way to share state and behavior between objects

    is just not possible. Interfaces have neither state nor behaviour. They can define properties, that implementing classes must provide, but that's as close as they can get. You cannot share behaviour using interfaces.

    You can make an assumption that people will implement an interface to provide the sort of behaviour implied by the name of its methods, but that's not anything like the same thing. And it places no restrictions at all on when such methods are called (eg that Start should be called before Stop).

    This statement

    Required for GoF type patterns, such as the visitor pattern

    is also incorrect. The GoF book uses exactly zero interfaces, as they were not a feature of the languages used at the time. None of the patterns require interfaces, although some can use them. IMO, the Observer pattern is one in which interfaces can play a more elegant role (although the pattern is normally implemented using events nowadays). In the Visitor pattern it is almost always the case that a base Visitor class implementing default behaviour for each type of visited node is required, IME.

    Personally, I think the answer to the question is threefold:

    1. Interfaces are seen by many as a silver bullet (these people usually labour under the "contract" misapprehension, or think that interfaces magically decouple their code)

    2. Java people are very focussed on using frameworks, many of which (rightly) require classes to implement their interfaces

    3. Interfaces were the best way to do some things before generics and annotations (attributes in C#) were introduced.

    Interfaces are a very useful language feature, but are much abused. Symptoms include:

    1. An interface is only implemented by one class

    2. A class implements multiple interfaces. Often touted as an advantage of interfaces, usually it means that the class in question is violating the principle of separation of concerns.

    3. There is an inheritance hierarchy of interfaces (often mirrored by a hierarchy of classes). This is the situation you're trying to avoid by using interfaces in the first place. Too much inheritance is a bad thing, both for classes and interfaces.

    All these things are code smells, IMO.

提交回复
热议问题