Java Interface Design - Helper Methods

user2205316

I have an interface like so

public interface Manager {
    public void manage();
}

Now, all Managers will need to load work to manage, however, I have mixed feelings about adding public void loadWork() to the interface...

On one hand, all Managers will do this, but on the other hand, users of a Manager class will not need to know about loadWork().

Question: Is it bad practice to add "helper" or "setup" type methods to an interface?

Pau Carre

It's not always a bad idea to add "setup" methods in an interface. For example, Java EE has an interface called ServletContextListener that is purely meant to make setup and shut down. It's even sometimes acceptable to make interfaces with methods you should actually never directly call such as the Runnable or the Callable interface.

Being said that, it seems is that you want to force your developers to implement a loadWork() method in Manager but you also want to hide it from the class' users. As you say, one option is adding the method in the interface but this way the method will be accessible (which you don't want). If you don't want the method to have visibility I see two options:

  1. Make the class Manager an abstract class and add a loadWork() protected method.
  2. Create an interface called LoadWorker with a method loadWork(). Then create an abstract class AbstractManager that implements Manager and has as a private/protected LoadWorker field. This way, even though loadWork() is public, it's not accessible from AbstractManager's users as it is called through a protected/private field (LoadWorker).

At the end it comes to a balance between overengineering and good design. It's up to you to take the decision following the specific needs. Nevertheless, there is no 'perfect solution'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursion in helper methods (Java)

From Dev

How to use Java Helper methods?

From Dev

Strange interface design Java

From Java

Abstract methods in a Java interface

From Java

Conflicting interface methods in Java

From Java

Optional Methods in Java Interface

From Java

Optional Methods in Java Interface

From Dev

overriding methods in Interface java

From Dev

Is there any design pattern for using helper methods in models in Rails?

From Dev

Helper methods for creating tuples and tupled functions in Java?

From Java

Why is using static helper methods in Java bad?

From Java

Java Comparable: helper methods for isLessThan, isGreaterThan, isEqualTo

From Dev

Where to store java stream helper methods

From Java

Private helper methods vs. public static utility methods in Java

From Java

Java abstract/interface design in Python

From Java

Java class interface design question

From Dev

Java - interface methods naming convention

From Java

Implementing subtypes for interface methods Java

From Dev

How to refactor helper methods with generics and foreign classes? (Java Android)

From Dev

Java / Android: Collect all Button methods in Helper class

From Java

Decorator design pattern java overriding methods question

From Java

java design: repetitive methods of sub-objects

From Dev

Design Pattern for restricting access to methods in Java

From Java

Java: design interface to force implementations to override toString

From Dev

JAVA - extends vs interface - Strategy design pattern

From Dev

Default access modifier for interface methods in Java 9?

From Java

Why is "final" not allowed in Java 8 interface methods?

From Dev

java Optional interface methods: Ambiguous method reference

From Dev

Java default interface methods concrete use cases

Related Related

  1. 1

    Recursion in helper methods (Java)

  2. 2

    How to use Java Helper methods?

  3. 3

    Strange interface design Java

  4. 4

    Abstract methods in a Java interface

  5. 5

    Conflicting interface methods in Java

  6. 6

    Optional Methods in Java Interface

  7. 7

    Optional Methods in Java Interface

  8. 8

    overriding methods in Interface java

  9. 9

    Is there any design pattern for using helper methods in models in Rails?

  10. 10

    Helper methods for creating tuples and tupled functions in Java?

  11. 11

    Why is using static helper methods in Java bad?

  12. 12

    Java Comparable: helper methods for isLessThan, isGreaterThan, isEqualTo

  13. 13

    Where to store java stream helper methods

  14. 14

    Private helper methods vs. public static utility methods in Java

  15. 15

    Java abstract/interface design in Python

  16. 16

    Java class interface design question

  17. 17

    Java - interface methods naming convention

  18. 18

    Implementing subtypes for interface methods Java

  19. 19

    How to refactor helper methods with generics and foreign classes? (Java Android)

  20. 20

    Java / Android: Collect all Button methods in Helper class

  21. 21

    Decorator design pattern java overriding methods question

  22. 22

    java design: repetitive methods of sub-objects

  23. 23

    Design Pattern for restricting access to methods in Java

  24. 24

    Java: design interface to force implementations to override toString

  25. 25

    JAVA - extends vs interface - Strategy design pattern

  26. 26

    Default access modifier for interface methods in Java 9?

  27. 27

    Why is "final" not allowed in Java 8 interface methods?

  28. 28

    java Optional interface methods: Ambiguous method reference

  29. 29

    Java default interface methods concrete use cases

HotTag

Archive