This is the 24th day of my participation in the August Text Challenge.More challenges in August

This is essentially a factory model, otherwise there is no factory in the name. The patterns that involve factories are all instantiation-creating patterns, with much the same idea of maximizing code reuse and reducing coupling to the client. In fact, when I first learned programming, I realized that the client is not necessarily a third-party remote caller like a mobile phone, but most of the time is other classes inside the business code. The goal of reducing coupling is to make the project code itself look cleaner.

So what exactly is an abstract factory? The factory pattern does only one layer of abstraction, and most of the methods used by the concrete subclass are in the parent class’s abstract method set, which is a relatively simple inheritance relationship. But abstract methods add another layer of abstraction, abstracting both actions and objects and handing them over to concrete business classes to inherit and implement, respectively. What’s the good of that? When multiple behaviors have different implementations in different scenarios, this reduces coupling to a large extent and allows you to focus only on the implementation of each scenario, making the logic more uniform and avoiding a lot of repetitive code. Here’s an example:

Choice of means of transportation for travel, by plane or by train:

public interface AbstractFactory {
    / / by plane
    PlaneTrip takePlane(String name);
    / / by train
    TrainTrip takeTrain(String name);
}
Copy the code

How to book and ride trains and planes are different in:

/ / by plane
public interface PlaneTrip {
    void order(String name) throws BizException;
}

/ / by train
public interface TrainTrip {
    void order(String name);
}
Copy the code

Here is the implementation:

public class PlaneTripImpl implements HtmlDocument {
    public void order(String name) throws BizException {... }}public class TrainTripImpl implements TrainTrip {
    public void order(String name) throws BizException {... }}Copy the code

Then, for example, we accessed third-party resources like Ctrip and Qunar:

public class CtripFactory implements AbstractFactory {
    public PlaneTrip takePlane(String name) {
        return new PlaneTrip(md);
    }
    public TrainTrip takeTrain(String name) {
        return newTrainTrip(md); }}Copy the code

Finally, let’s use the method of creating a factory instance by name in a simple factory:

public interface AbstractFactory {
    public static AbstractFactory createFactory(String name) {
        if (name.equalsIgnoreCase("Ctrip")) {
            return new CtripFactory();
        } else if (name.equalsIgnoreCase("Qunar")) {
            return new QunarFactory();
        } else {
            throw new IllegalArgumentException("Invalid factory name"); }}}Copy the code

To create a factory, call the create method:

AbstractFactory factory = AbstractFactory.createFactory("Ctrip");
Copy the code

Now that the creative design pattern has become less important with the popularity of Spring dependency injection, there is no longer any anxiety about creating objects, but the idea of reducing coupling is still worth using.