Difference between constructors and factory methods

Source: Internet
Author: User

Difference between constructors and factory methods

 

 

 

First, let's take a look at the differences between the two in object creation.

// instantiating a class using constructorDog dog = new Dog(); 
// instantiating the class using static methodClass Dog{  private Dog(){  }  // factory method to instantiate the class  public static Dog getInstance(){    return new Dog();  }}

The following describes the advantages and disadvantages of the static factory method:

Advantages: 1. The static factory method has a name but the constructor does not

Because there can be multiple factory methods, different methods can be distinguished by names, but only one constructor name can be distinguished by parameters, so the static factory method is clearer.

2. The static factory method supports conditional instantiation.

That is to say, when creating an object, you sometimes need to add some conditions to determine whether to create the object. If the conditions are met, an instance is returned. If the conditions are not met, NULL is returned, such as the singleton mode. This is not possible for constructors, but static factory methods can be easily implemented.

3. The method can return the child type of the return type.

Suppose there is a class Animal and it has subclass Dog. We have static method with signature

  public static Animal getInstance(){  }

Then method can return Both objects of type Animal and Dog which provides great flexibility.

Disadvantage: 1.If constructor of the class is not public or protected then the class cannot be sub classed

If a class can only obtain instances through the static factory method, the constructor of the class cannot be common or protected, and the class will not have child classes, that is, the class cannot be inherited.In Singleton mode, the constructor must be privatized first..

2. Static factory methods and other static methods cannot be distinguished by names

The following are common static factory naming methods:

ValueOf-Returns an instance that has, loosely speaking, the same value as its parameters. Such static factories are supported tively type-conversion methods.

Of-A concise alternative to valueOf, popularized by EnumSet

GetInstance-Returns an instance that is described by the parameters but cannot be said to have the same value. In the case of a singleton, getInstance takes no parameters and returns the sole instance.

NewInstance-Like getInstance, response t that newInstance guarantees that each instance returned is distinct from all others.

GetType-Like getInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

NewType-Like newInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

14:39:17

Brave, Happy, Thanksgiving!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.