vendredi 10 août 2018

Java 8. Factory method pattern. Abstract class vs interface with default method

What is the best practice to use Factory Method pattern in Java8.

For example, which of two is better?

interface UserFactory {
  default String makeDialog() {
    return "Hello\n" + createUser().sayHi();
  }

  User createUser();
}

or

abstract class UserFactory {
  String makeDialog() {
    return "Hello\n" + createUser().sayHi();
  }

  abstract User createUser();
}

Aucun commentaire:

Enregistrer un commentaire