Factory mode of design mode

Factory mode of design mode

1. simple factory mode

Product interface:

public interface IBike {
    void getName();
}

Product realization class:

@Slf4j
public class ForeverBike implements IBike {
    @Override
    public void getName() {
        log.info("Permanent bicycle");
    }
}
@Slf4j
public class JieAnDaBike implements IBike {
    @Override
    public String getName() {
        log.info("Jieanda bicycle");
    }
}

Client:

public static void main(String[] args) {
    ForeverBike foreverBike = new ForeverBike();
    JieAnDaBike jieAnDaBike = new JieAnDaBike();
    foreverBike.getName();
    jieAnDaBike.getName();
}

In the above code, we do not use any design patterns. In order to obtain different Bike instance objects, we need to rely on different Bike implementation classes. If we have more and more Bike implementation classes, we will have more and more dependencies. We should hide the details of creating Bike instance objects and reduce this dependency. Although in the current code, the process of creating Bike instance objects is not complicated, but it is highly dependent and coupled.

Next, we will hide the details of creating instance objects to reduce the client's dependence on these implementation classes

public class SimpleBikeFactory {
    public static Bike getBike(String name) {
        if ("permanent".equals(name)) {
            return new ForeverBike();
        } else if ("Gianda".equals(name)) {
            return new JieAnDaBike();
        } else {
            throw new IllegalArgumentException();
        }
    }
}
public static void main(String[] args) {
    Bike foreverBike = SimpleBikeFactory.getBike("permanent");
    Bike jieAnDaBike = SimpleBikeFactory.getBike("Gianda");
    System.out.println(foreverBike.getName());
    System.out.println(jieAnDaBike.getName());
}

Advantages: through the above transformation, we encapsulate the details of creating objects into SimpleBikeFactory, reducing the coupling in the client.

Disadvantages: if we want to obtain bikes of other brands, we still need to modify the code in SimpleBikeFactory, which violates the opening and closing principle (open to extension and closed to modification)

2. factory method mode

As we know from the previous section, the simple factory mode violates the opening and closing principle. Using the factory method mode can solve this problem.

Factory interface:

public interface IBikeFactory {
    IBike get();
}

Factory implementation class:

public class ForeverBikeFactory implements IBikeFactory {
    @Override
    public IBike get() {
        return new ForeverBike();
    }
}
public class JieAnDaBikeFactory implements IBikeFactory {
    @Override
    public IBike get() {
        return new JieAnDaBike();
    }
}

Client:

public static void main(String[] args) {
    ForeverBikeFactory foreverBikeFactory = new ForeverBikeFactory();
    IBike foreverBike = foreverBikeFactory.get();
    JieAnDaBikeFactory jieAnDaBikeFactory = new JieAnDaBikeFactory();
    IBike jieAnDaBike = jieAnDaBikeFactory.get();
    foreverBike.getName();
    jieAnDaBike.getName();
}

Advantages: we can get the desired Bike object only through the factory object. When adding Bike products, there is no need to modify the original factory and follow the opening and closing principle

Disadvantages: every time a Bike product is added, a Bike implementation class and a corresponding factory class must be added, which increases the complexity of the system

If we now need to produce not only bicycles, but also electric vehicles, such as permanent electric vehicles and jieanda electric vehicles. If we follow the factory method mode, we need to define (electric vehicle interface, permanent electric vehicle class, jieanda electric vehicle class), (electric vehicle factory interface, permanent electric vehicle factory class, jieanda electric vehicle factory class), and the system will become more and more complex. Fortunately, we found that the permanent bicycle and the permanent electric vehicle are a product family, both of which belong to the permanent factory. The jieanda bicycle and the jieanda electric vehicle are a product family, both of which belong to the jieanda factory, and the bicycle and the electric vehicle are also a product family. Therefore, we can use the abstract factory model.

3. abstract factory mode

**Factory interface: * * both bicycles and electric vehicles can be produced

public interface ICarFactory {
    IBike getBike();

    IElectricCar getElectricCar();
}

Factory implementation class:

public class ForeverCarFactory implements ICarFactory {
    @Override
    public IBike getBike() {
        return new ForeverBike();
    }

    @Override
    public IElectricCar getElectricCar() {
        return new ForeverElectricCar();
    }
}
public class JieAnDaCarFactory implements ICarFactory {
    @Override
    public IBike getBike() {
        return new JieAnDaBike();
    }

    @Override
    public IElectricCar getElectricCar() {
        return new JieAnDaElectricCar();
    }
}

Electric vehicle interface:

public interface IElectricCar {
    void getName();
}

Electric vehicle realization:

@Slf4j
public class ForeverElectricCar implements IElectricCar {
    @Override
    public void getName() {
        log.info("Permanent electric vehicle");
    }
}
@Slf4j
public class JieAnDaElectricCar implements IElectricCar {
    @Override
    public void getName() {
        log.info("Jieanda electric vehicle");
    }
}

Client:

public static void main(String[] args) {
    ICarFactory foreverCarFactory = new ForeverCarFactory();
    foreverCarFactory.getBike().getName();
    foreverCarFactory.getElectricCar().getName();
}

Advantages: using the abstract factory mode, when we add new products, we can avoid using the factory method mode to add new factory classes, which will not increase the system complexity

Disadvantages: when we add new products, all factory categories need to be adjusted

The above is all about the factory mode. I will continue to update the design mode. Welcome to communicate with us 😁

Tags: Design Pattern

Posted by Cogen2 on Wed, 01 Jun 2022 21:45:53 +0530