Java Foundation (Application of interface: creative design pattern factory design pattern Proxy)

Keywords: Java Design Pattern

Application of interface: factory mode

Factory mode: it realizes the separation of creator and caller, that is, the specific process of creating objects is shielded and isolated, so as to improve flexibility.
In fact, both design patterns and object-oriented design principles are to make the development project easier to expand and maintain. The solution is a "division of labor".
The same is true of social development, with more and more detailed division of labor.
People in primitive society: people have to be able to grow everything, hunt, weave clothes and cure their own diseases
Now people: can only be the same, others can't, can only live in Java, can't cook, can't drive, can't

Classification of plant modes:

Simple factory mode: used to produce any product in the same hierarchical structure. (for adding new products, the existing code needs to be modified)
Factory method mode: used to produce fixed products in the same hierarchical structure. (any product can be added)
Abstract factory pattern: used to produce all products of different product families. (there is nothing you can do to add new products; support adding product families).

No factory mode

Client01.java

package com.klvchen.java1;

interface Car{
    void run();
}

class Audi implements Car{

    @Override
    public void run() {
        System.out.println("Audi is running");
    }
    
}

class BYD implements Car{

    @Override
    public void run() {
        System.out.println("BYD is running");
    }
    
}

public class Client01 {

    public static void main(String[] args) {
        Car a = new Audi();
        Car b = new BYD();
        a.run();
        b.run();
    }
}

Simple factory mode

Simple factory mode is also called static factory mode, that is, factory classes generally use static methods to return different instance objects through different received parameters.
Disadvantages: for adding new products, it cannot be extended without modifying the code. Violation of the opening and closing principle (open to expansion; closed to modification).

Client02.java

package com.klvchen.java1;

interface Car2{
    void run();
}

class Audi2 implements Car2{

    @Override
    public void run() {
        System.out.println("Audi is running");
    }
    
}

class BYD2 implements Car2{

    @Override
    public void run() {
        System.out.println("BYD is running");        
    }
    
}

class CarFactory{
    //Mode 1
    public static Car2 getCar(String type) {
        if("audi".equals(type)) {
            return new Audi2();
        }else if ("BYD".equals(type)) {
            return new BYD2();
        } else {
            return null;
        }
    }
}

public class Client02 {
    public static void main(String[] args) {
        Car2 a = CarFactory.getCar("audi");
        a.run();
        Car2 b = CarFactory.getCar("BYD");
        b.run();
        
    }
}

Factory method model

In order to avoid the disadvantages of simple factory mode, OCP is not fully satisfied (open to extension and closed to modification). The biggest difference between the factory method pattern and the simple factory pattern is that the simple factory pattern has only one factory class (for a project or an independent module), while the factory method pattern has a group of factory classes that implement the same interface. In this way, the pressure on factory methods in the simple factory mode can be shared by different factory subclasses in the factory method mode.

Client03.java

package com.klvchen.java1;

interface Car3{
    void run();
}

class Audi3 implements Car3{

    @Override
    public void run() {
        System.out.println("Audi is running");        
    }
}

class BYD3 implements Car3{

    @Override
    public void run() {
        System.out.println("BYD is running");        
    }
    
}

interface Factory{
    Car3 getCar();
}

class AudiFactory3 implements Factory{

    @Override
    public Audi3 getCar() {
        return new Audi3();
    }
    
}

class BydFactory3 implements Factory{

    @Override
    public BYD3 getCar() {
        return new BYD3();
    }
    
}

public class Client03 {
    
    public static void main(String[] args) {
        Car3 a = new AudiFactory3().getCar();
        Car3 b = new BydFactory3().getCar();
        a.run();
        b.run();
    }

}

Abstract factory pattern

The difference between the abstract factory pattern and the method pattern lies in the complexity of creating objects. Moreover, the abstract factory pattern is the most abstract and general of the three.
The purpose of the abstract factory pattern is to provide an interface for the client to create product objects in multiple product families.
In addition, the following conditions must be met when using the abstract factory pattern:
1) There are multiple product families in the system, and the system can only consume one family of products at a time.
2) Products belonging to the same product family are used by them.

Posted by Sinemacula on Mon, 29 Nov 2021 01:57:46 -0800