Design Pattern - Factory Pattern

factory pattern

Simple Factory Pattern (Simple Factory)

Here, we will use the vehicle as an example, abstract the common features of the vehicle to run and stop, and get the following interface; (of course, there may be more abstractions, and they can also be abstracted into abstract classes)

public interface Vehicle {


    /*
     * 车辆跑起来
     * 
     */
    public void run();

    /*
     * 车辆停下来
     */
    public void stop();
}

Here are the implementation classes of two vehicles: car and bike;
implementation classes can have their own attributes and methods, such as power, number of wheels, etc.;

public class Car implements Vehicle{

    @Override
    public void run() {
        System.out.println("小汽车跑起来...");

    }

    @Override
    public void stop() {
        System.out.println("小汽车停下来...");

    }

}
public class Bike implements Vehicle{

    @Override
    public void run() {
        System.out.println("自行车跑起来...");

    }

    @Override
    public void stop() {
        System.out.println("自行车停下来...");

    }

}

Factory class

public class VehicleFactory {

    public static Vehicle factory(String className) throws BadRequestException {

        if(className.equalsIgnoreCase("car")) {
            return new Car();
        }else if(className.equalsIgnoreCase("bike")){
            return new Bike();
        }else {
            throw new BadRequestException("无效的请求");
        }



    }
}

Write a main class to simulate the client

public class Main {

    public static void main(String[] args) throws BadRequestException {
        Vehicle car = VehicleFactory.factory("car");
        car.run();//小汽车跑起来...


        Vehicle bike = VehicleFactory.factory("bike");
        bike.run();//自行车跑起来...
    }
}

Using this mode, in the client program, we don't need to care about the class corresponding to the object we want to implement, and how the object is obtained; the factory class can create the client's desired object according to the parameters passed in by the client. The client only needs to use the interface or abstract class of the class to receive the object for consumption.

There are three key roles in the design mode. Of course, these three roles may be merged together, but none of them are indispensable;

  • factory role

    The role of the factory is the core of this pattern, it is directly depended on by the client, and it contains the logic of creating and implementing objects of the class under what circumstances.

  • The abstract role of the product

    The abstract role of the product is generally the abstract class inherited or the interface implemented by the class to create the object, and it is also a tool used by the client to receive the object;

  • Product implementation class role

    The role of the implementation class of the product, which is the specific implementation class of the object that the client wants, and the object created by the factory is actually an instance of this class;

Defect:
When the implementation class of the product is added, the factory class needs to modify the logic and add the new implementation class to the logic; in
addition, due to the single-inheritance and multi-implementation feature of Java, when the product class implements multiple interfaces, the factory class The class has to judge when to create the object of this implementation class. When these judgments are multiplied with the judgment of which entity class to create, the whole logic becomes very complicated; in addition, because the implementation method of this mode is a static method, Unable to inherit, it is not conducive to the formation of the vertical structure of the factory class; this defect has been solved in the factory method pattern;

Application: There are classic applications in the date formatting class DateFormat, you can look at the source code for a deeper understanding;

Factory Method Pattern (Factory Method)

Compared with the simple factory pattern, the factory method pattern not only abstracts the product, but also abstracts the factory itself. The original implementation class that produces all products is extended to a factory abstract implementation class to manage a The product realization class solves the problem that the factory realization class requires constant changes and has poor vertical expansion capability. Take transportation as an example. Originally, a transportation factory produced various transportation vehicles, including cars and bicycles; now it is produced by automobile factories. Automobile and bicycle factories produce bicycles; the original vehicle factory can now be regarded as a group;

group:


public interface VehicleFactory {

    public Vehicle factory();
}

car factory:

public class CarFactory  implements VehicleFactory{

    @Override
    public  Vehicle factory()  {

        return new Car();

    }
}

Bicycle Factory:

public class BikeFactory implements VehicleFactory {

    @Override
    public  Vehicle factory()  {

        return new Bike();

    }
}

Vehicle abstraction:

public interface Vehicle {


    /*
     * 车辆跑起来
     * 
     */
    public void run();

    /*
     * 车辆停下来
     */
    public void stop();
}

car:

public class Car implements Vehicle{

    @Override
    public void run() {
        System.out.println("小汽车跑起来...");

    }

    @Override
    public void stop() {
        System.out.println("小汽车停下来...");

    }

}

bike:

public class Bike implements Vehicle{

    @Override
    public void run() {
        System.out.println("自行车跑起来...");

    }

    @Override
    public void stop() {
        System.out.println("自行车停下来...");

    }

}

Client:

public class Main {

    public static void main(String[] args) throws BadRequestException {
        VehicleFactory carFactory = new CarFactory();

        Vehicle car = carFactory.factory();

        car.run();//小汽车跑起来...


    VehicleFactory bikeFactory = new BikeFactory();

        Vehicle bike = bikeFactory.factory();

        bike.run();//自行车跑起来...


    }
}

Four roles:

  • abstract role of factory

    The core of this mode defines the production action of the factory, which is directly dependent on the client

  • Factory implementation class

    The actual executor of the production object will also be relied on by the client, and only produces objects of the implementation class corresponding to itself, and the factories do not affect each other;

  • product abstraction

  • Product implementation class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324541731&siteId=291194637