JAVA Factory mode: Simple Factory

Source: Internet
Author: User

Simple Factory mode (simplefactory pattern):

Also known as the Static factory methods (static Factory method) mode, which belongs to the class-creation pattern . In the simple Factory mode, you can return instances of different classes depending on the parameters. The Simple factory model specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class .

1. Automotive interface

 Package com.lf.shejimoshi.Factory; /** * @classDesc: Class Description: (Auto interface)   @author Baobaolan * @createTime January 10, 2018    @version  */Publicinterface  Car        {void  run ();    }

2. Audi Class

 package   com.lf.shejimoshi.Factory;  /**   * @classDesc: Class Description: (Audi car Class) *   @author   Baobaolan * @createTime January 10, 2018 *   @version   v1.0  */ public  class  aodicar implements   Car {@Override  public  void   run () { //  TODO auto-generated method stub  System.out.println (" generated an Audi!    "); }}

3. Mercedes-Benz Class

 package   com.lf.shejimoshi.Factory;  /**   * @classDesc: Class Description: (Mercedes Benz Class) *   @author   Baobaolan * @createTime January 10, 2018 *   @version   v1.0  */ public  class  benchicar implements   Car {@Override  public  void   run () { //  TODO auto-generated method stub  System.out.println (" generated a Mercedes!    "); }}

4. Automotive Plant

 Packagecom.lf.shejimoshi.Factory;/*** @classDesc: Class Description: (Auto Factory) *@authorBaobaolan * @createTime January 10, 2018 *@versionv1.0*/ Public classCarfactory {//jdk1.7 Switch Support string     Public Staticcar Getcar (String cartype) {car car=NULL; Switch(cartype) { CaseAudi: Car=NewAodicar ();  Break;  CaseBenz: Car=NewBenchicar ();  Break; default:             Break; }        returncar; }    }

5. Test class

 Package com.lf.shejimoshi.Factory; /** * @classDesc: Class Description: ()   @author Baobaolan * @createTime January 10, 2018    @version  */Publicclass  Main        {publicstatic  void  main (string[] args) {        = Carfactory.getcar ("Audi");        Car.run ();                    }}

6. Result printing

JAVA Factory mode: Simple Factory

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.