Java Factory method Pattern Simple instance

Source: Internet
Author: User

Factory method Mode: Also called the Factory mode, belongs to the class creation mode, the factory parent class (interface) is responsible for defining the public interface of the product object, and the subclass factory is responsible for creating the specific Product object.

Purpose: To defer the instantiation of the product to the sub-factory, through the factory sub-class to determine exactly which product specific object should be instantiated.

The factory method pattern consists of four parts:

1. Abstract Product: The same base class as the Product object, or the same interface.

2. Specific products: various instance object classes

3. Abstract Factory: All sub-class factory class base class, or the same interface

4. Specific factory sub-category: Responsible for the actual creation of each different product object

The specific instance code:

1. Abstract Product class: Define car vehicles under the category:

 Package factory.simple; /** * Abstract Product Role Vehicle interface *   @author Lilin *   */  Public Interface Car {    /**     * Work function     * /     void  gotowork ();}

2. Define the actual product class, define a total of two, bike and bus respectively, the different modes of transport

 Package factory.simple; /** * Specific product characters, bikes *   @author Lilin *   */  Public class Implements Car {    @Override    publicvoid  gotowork () {        System.out.println ( "Ride a bike to work!" ");    }}

 Package factory.simple; /**  @author  lilin *  * */publicclassimplements Car {    @Override    publicvoid  gotowork () {        System.out.println ( "Take the bus to work!" ");    }}

3. The abstract factory interface is defined as follows:,

/**  *   */  Package factory.factory; Import Factory.simple.Car; /**  @author  lilin *  * */publicinterface  icarfactory {    /**     * Get transportation     *      @return* *           Car getcar ();}

4. The specific factory subclass, respectively, for each specific product class to create a different factory sub-class:

/** *  */ Packagefactory.factory;ImportFactory.simple.Bike;ImportFactory.simple.Car;/** * @authorLilin **/ Public classBikefactoryImplementsicarfactory {@Override PublicCar Getcar () {return NewBike (); }}

/** *  */ Packagefactory.factory;ImportFactory.simple.Bus;ImportFactory.simple.Car;/** * @authorLilin **/ Public classBusfactoryImplementsicarfactory {@Override PublicCar Getcar () {return NewBus (); }}

Finally, using a simple test class to verify that different factories can produce different product objects: The test class is as follows:

/** *  */ Packagefactory.factory;Importorg.testng.annotations.Test;ImportFactory.simple.Car;/** * @authorLilin **/ Public classtestfactory {@Test Public voidTest () {Icarfactory factory=NULL; //BikeFactory =Newbikefactory (); Car Bike=Factory.getcar ();        Bike.gotowork (); //BusFactory =Newbusfactory (); Car Bus=Factory.getcar ();    Bus.gotowork (); }}

The printing results are as follows:

Ride a bike to work!
Take the bus to work!

The factory model can be understood as: with a lot of factory methods, which product you need, we call the factory method of the current product to obtain the corresponding concrete instance.

Java Factory method Pattern Simple instance

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.