Instead of considering a static factory method constructor

Instead of considering a static factory method constructor

What is a static factory method

In Java, a class instance get the easiest way is to use the new keyword, to achieve the creation of an object by the constructor.
like this:

    Fragment fragment = new MyFragment();
    // or
    Date date = new Date();

But in the actual development, we often also see another way to get class instance:

	Fragment fragment = MyFragment.newIntance();
    // or 
    Calendar calendar = Calendar.getInstance();
    // or 
    Integer number = Integer.valueOf("3");

↑ like this: By not new, but rather a way to provide external static method itself instance, that is what we call a static factory method (Static factory method).

Effective Java static factory summarize the advantages

1. different constructors with static factory method first advantage is that they have a name
2. Different static factory methods and constructors second advantage, do not always create a new object is called
3. static factory method and structure a third advantage is different, can return to the original return type of the subclass
4. fourth advantage, when creating instances of a generic tape, the code can become simple

Static factory method may have the same name but with different parameters of the method (which is different from the constitution can only be unified name)

class Child{
    int age = 10;
    int weight = 30;
    public static Child newChild(int age, int weight) {
        Child child = new Child();
        child.weight = weight;
        child.age = age;
        return child;
    }
    public static Child newChildWithWeight(int weight) {
        Child child = new Child();
        child.weight = weight;
        return child;
    }
    public static Child newChildWithAge(int age) {
        Child child = new Child();
        child.age = age;
        return child;
    }
}

↑ like this: the difference between a constructor can only be a question of the same name, newChild (), newChildWithWeight () , newChildWithAge (), shown more clearly see the name EENOW
( Note: in order to highlight the name of a careful choice between them the difference) .

Reduce external exposure of the property through the static factory method

Software development has a very important lesson: the more exposed to the external attributes, the easier the caller error. So for the provider classes, in general, efforts should be made to reduce external exposure of properties, thereby reducing the chance of error caller.

Consider the following a Player class:

// Player : Version 1
class Player {
    public static final int TYPE_RUNNER = 1;
    public static final int TYPE_SWIMMER = 2;
    public static final int TYPE_RACER = 3;
    protected int type;
    public Player(int type) {
        this.type = type;
    }
}

Foreign Player provides a constructor, allowing users to pass a type to indicate the type. Then this class is expected to call this way:

    Player player1 = new Player(Player.TYPE_RUNNER);
    Player player2 = new Player(Player.TYPE_SWEIMMER);

However, we know that the provider is unable to control the behavior of the caller, the actual invocation might look like this:

    Player player3 = new Player(0);
    Player player4 = new Player(-1);
    Player player5 = new Player(10086);

Provider desired value passed in the constructor is one of several pre-defined constants, but if not, can easily lead to program errors.
- To avoid this error, use an enumeration instead of constant values is a common method, of course, if you do not use enumeration, use what we call today the protagonist of static factory method is a very good way.

If the above requirements is achieved by a static factory method, so the code is roughly:

// Player : Version 2
class Player {
    public static final int TYPE_RUNNER = 1;
    public static final int TYPE_SWIMMER = 2;
    public static final int TYPE_RACER = 3;
    int type;

    private Player(int type) {
        this.type = type;
    }

    public static Player newRunner() {
        return new Player(TYPE_RUNNER);
    }
    public static Player newSwimmer() {
        return new Player(TYPE_SWIMMER);
    }
    public static Player newRacer() {
        return new Player(TYPE_RACER);
    }
}

Note where the constructor is declared to private, so you can prevent it from being called outside, so the caller when using the Player instance, probably should be created by newRunner, newSwimmer, newRacer these static factory method, the caller without there is no need to know the value of a specified type - so you can type in the scope of the assignment under control, preventing the previously mentioned outlier cases

Source: https://www.jianshu.com/p/ceb5ec8f1174

Published 30 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/wg22222222/article/details/84104731