Simple Factory mode (static Factory method mode) "Design mode"

Source: Internet
Author: User

Definition: provides a feature to create an instance, and the client consumer does not need to be concerned about the specific implementation of the instance. The created instance can be an interface, an abstract class, or a concrete class. It is also known as the Static factory methods (static Factory method) mode

  The function of the simple Factory mode, the advantages: for the client to mask the specific implementation of the required instance, the client only need to focus on an interface can be, no need to focus on implementation. The concrete implementation is encapsulated , the client and the concrete implementation decoupling, the client only needs to pay attention to the interface, the interface implementation change has no impact on the client. Summing up, is the encapsulation function, the concrete implementation of the example of the closure, decoupling function, the change of the interface can not affect the client.
Example: Example 1: Without parametersNo parameters are passed to the factory method, and the factory method returns only one type of implementation, without using the parameter selection implementation.   public class DbConnection {private static final int max_conns = 100;    private static int totalconnections = 0;    private static set<dbconnection> availableconnections = new hashset<dbconnection> ();   Private DbConnection () {//... totalconnections++; }Public static DbConnection getdbconnection ()   { if (Totalconnections < Max_conns)     { return new DbConnection ();     }  Else if (availableconnections.size () > 0)     { DbConnection DBC = Availableconnections.iterator (). Next ();Availableconnections.remove (DBC);return DBC;     } else {throw new Nodbconnections ();     }    } public static void Returndbconnection (DbConnection dbc) {availableconnections.add (DBC); //...    } } Example 2: With parameters, select the specific implementationThe simple factory method has incoming parameters and uses the incoming parameters to determine the implementation. This approach exposes certain internal implementation details, because the client, in order to understand the meaning of the parameter, can use the simple factory method correctly. The meaning of the parameter is related to the concrete implementation, and the parameter is used to select a specific implementation. Public  InterfaceApi {    Public  voidTestone (); } Public  classImplapi Implementsapi{    Public  voidTestone ()  {System. out. println ("This is Implapi." );  } }  Public  classImpltwoapi ImplementsApi {    Public  voidTestone ()  {System. out. println ("This is Impltwoapi." );  } }  Public  classtypeconstants {    Public  Static  Final int  Typeone=1;   Public  Static  Final int  Typetwo=2;  }  Public  classApifactory {    /**   * @param Type the value must in Typeconstants   *  @throwsException   *   */  Public  StaticApi Createapi ( int type) throwsException {           Switch(type)      {         Casetypeconstants. typeone :           return  NewImplapi ();                           Casetypeconstants. typetwo :           return  NewImpltwoapi ();                   default :            Throw  New Exception ("unsupoorted param exception");      } } }  The client needs to understand the meaning of the incoming parameter, which exposes the internal implementation of the factory method to some extent. Public  classClient {     Public  Static  voidMain (string[] args)  {     Try {Api one = apifactory. Createapi (Typeconstants. typeone );One.testone ();    }  Catch(Exception e) {      //  TODOauto-generated Catch blocke.printstacktrace ();    }        } }Example 3: An incoming parameter is required, and the implementation is implemented by reading the configuration file or dependency injection, that is, the responsibility for implementing the method is delegated to other parts. This avoids the constant addition of implementation code to the factory method. Citation profile: Http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java

Simple Factory mode (static Factory method mode) "Design mode"

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.