Java design mode-factory mode (Factory method mode)

Source: Internet
Author: User
Factory model OverviewThe factory mode belongs to the class creation mode, and the factory mode is responsible for instantiating a large number of classes with common interfaces. The factory mode dynamically determines which class to instantiate without having to know which class to instantiate each time in advance. The factory model has three basic forms:

1. Simple factory mode, also known as static factory method pattern ).

2. Factory method mode, also known as polymorphic factory mode or virtual constructor mode.

3. Abstract Factory mode, also known as toolkit or toolkit mode.

Factory method mode

The factory method mode further abstracts and promotes the simple factory mode. In the factory method mode, it is no longer determined by only one factory class that the product class should be instantiated, this decision is handed over to the subclass of the abstract factory.
Let's take a look at its composition:
 
1. Abstract Factory role: this is the core of the factory method model and has nothing to do with the application. It is an interface that must be implemented by a specific factory role or a parent class that must be inherited. In Java, it is implemented by abstract classes or interfaces.     2. Specific factory role: it contains Code related to the specific business logic. An application is called to create the objects of a specific product.

      3. Abstract Product role: it is the parent class or implemented interface inherited by a specific product. In Java, abstract classes or interfaces are generally used for implementation.

      4. Specific product role: the object created by the specific factory role is the instance of this role. It is implemented by specific classes in Java.

      The factory method mode uses multiple sub-classes inherited from the abstract factory role to replace the "God class" in the simple factory mode ". As mentioned above, it shares the pressure on the object, and makes the structure more flexible-when new products (I .e. the car of the nouveau riche) are generated, as long as the product role is generated according to the contract provided by the abstract Product role and the abstract factory role, it can be used by the customer without having to modify any existing code. We can see that the structure of the factory role is also in line with the principle of opening and closing!

Okay, let's talk about the code. First, abstract the scenario:
A film and television company wants to make a war-themed TV series, requiring XXX military factory to build models, including flying planes, tanks that can run, and ships that can launch water. Then the xxx military factory began to build the model as required.

Abstract Product role

public interface Moveable {    void move();}

Abstract Factory role

public abstract class VehicleFactory{    public abstract Moveable creat();}

Specific product roles

public class Plane implements Moveable {    @Override    public void move() {System.out.println("plane fly...");    }}public class Tank implements Moveable {    @Override    public void move() {System.out.println("tank run...");    }}public class Boat implements Moveable {    @Override    public void move() {System.out.println("boat swim...");    }}

Specific factory roles

public class PlaneFactory extends VehicleFactory {    @Override    public Moveable creat() {return new Plane();    }}public class TankFactory extends VehicleFactory {    @Override    public Moveable creat() { return new Tank();    }}public class BoatFactory extends VehicleFactory {     @Override    public Moveable creat() {return new Boat();    }}

The specific implementation is implemented based on Android. Java writes a test class to implement it in the main method.

VehicleFactory planeFactory = new PlaneFactory();Moveable planeMove = planeFactory.creat();planeMove.move();VehicleFactory tankFactory = new TankFactory();Moveable tankMove = tankFactory.creat();tankMove.move();VehicleFactory boatFactory = new BoatFactory();Moveable boatMove = boatFactory.creat();boatMove.move();

Look at the output:

Then we can see the war-themed TV series of dogs and blood ......

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.