Home > OS >  Is it ok to Hard Couple a class implementation with its interface?
Is it ok to Hard Couple a class implementation with its interface?

Time:10-20

I try to understand others people code and I saw something quite strange in an implementation regarding how to create a builder. Is this design good?

public interface Car {
 static Builder createBuilder() {
     return new CarImpl.Builder();
 }
 interface Builder {
  /// Setters contract
 }
}

CodePudding user response:

Generally no, your interface should define the behaviour of a class (the signature of functions), for example in 'Car' exposing functions such as drive, stop, refuel, etc.

Your interface should not be aware of implementation details. This is the idea behind an interface - it abstracts away the implementation details so they are incapsulated in whomever layer that is responsible for the implementation, which leaves your interface clean, exposing only the what & not the how.

  • Related