Difference between a constructor and a factory method

Source: Internet
Author: User

Original address: http://leihuang.org/2014/11/09/Constructors-VS-Factory-Methods/


First look at the differences between the two on creating objects


Instantiating the class using static Methodclass dog{  Private Dog () {  }  //factory method to instantiate the Class public  static Dog getinstance () {    return new Dog ();}  }


Here are some advantages and disadvantages of the static factory approach:

Advantages: 1. Static factory methods have a name and the constructor does not

Because the factory method can have multiple, by the name of the different methods, but the constructor name can only have one, only can be by the number of differences, so the use of static factory method is more clear.

2. Static factory methods support conditional instantiation

That means when you create an object. Sometimes it is necessary to add some conditions to infer whether it should be created, assuming that the condition is satisfied and return an instance, or null if not satisfied, for example, Singleton mode.

Constructors do not do this, and the static factory method can be very easy to do.

3. The method can return a subtype of the return type

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

public static Animal getinstance () {  }


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

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

Assuming that a class can only obtain an instance through a static factory method, the constructor for that class cannot be co-owned or protected. Then there is no subclass of the class, that is, the class cannot be inherited.

in singleton mode, you first want to privatize the constructor .

2. Static factory methods and other static methods can not be distinguished from the name

The following are the frequently used naming of static factory methods

? valueOf -returns An instance that have, loosely speaking, the same value as its parameters. Such static factories is effectively type-conversion methods.

? of-A concise alternative to valueOf, popularized by Enumset

? getinstance -returns An instance that's described by the parameters but cannot being said to has the same value. In the case of a singleton, getinstance takes no parameters and returns the sole instance.

? newinstance -like getinstance, except that newinstance guarantees so each instance returned was distinct from a ll others.

? getType-like getinstance, but used when the factory method was in a different class. Type indicates the type of object returned by the factory method.

? newType-like newinstance, but used when the factory method was in a different class. Type indicates the type of object returned by the factory method.

2014-11-09 14:39:17

Brave,happy,thanksgiving!


Difference between a constructor and a factory method

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.