Monday, July 15, 2019

Effective Java Study Note - Consider static factory methods instead of constructors

There are advantages of static factory methods, you can have name for the method, to make its usage more clear; or you can hide the implementation; you don't even create a new instance when the method invoked. There're many common names used for the static factory methods as mentioned below.

  • from
    • Date.from(instance)
  • of
    • EnumSet.of(A, B, C)
  • valueOf
    • Integer.valueOf(100)
  • instance
  • getInstance
  • create
  • newInstance
  • getType
    • FileStore fileStore = Files.getFileStore(path)
  • newType
    • BufferedReader bufferedReader = Files.newBufferedReader(path)
  • type
    • Collections.list(data)

No comments:

Post a Comment