Interfaces with only one implementation

When using jmock, you can mock a particular class if you install a class improvisator. I noticed that the class humidifier is in an inherited package, so I really don't want to use it (especially because it is very easy to extract the interface using refactoring tools in my IDE). I also don't like instance variables of specific classes.

Extracting the interface, however, I noticed a pattern appearing in my code base. A lot of time an interface has only one implementing a specific class. I prefer to use the interface wherever possible, but it seems that there really are all these additional files. Also, it’s a little tedious to update the interface and update the implementation every time I want to add a new class to the class.

Is it just the price you pay for the right abstraction, or is there a better approach that I haven't thought about? Should all classes implement an interface, even if the only thing in the interface is getters / setters?

+5
source share
9 answers

- , , , - , . . , .

, , . , , - . ( ), .

, , - , , .

+9

, :

  • . , , .
  • . .
  • , .
  • , : ; ; mockability; .
  • , , , .
  • - , , API-, .
  • , .
  • - . , , , .

, , . , , .

+5

, .

, , ? , . , , .

, - . , .

+3

" ". , , . , , , , , . , , , .

, , , . , , , . , (.. ) .

, , . , , , (.. "extract interface..." IDE), . , , .

+3

, , , , . . .

interface IRunner {
      void run();
      int doOtherThing();
}

abstract class Thing implements IRunner {

    //this is class-specific
    abstract void run();
    //this is common to all 'Things'
    int doOtherThing() { return 0; }

    //as are these properties
    public int getProp() {...}
    public void setProp(int val) {...}

}

public class Goo extends Thing {
      public void run() {
         int i = getProp() + doOtherThing();
         makeMagicGoo(i);
      }

}
+1

, () , .

() , , . .

0

, , - : JDK ( Spring , ). - cglib , ; JDK .

, IDE , . , (Eclipse) , @Override . ( , , .)

0
0
source

All Articles