Java – when to use getinstanceof instead of constructor

A few months ago, I attended a speech hosted by representatives of two independent software development companies It is mainly about good software design and practice

These two people mainly talked about Java. I remember they said that in some cases, it is a good practice to use getinstanceof () instead of constructor It has something to do with always calling getinstanceof () from different classes instead of constructors, and how it can be a better method on larger projects

As you can see, I can't remember now: / but I remember the arguments they used were really convincing I want to know if you have ever encountered such a design. Do you know when it will be useful? Or do you think it doesn't exist at all?

Solution

They may be discussing static factory method patterns (rather than reflective API methods for dynamically creating objects)

There are several advantages to using getinstanceof () method and new method on constructor Static factory methods can

>If it is necessary in some cases (based on environmental conditions, such as attributes and other object / singleton or method parameters), choose to create different subclasses of the main class. > Choose to return an existing object instead of creating it For this example, see Boolean. Net in the Java API valueOf(boolean). > Do the same thing as constructors – only return new instances of the class itself. > Provide many different types of methods to construct a new object and name these methods so that they are not so confusing (for example, try to use constructors, and there will soon be many different overloads) Sometimes, if you need to be able to create instances in two different ways, but only need the same type of parameters, the builder can't even do this Example:

// This class will not compile!
public class MyClass {
    public MyClass(String name,int max) {
        //init here
    }
    public MyClass(String name,int age) {
        // init here
    }
}

// This class will compile.
public class MyClass2 {
    private MyClass2() {
    }
    public static MyClass2 getInstanceOfMax(String name,int max) {
        MyClass2 m2 = new MyClass2();
        // init here
        return m2;
    }
    public static MyClass2 getInstanceOfAge(String name,int age) {
        MyClass2 m2 = new MyClass2();
        // init here
        return m2;
    }
}

>Make any combination of the above. > And, most importantly, it hides the details of instantiating instances from other classes, so it can be changed in the future (construct encapsulation)

The constructor can only create a new instance of an object of the exact type requested It can't be changed in the future

Some disadvantages of this model are:

>Factory methods are static and therefore cannot be inherited in subclasses; Subclasses can easily access the parent constructor. > Factory method names can vary widely, which may confuse some (New) developers

You also need personal experience Yes, I often use these two modes For most class constructors, but when there are more advanced requirements, I use static factories I also work on projects in other languages (proprietary, but similar to Java), and this form of construction is mandatory

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>