Beginner factory model

Basic concepts

Function: separate the creator from the caller

Core essence: instantiated objects do not use new, but use factory methods instead
The implementation class will be selected to create an object for unified management and control, so as to decouple the caller from the implementation class

Three factory modes

1. Simple factory

Simple factory is also called static factory mode. It generally uses static methods to return different instances by accepting different parameters

Disadvantages: there is nothing we can do to add new products, which can only be extended through modification, which does not comply with the opening and closing principle

// Simple factory mode
public class CarFactory {
    // Use static methods to return different instances by accepting different parameters
    public static Car createCar(String name) {
        if ("BYD".equals(name)) {
           return new Byd();
        } else if ("audi".equals(name)) {
            return new Audi();
        } else {
            return null;
        }
    }
}

// This is also the embodiment of the simple factory model
public class CarFactory02 {
    public static Car createByd() {
        return new Byd();
    }
     public static Car createAudi() {
        return new Audi();
    }
}


// In the case of a simple factory, obtain an object instance
public class Client {
    public static void main(String[] args) {
        // Get the object instance by calling the factory class
        Car c1 = CarFactory.createCar("audi");
        Car c2 = CarFactory.createCar("BYD");

        c1.showName();
       c2.showName();
    }
}

2. Factory method model

The biggest difference between factory method mode and simple factory mode:
The simple factory pattern has only one factory class, while the factory method pattern has a set of factory classes that implement the same interface

public interface CarFactory {
    Car createCar();
}

//Returns an instance of a class
public class AudiFactory implements CarFactory{
    @Override
    public Car createCar() {
        return new Audi();
    }
}

//Return BYD class instance
public class BydFactory implements CarFactory{
    @Override
    public Car createCar() {
        return new Byd();
    }
}

3. Abstract factory pattern

Abstract Factory Pattern:
Is to create other factories around a super factory. The super factory is also called the factory of other factories, focusing on understanding what is the product family

Product grade structure: that is, the inheritance structure of the product. For example, if an abstract class is tire, and its subclasses include high-end tire and low-end tire, the tire and the specific level tire constitute a product grade structure

Product family: in the abstract factory mode, product family refers to a group of products produced by the same factory and located in different product hierarchy. For example, the tires, engines and seats produced by high-end car factories in the following example can be regarded as a product family

For example, we have a super car factory, which produces both high-end and low-end cars
In order to facilitate management, we need to build two sub factories, which are responsible for producing high-end cars and low-end cars

public interface CarFactory {
    Engine createEngine();

    Seat createSeat();

    Tire createTire();
}

public class LowerFactory implements CarFactory{
    @Override
    public Engine createEngine() {
        return new LowerEngine();
    }

    @Override
    public Seat createSeat() {
        return new LowerSeat();
    }

    @Override
    public Tire createTire() {
        return new LowerTire();
    }
}

public class AdvancedFactory implements CarFactory{
    @Override
    public Engine createEngine() {
        return new AdvancedEngine();
    }

    @Override
    public Seat createSeat() {
        return new AdvancedSeat();
    }

    @Override
    public Tire createTire() {
        return new AdvancedTire();
    }
}

The sub factory also has a method of specializing in the production of seats, engines and tires

public interface Engine {
    void engineInfo();
}

class AdvancedEngine implements  Engine {
    @Override
    public void engineInfo() {
        System.out.println("High end engine");
    }
}

class LowerEngine implements Engine {
    @Override
    public void engineInfo() {
        System.out.println("Low end engine");
    }
}
===============================================
public interface Seat {
    void seatInfo();
}

class AdvancedSeat implements Seat {
    @Override
    public void seatInfo() {
        System.out.println("High end seats");
    }
}

class LowerSeat implements  Seat {
    @Override
    public void seatInfo() {
        System.out.println("Low end seat");
    }
}
===============================================
public interface Tire {
    void tireInfo();
}

class AdvancedTire implements Tire {
    @Override
    public void tireInfo() {
        System.out.println("High end tire");
    }
}

class LowerTire implements  Tire {
    @Override
    public void tireInfo() {
        System.out.println("Low end tire");
    }
}

The super factory only needs to call the corresponding sub factory to produce high-end cars and low-end cars

public class Client {
    public static void main(String[] args) {
        CarFactory carFactory = new AdvancedFactory();
        Engine engine = carFactory.createEngine();
        engine.engineInfo();
    }
}

If there is any mistake, please correct it

Tags: Design Pattern

Posted on Wed, 10 Nov 2021 19:15:01 -0500 by elwadhos