Make members private and provide getter and setter methods uniformly to ensure safe access.
Make the constructor private and replace it with an ordinary static method. There are three advantages over the constructor: a. The name can be more meaningful and clear at a glance; b. There is no need to construct a new instance every time it is called; c .Can return more flexible types, such as subclasses of this class, not necessarily this class. The fourth advantage, for type inference to simplify syntax, has been improved with the introduction of diamond syntax in Java 7. --"Effective Java" 2nd
Simple factory mode and singleton mode can provide flexible instances, reduce the cost of creating instances, ensure consistent status, etc...
f is the new factory() class. getinstance is an instantiation of the factory class. Call this method to get an instantiation of the class, and then you can call the internal method of the factory class
This getInstance() method:
Make members private and provide getter and setter methods uniformly to ensure safe access.
Make the constructor private and replace it with an ordinary static method. There are three advantages over the constructor: a. The name can be more meaningful and clear at a glance; b. There is no need to construct a new instance every time it is called; c .Can return more flexible types, such as subclasses of this class, not necessarily this class. The fourth advantage, for type inference to simplify syntax, has been improved with the introduction of diamond syntax in Java 7. --"Effective Java" 2nd
Simple factory mode and singleton mode can provide flexible instances, reduce the cost of creating instances, ensure consistent status, etc...
f is the new factory() class. getinstance is an instantiation of the factory class. Call this method to get an instantiation of the class, and then you can call the internal method of the factory class