Constructor and static factory Method

Source: Internet
Author: User

Obtain an instance of the class. The two most common methods are the constructor creation and static factory methods.

Advantages of static factory methods over constructors:

(1) The static factory method has a name.Different constructors of the same class only have different parameters, parameter lengths, and order. The static factory method can customize the name according to the usage.

(2) the code can be simplified when you create a parameterized instance.

For example, Map <string, list <string> M = new hashmap <string, list <string> (); // The constructor must provide type parameters twice.

Map <string, list <string> M = hashmap. newinstance (); // The type derivation is used by the static factory method. The transformer finds the parameter type for you.

(3) You do not need to create a new object every time you call the static factory method.

The static factory method can return the same object for repeated calls. If two equal instances are not reported, A is available only when a = B. if equals (B) is true, A = B can be used to replace. equals (B) to improve performance.

(4) The static factory method can return any child type objects of the original return type.

Application 1: The API can return objects without changing the object class to public. In an interface-based framework (such as Java collections framework), an interface provides a natural return type for the static factory method, and the interface cannot have a static method, the static factory method of the interface is put in a class that cannot be instantiated. The client can reference the returned object through an interface, rather than its implementation class.

Application 2: the class of the object returned by the static factory method, which does not need to exist when writing a class that protects the static factory method. This flexible static factory method constitutesService provider framework (The service provider framework is worth implementing one service by multiple service providers. The system provides multiple implementations for the client of the service provider and decouples them from multiple implementations .). Such as JDBC. Service provider framework:

Service Interface: This is implemented by the provider. For example, connection in JDBC

Provider registration API: This is used by the system to register the implementation and allow the client to access them. For example, drivermanager. registerdriver

Service access API: used by the client to obtain the service instance. Drivermanager. Get. Connection

Service Provider Interface: These providers are responsible for creating their service implementation instances. Such as driver

Disadvantages of the relative constructor and static factory method:

(1) If the class does not include a public or protected constructor, it cannot be divided into categories. The same applies to non-public classes returned by public static factory methods.

(2) There is no difference with other static methods. Common static factory method name: valueof getinstance newinstance GetType newtype

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.