One instance with methods in java

I am wondering how to program a solution, which I think is a matter of style. I need to have a single instance of a class that only has methods and attributes. To get this in java, I have two options:

  • create an abstract class with static methods inside, so it will not be possible to create any instance of the class, and this is normal,
  • use singleton pattern with public methods.

I tend to go for the second approach, although I met 1. What and why is better than that, or is there a third option.

+3
source share
6 answers

, ?

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

, # " " - , . .

, .

+4

, . , -, . , .

public class myClass {

   /** This class should never be instantiated. */
   private myClass() {
   }

   public static void myMethod() {

   }

   ...
   //etc
   ...
}
+2

№1, , . , , - , - - . , - - .

- , , , , . , Singleton, ​​ . .

+1

, singleton , . , apache commons, .

+1

. OO, , , - . java-apis , java.Math. , .

, " ", , , .

, , ( , ?).

0

, .

. . . : .

You can still implement the interface if you ever need to challenge the implementation during testing.

0
source

Source: https://habr.com/ru/post/1765354/


All Articles