First, the preface
Start today with a one-month plan to read the classics in the Java domain by writing reading notes--effective the Java process--for future reference, while sharing it with hope that it will help others, with little nonsense. Now start the first article: Create and Destroy objects.
Second, consider using static factory method instead of the constructor
①. What are the general ways to create objects?
Method 1: Use the class public constructor.
Method 2: Returns an instance using the static method of the class.
②. What are the advantages of using a static method to create an object?
Advantage 1: The name of the static factory method is named by ourselves, and the constructor must have the same name as the class.
Primenumber primeNumber1 = new Primenumber (int Random); Returns a prime number
primenumber PrimeNumber2 = primenumber.newinstance ();//Use static Factory method to express more clearly
Advantage 2: The construction method needs to create an object each time it is invoked, and the static factory method does not create a new object each time it is invoked, which can greatly improve performance for a program that frequently creates objects, as in the case of a single pattern.
Advantage 3: Static factory methods can return objects of any subtype of the original return type, which gives us greater flexibility in choosing the class that returns the object.
class Father {private Father () {} public static Father newinstance (String type) {
if (Type.equals ("Childa")) {//returns the subclass object based on type judgment return new Childa ();
else {return new childb ();
}} public void GetName () {System.out.println (' My name is Father '); private static class Childa extends Father {public void GetName () {SYSTEM.OUT.PR
Intln ("My name was Child A"); } private static Class Childb extends Father {public void GetName () {System
. OUT.PRINTLN ("My name was Child B"); }} public class Test {public static void main (string[] args) {Father C1 = father.new
Instance ("Childa");
C1.getname ();
Father C2 = father.newinstance ("Childb");
C2.getname (); }
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
Advantage 4: Static factory methods can make code more concise when creating instances of parameterized types.
Private map<string, list<string>> Map = new hashmap<string, list<string>> ();
public static <k, v> hashmap<k, v> newinstance () {return
new hashmap<k, v> ();
}
③. What are the disadvantages of creating an object using a static factory method?
Disadvantage 1: If a class can only get an instance through a static factory method, then the constructor of the class cannot be shared or protected, then the class will not have subclasses, that is, the class cannot be inherited. In a single case mode, the constructor must be privatized first.
Disadvantage 2: Static factory methods and other static methods are indistinguishable from names, so we can contract static factory method names using valueof or getinstance.
④. When to use a static factory method, and when to use a builder?
Static factory methods and common constructors are useful, we need to understand their respective strengths, static factories are usually more appropriate, so avoid the first reaction is to provide public constructors, without first considering static factory.
Author: csdn Blog zdp072