Effective JAVA first day static factory method

Source: Internet
Author: User

Static Factory Method: (this refers to the normal static method), the class can be provided to its client through the static factory method, not through the constructor. There are several advantages to providing a static factory method instead of a public constructor.    The class that uses the API in the implementation of the class is called the API by the client (client) who uses the API to write programs that the programmer calls the API User term: base type: Primitive, Reference type: reference type Method signature: Consists of a name and all parameter types, not including the return type advantages:1, with a name (when a class has multiple constructors with the same signature, a static factory instead is easier to understand)2, do not have to each call when the new one (singleton mode, enumeration object comparison can be directly used = =Instead of equal (can improve performance). If it is often requested to create the same object, this can greatly improve performance)3. You can return any subclass object of the original return type. (suitable for interface-based framework interface-based- Framework,api can return an object without making the object's class public (which is not understood), such as the Java Collections framework) static factory returns the class to which the object belongs. The client class can be written without having to exist (just define it, a typical application: A service provider framework: such as JDBC, and finally a similar example.) Service Access API can provide richer service with adapter mode)4, the code is more concise when creating instances of parameterized types. Disadvantages:1, a class that does not have a public or protected constructor cannot be instantiated ( such as the collections framework (see here) , so the benefit is to encourage the use of compliance with composition rather than inheritance) 2, no difference from other static methods The following is the name of a static factory:1) ValueOf2) of---A concise alternative to valueof, as in Enumset .3) getinstance4) newinstance5) GetType---Type represents the object types returned by the factory method6) NewType Packageservice provider framework model;/*** Description: Service Interface*/ Public InterfaceService {}/*** Service Provider Interface * Role: responsible for creating the service instance (if there is no service provider interface, the implementation is registered according to the class name and instantiated through class reflection) * For Jdbc,connection is the service interface, Drivermanager.registerdriver is the provider registration API * Drivermanager.getconnection is the service Access API * Driver is the service provider interface*/ Public InterfaceProvider {//responsible for creating the service instanceService NewService ();}/*** Description: Service Manager*/ Public classServiceManager {//prevents instantiation prevent instantiation    PrivateServiceManager () {} Public Static FinalString default_provider_name = "<def>";  Public Staticconcurrenthashmap<string, provider> providers =NewConcurrenthashmap<string, provider>(); //Provider Registration API     Public Static voidResiterprovider (Provider p) {resiterprovider (default_provider_name,p); }     Public Static voidresiterprovider (String name,provider p) {providers.put (name, p); }        //The Service Access API ( which provides a richer service with adapter mode )     Public StaticService newinstance () {returnnewinstance (default_provider_name); }     Public StaticService newinstance (String privodername) {Provider P=Providers.get (privodername); if(p = =NULL)            Throw NewIllegalArgumentException ("No registered service provider! "); returnP.newservice (); }}

Effective JAVA first day static 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.