Design Pattern Factory Pattern

After learning object-oriented programming, learn to create objects using keywords"new" To create objects, but creating objects in this way is a bit redundant and too coupled when a complex and multiple objects need to be created, and it is not easy to be understood. Factory pattern is used to create objects

First, the simple factory mode.

The simple factory pattern is a special case based on the factory method pattern. The simple factory pattern is easy to understand: "Build concrete goods based on abstract goods, and then use factory classes to create specific goods for customers according to different situations. The specific code is as follows

  • abstract commodity
//Abstract commodity interface, take BMW as an example
public interface Bmw {
    public void run();
}
  • The following is the specific implementation class
public class BmwX3 implements Bmw{
    @Override
    public void run() {
        System.out.println("BMW x3 making");
    }
}

public class BmwX5 implements Bmw{
    @Override
    public void run() {
        System.out.println("BMW x5 making");
    }
}
  • specific factory
public class SimpleFactory {
    public static Bmw CreateCar(String sign){
        switch (sign){
            case "x3" :
                return new BmwX3();
            case "x5" :
                return new BmwX5();
            default:
                break;
        }
        return null;
    }
}
  • Client calling code
public class Client {
    public static void main(String[] args) {
        Bmw x3 = SimpleFactory.CreateCar("x3");
        x3.run();
        Bmw x5 = SimpleFactory.CreateCar("x5");
        x5.run();
    }
}

Disadvantages of a simple factory: First, if there is a new product in the product interface, an implementation class needs to be added, but after the implementation class is added, a new case needs to be added to the factory class for judgment to choose to create a new one and return a specific one. commodity object. Every time a new product is added, it is necessary to make a big effort to the factory class. This is not very good and violates the principle of opening and closing. So further optimize the introduction of the factory method pattern

Second, the factory method pattern

The simple factory pattern is where a factory creates multiple item instances but becomes out of place when there are multiple items of different types. For example, under the Volkswagen brand, there are specific brands such as Mercedes-Benz, Audi, Lamborghini, etc. If it is too cumbersome to use a factory for production at this time, it is like an acquisition in real life, and a specific factory is used to produce specific products. The specific code is as follows:

  • Abstract Product Definition
public interface CarProduct {
    public void introduce();
}
  • specific product
public class Bike implements CarProduct{
    @Override
    public void introduce() {
        System.out.println("i am a bicycle");
    }
}
public class Bmw implements CarProduct{
    @Override
    public void introduce() {
        System.out.println("i am a bmw");
    }
}
public class Train implements CarProduct{
    @Override
    public void introduce() {
        System.out.println("i am the train");
    }
}
  • Factory interface definition
public interface ProductFactory {
    public CarProduct generate();
}

Implementation of a specific factory

//bicycle factory
public class BikeFactory implements ProductFactory{
    @Override
    public CarProduct generate() {
        return new Bike();
    }
}
//BMW car factory
public class BmwFactory implements ProductFactory{
    @Override
    public CarProduct generate() {
        return new Bmw();
    }
}

//Train Factory Realization
public class TrainFactory implements ProductFactory{
    @Override
    public CarProduct generate() {
        return new Train();
    }
}

  • Client call
public class Client {
    public static void main(String[] args) {
        //Bicycle factory produces bicycles
        ProductFactory bikeFactory = new BikeFactory();
        bikeFactory.generate().introduce();

        //BMW factory production BMW cars
        ProductFactory bmwFactory = new BmwFactory();
        bmwFactory.generate().introduce();

        //train factory production
        ProductFactory trainFactory = new TrainFactory();
        trainFactory.generate().introduce();
    }
}

Summary: The personal feeling of the factory method pattern is to further strip the factory from the abstract definition interface rules. When a new product is produced, it is a process to directly create a new factory to implement the interface and return the newly created product. A single factory is responsible for creating a single product. Functions are clearer and clearer

3. Abstract Factory Pattern

When the factory is upgraded again to need to assemble different tires and different engines on a car, there are different dimensions of commodity abstraction. Therefore, factories are needed to assemble these products of different dimensions. The codes are as follows:

  1. Abstract engine and concrete engine products
//abstract engine product
public abstract class Engine {
    public abstract void equipment();
}
// Concrete snow engine
public class HongDouEngine extends Engine{
    @Override
    public void equipment() {
        System.out.println("Equipped with snow engine");
    }
}

public class KunLunEngine extends Engine{
    @Override
    public void equipment() {
        System.out.println("Assembling the Kunlun engine");
    }
}
  1. Abstract tires as well as concrete tire products
//abstract tire product
public abstract class Tires {
    public abstract void product();
}
//Specific red bean tire products
public class HongDouTires extends Tires{
    @Override
    public void product() {
        System.out.println("Use red bean tires");
    }
}
// specific mickey tires
public class KunLunTires extends Tires{
    @Override
    public void product() {
        System.out.println("Use Mickey tires");
    }
}
  1. Abstract factories and concrete implementations of factories
//An abstract car factory requires a concrete car factory to assemble concrete tires and engines
public interface  CarFactory {
    public Engine CreateEngine();

    public Tires CreateTires();
}

//Used red bean engine and red bean tires for bmw 320
public class Bmw320Factory implements CarFactory{
    @Override
    public Engine CreateEngine() {
        return new HongDouEngine();
    }

    @Override
    public Tires CreateTires() {
        return new HongDouTires();
    }
}

//Fitting Kunlun engine and Kunlun tires to BMW X5
public class BmwX5Factory implements CarFactory{
    @Override
    public Engine CreateEngine() {
        return new KunLunEngine();
    }

    @Override
    public Tires CreateTires() {
        return new KunLunTires();
    }
}
  1. Client call
//Client call
public class Client {
    public static void main(String[] args) {
        System.out.println("===========================>BMW 320 assembly");
        CarFactory bmw320 = new Bmw320Factory();
        bmw320.CreateEngine().equipment();
        bmw320.CreateTires().product();

        System.out.println("===========================>BMW X5 assemble");
        CarFactory bmwX5 = new BmwX5Factory();
        bmwX5.CreateEngine().equipment();
        bmwX5.CreateTires().product();
    }
}

The factory method solves the pattern of creating objects in one dimension. For example, a product is only a single product factory and is only responsible for creating a single type of type. However, the abstract factory is a multi-dimensional assembly of different products and then completes the creation. In the end, if there are any flaws, don't hesitate to enlighten me and make progress together.

Tags: Java Design Pattern

Posted by mr_badger on Fri, 13 May 2022 19:56:49 +0300