Learning notes (design mode - factory mode)

Posted by klainis on Sat, 25 Dec 2021 23:46:17 +0100

Factory mode

1. General factory mode

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern belongs to creation pattern, which provides the best way to create objects.

In factory mode, when creating objects, we do not expose the creation logic to the client, and point to the newly created objects by using a common interface.

Advantages:

1. If a caller wants to create an object, just know its name.

2. High scalability. If you want to add a product, just extend a factory class.

3. Shield the specific implementation of the product, and the caller only cares about the product interface.

Disadvantages: each time you add a product, you need to add a specific class and object implementation factory, which multiplies the number of classes in the system, increases the complexity of the system to a certain extent, and also increases the dependence of the specific classes of the system.

Usage scenario

1. Log recorder: records may be recorded to local hard disk, system events, remote servers, etc. users can choose where to record logs.

2. Database access, when the user does not know what kind of database the system uses and the database may change.

3. Designing a framework for connecting to a server requires three protocols, "POP3", "IMAP" and "HTTP", which can be used as product classes to jointly implement an interface.

Common factory mode implementation steps

1. Create an interface and an entity class that implements the interface.

2. Create a factory to generate the object of entity class based on the given information.

Create an interface:

public interface Car {
    void build();
}

Create an entity class that implements the interface:

Audi.java

public class Audi implements Car{
    @Override
    public void build() {
        System.out.println("This is a Audi car!");
    }
}

Benz.java

public class Benz implements Car{
    @Override
    public void build() {
        System.out.println("This is a Benz car!");
    }
}

Create a factory to generate objects of entity classes based on the given information.

public class CarFactory {
    public Car getCar(String carName){
        if(carName == null){
            return null;
        }
        if (carName.equalsIgnoreCase("Audi")){
            return new Audi();
        }else if (carName.equalsIgnoreCase("Benz")){
            return new Benz();
        }
        return null;
    }
}

Use this factory to get the object of the entity class by passing information.

public class CarDemo {
    public static void main(String[] args) {
        CarFactory carFactory = new CarFactory();
        Car audi = carFactory.getCar("Audi");
        audi.build();//This is a Audi car!
    }
}

2. Abstract factory pattern

Create other factories through one super factory. In the abstract factory pattern, there is no need to specify the class that produces the product factory. Each generated factory provides objects according to the factory pattern.

Implementation steps

1. Create Car and Motorcycle interfaces and entity classes that implement these interfaces.

2. Create abstract factory class {AbstractFactory.

3. Define factory classes {CarFactory} and} MotorFactory, both of which extend} AbstractFactory.

4. Create a factory creator / generator class {FactoryProducer.

code

Create an interface for the car

public interface Car {
    void build();
}

Create an entity class that implements the car interface, Audi java

public class Audi implements Car{
    @Override
    public void build() {
        System.out.println("This is a Audi car!");
    }
}

Benz.java

public class Benz implements Car{
    @Override
    public void build() {
        System.out.println("This is a Benz car!");
    }
}

Create an interface for the motorcycle

public interface Motorcycle {
    void makeMotorcycle();
}

Create an entity class Honda that implements the motorcycle interface java

public class Honda implements Motorcycle {
    @Override
    public void makeMotorcycle() {
        System.out.println("Build a Honda motorcycle");
    }
}

Create an entity class that implements the motorcycle interface, spring java.

public class Spring implements Motorcycle {
    @Override
    public void makeMotorcycle() {
        System.out.println("Build a spring breeze motorcycle");
    }
}

Create abstract classes for Car and Color objects to get factories.

public abstract class AbstracFactory {
    public abstract Motorcycle getMotor(String color);
    public abstract Car getCar(String car);
}

Create the Motor factory class integration factory abstract class. Generate entity class objects based on the given information.

public class MotorFactory extends AbstracFactory{

    @Override
    public Motorcycle getMotor(String color) {
        if (color == null){
            return null;
        }
        if (color.equalsIgnoreCase("Spring")){
            return new Spring();
        }else if (color.equalsIgnoreCase("Honda")){
            return new Honda();
        }
        return null;
    }

    @Override
    public Car getCar(String car) {
        return null;
    }
}

Create car factory class and integrate factory abstract class. Generate entity class objects based on the given information.

public class CarFactory extends AbstracFactory{
    @Override
    public Motorcycle getMotor(String color) {
        return null;
    }
    @Override
    public Car getCar(String carName){
        if(carName == null){
            return null;
        }
        if (carName.equalsIgnoreCase("Audi")){
            return new Audi();
        }else if (carName.equalsIgnoreCase("Benz")){
            return new Benz();
        }
        return null;
    }
}

Create a factory creation class and get the factory through the given conditions.

public class FactoryProducer {
    public static AbstracFactory getFactory(String choice){
        if (choice.equalsIgnoreCase("Car")){
            return new CarFactory();
        }else if (choice.equalsIgnoreCase("Motor")){
            return new MotorFactory();
        }
        return null;
    }
}

Use FactoryProducer to obtain AbstractFactory and obtain the object of entity class by passing type information.

public class AbstractFactoryPatternDemo {
    public static void main(String[] args) {
        AbstracFactory motorFactory = FactoryProducer.getFactory("Motor");
        Motorcycle motor = motorFactory.getMotor("Spring");
        motor.makeMotorcycle();//Build a spring breeze motorcycle
        AbstracFactory carFactory = FactoryProducer.getFactory("CAR");
        Car audi = carFactory.getCar("Audi");//This is a Audi car!
        audi.build();
    }
}

To add new products, you only need to add the interface for creating products in AbstractFactory and create new products in specific factories.

Topics: Java Design Pattern