Final class with a private designer, what is the design principle

I recently went through one of Netflix's open source

There I found the use of both the final class and the private constructor. I am fully aware that

  • final should avoid inheritance
  • private - prohibit instantiation

But it’s just interesting to know why they are both used together. Although the methods are static, so we can use them without instantiating, but still want to know the design principle.

+4
source share
2 answers

With this code you will have these functions

  • Do not allow any subclass ( extends) of your class
  • Do not allow class instance creation
  • ( , , ).

singleton , IHMO helper/util API Netflix, , :

StaticFinalClassExample.methodYouWantToCall();

, , :

/**
 * This class consists exclusively of static methods that help verify the compliance of OP1A-conformant....
 */

//to prevent instantiation
private IMFConstraints()
{}

:

, Item 4 Joshua Bloch Java (2- ):

4:

, . , , , .

  • java.lang.Math java.util.Arrays.
  • , factory ( 1), , , java.util.Collections.
  • , final .

: . . . API.

. , - . , , , ( 17).

, , , . , , , .

+3

static "", , , , . , , .

2: , (, ). static factory, . :

public static IMFConstraints getInstance()

, . static, . :

public static void checkIMFCompliance(List<PartitionPack> partitionPacks)

:

// your cool client code here...
IMFConstraints.checkIMFCompliance(myPartitionPacks);
// more of your awesome code...

- .

0

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


All Articles