Creating and destroying objects - instead of constructors with static factory method

Reference: "as Effective the Java", https://www.jianshu.com/p/ceb5ec8f1174 .

Based on the review

1. What is the constructor?

Configured with the same name as the class, when a new object class constructor runs for instance initialized to the desired state.

Each class will have a default constructor with no arguments. You can write a constructor yourself manually.

For example, to write a class constructor in People:

public People(String Name,int Age)
{
    this.Name=Name;
    this.Age=Age;    
}

Then create a new instance of this class on

People a=new Prople("张三",18);

Such as above, may be added to the corresponding parameter after new. If we do not add parameters, it will call the default no-argument constructor.

 

2. factory method

The static factory method here is not a direct correspondence among factory method design pattern .

Static factory method NumberFormat commonly used in this kind or similar LoaclDate them. For example LocalDate.now and so on.

Why use static factory methods? For example NumberFormat.getCurrencyInstance () and NumberFormat.getPercentInstance () methods both plants, obtained a monetary type, a type of a percentage.

But their reception parameters are the same, and the constructor must have the same name and class, if have to use the constructor to do this, have to add additional parameters, while not explicitly name to distinguish, increasing the difficulty of using the developer's .

The following details will tell the other advantages of static factory methods.

 

Advantage of static factory method

To write their own static factory method

public  class EJ01_01 {
     Private  int the Num = 0 ; 

    // static factory method 
    public  static EJ01_01 Convert ( Double {the Num) 
        EJ01_01 ej01_01 = new new EJ01_01 (); 
        ej01_01.Num = ( int ) the Num;
         return ej01_01; 
    } 
    // test may return subclasses 
    public  static EJ01_01_Son Convert () {
         return  new new EJ01_01_Son (); 
    } 

    public  int the Show () {
         return  the this  .num;
    }

    public int Add_10() {
        return this.Num + 10;
    }
}

class Test {
    public static void main(String args[]) {
        EJ01_01 test = EJ01_01.convert(1.2);
        System.out.println(test.Add_10());//输出11
        System.out.println(test.Show());//输出1
    }
}
//子类
class EJ01_01_Son extends EJ01_01 {
}

 

1. static factory methods have a name.

This goes without saying, as above NumberFormat.getCurrencyInstance () and NumberFormat.getPercentInstance () the same. Developers can see at a glance the difference between them.

2. do not have to create a new object each call.

As the above code. In the test class:

EJ01_01 test = EJ01_01.convert(1.2);

It does not create any new objects, called directly.

3. It can return to the original return any subclass type

The constructor can only return to their original type, but in static factory method can return subclasses. Such as:

 // test may return subclasses 
    public  static EJ01_01_Son Convert () {
         return  new new EJ01_01_Son (); 
    }

I tested can also return other common data types, such as the return type of the method can directly convert written int, but have not seen any reference materials which do not know whether such an approach norm.

4. You can reduce external exposure of the property

The following function code, when invoked get_type, only a predetermined input parameters, then the corresponding output data.

This situation is very common, such as some methods only allow input MAX, MIN this parameter, other parameters directly input error.

But if we only constructor to achieve, it is not what the limit is entered by the user. The following code is equivalent to a static factory method as arguments to the constructor, the user can call the corresponding static factory method, and then assign the factory method.

class Get_Type {
    public static final int TYPE_Small = 1;
    public static final int TYPE_Moderate = 2;
    public static final int TYPE_Big = 3;

    private Get_Type(int type) {
        switch (type){
            case 1:
                System.out.println("这是小号");
                break;
            case 2:
                System.out.println("这是中号");
                break;
            case 3:
                System.out.println("这是大号");
                break;
        }
    }

    public static Get_Type Small() {
        return new Get_Type(TYPE_Small);
    }
    public static Get_Type Moderate() {
        return new Get_Type(TYPE_Moderate);
    }
    public static Get_Type Big() {
        return new Get_Type(TYPE_Big);
    }
}

//调用
class Test2 {
     public  static  void main (String args []) { 
        get_type get_small = Get_Type.Big (); // output that is large 
    } 
}

 Static factory method than these points, I may have some other advantages not found, few know the rest of the current capabilities of view, can not fully understand.

Guess you like

Origin www.cnblogs.com/lbhym/p/11787505.html
Recommended