Factory Model
1. Description
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to its subclass.
2. Use Cases
The user needs an instance of a subclass of the class, but does not want to be coupled with the subclass of the class.
The most common method to obtain a subclass instance of a class is to directly create a new object. However, in some cases, you may not be able to use this method to obtain a subclass instance, the reason is that the system does not allow the coupling between user code and the subclass of this class, or the user does not know which subclasses of this class are available.
Eg: We designed a weapon system, including AK47 and OP96. If a soldier wants to get a gun, he cannot create it by himself (new by himself), but he can get it at the gun generation factory. In this scenario, we need to use the factory model. In this scenario, Gun (Gun) is an abstract product, while AK47 and OP96 are specific products. GunFactory is the constructor, while AK47 and OP96 are the constructor.
3. Role in the Mode
Abstract Product: an abstract class or interface that defines the methods required by a specific Product.
ConcreteProduct: a Product is a class. If the Product is an abstract class, the Product is a subclass of the Product. If the Product is an interface, the specific Product is the class that implements the Product interface.
Constructor: an interface or abstract class. The constructor is responsible for defining an abstract method called a factory method, which returns an instance of a specific product class.
ConcreteCreator: If the constructor is an abstract class, the constructor is a subclass of the constructor. If the constructor is an interface, the ConcreteCreator is a class that implements the constructor. The constructor overrides the factory method so that this method returns the instance of a specific product.
4. Class Diagram
5. Code
package org.sunday.simplefactory;public interface Gun {public abstract void shoot();}
package org.sunday.simplefactory;public class AK47 implements Gun {@Overridepublic void shoot() {System.out.println("AK47 shoot beng beng beng..............");}}
package org.sunday.simplefactory;public class OP96 implements Gun {@Overridepublic void shoot() {System.out.println("OP96 shoot da da da da..............");}}
package org.sunday.simplefactory;public interface GunFactory {public abstract Gun createGun();}
package org.sunday.simplefactory;public class AK47Factory implements GunFactory {@Overridepublic Gun createGun() {return new AK47();}}
package org.sunday.simplefactory;public class OP96Factory implements GunFactory {@Overridepublic Gun createGun() {return new OP96();}}
Package org. sunday. simplefactory; public class Test {public static void main (String [] args) {Gun gun = null; GunFactory gunFactory = null; // now I want an AC47gunFactory = new AK47Factory (); gun = gunFactory. createGun (); gun. shoot (); // now I want another OP96 sub-host gun gunFactory = new OP96Factory (); gun = gunFactory. createGun (); gun. shoot ();}}
Running result: