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