Efflective Java Reading notes the first one considers static factory methods instead of constructors

Source: Internet
Author: User

The method of creating an instance of a class is most commonly used to provide a public-only constructor, and another way is for a class to provide a public static factory method.

Static factory methods have different advantages and disadvantages for constructors.

Advantage:

  1. A static factory method can define a more interesting name, whereas a constructor can only be a class name.

The Eg:biginteger constructor may return a prime number, but using Biginteger.probableprime is more explicit.

  2. The static factory method creates a new object without having to call it, which can be pre-constructed for an immutable class, and the static factory method returns directly when called.

Eg:Boolean.valueOf method. Similar to flyweight mode, refer to enumeration type.

  3. The static factory method can return an object of any subtype of the original return type, for an interface-based framework.

The collection frame of the Eg:java. Enumset. The service provider framework, such as JDBC, includes three important component interfaces (service Interface), provider-registered APIs (Provider Registration API), which are implemented by the system with registration, allowing clients to use their The Services Access API (service access API), which is used by clients to obtain service instances, is a flexible static factory approach. There is also a fourth component service Provider Interface (Service Provider Interface), which is used by the provider to create an instance of its service implementation, and if there is no service provider interface, that implementation can only be registered by the class name and instantiated through reflection.

  4. Static methods can make the code more concise when creating instances of parameterized types. Use of type deduction.

Disadvantage:

1. Classes cannot be subclass if they do not include a public or protected constructor.

Corresponding Advantage 3.

2. Static factory methods do not actually differ from static methods, so you cannot know from the method name that it is a constructed type.

So there are some customary names for static factory methods:

    • ValueOf the instance returned by the method has the same value as the parameter, which is actually a type conversion method.
    • An alternative to valueof, Enumset.
    • GetInstance Singleton Singleton mode, returns a unique instance.
    • The instance returned by Newinstance is a new instance that is not the same as the one previously returned.
    • GetType is the same as getinstance, but is used when the factory method is in a different class.
    • NewType

Efflective Java Reading notes the first one considers static factory methods instead of constructors

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.