Factory mode of design mode

Factory mode:

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern is a 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.

Plant mode classification:

1. Simple factory

2. Abstract factory

Simple factory

Use a simple factory to create interfaces, but you can also use a simple factory to create instances of abstract classes or ordinary classes.

Example: creating objects with a simple factory

  1. First define an interface in which methods are defined
public interface Api {
    void print();
}
  1. Different types of objects implement the interface and the methods that implement the interface.
public class ApiImpl1 implements Api {
    @Override
    public void print() {
        System.out.println("ApiImpl1.......");
    }
}
public class ApiImpl2 implements Api {
    @Override
    public void print() {
        System.out.println("ApiImpl2....");
    }
}
  1. Define a factory and create different objects according to the type.
public class ApiFactory {
    public static Api createApi(int type) {
        Api api = null;
        if (1 == type) {
            api = new ApiImpl1();
        } else if (2 == type) {
            api = new ApiImpl2();
        }
        return api;
    }
}
  1. Create different objects according to their types and call their methods.
public class SimpleFactoryMode {
    public static void main(String[] args) {
        Api api = ApiFactory.createApi(1);
        api.print();
    }
}

Advantages and disadvantages of simple factory

advantage:
1. Help encapsulation (although the simple factory is very simple, it is very friendly to help us realize the encapsulation of components, and then make the external components truly interface oriented programming)
2. Decoupling (through a simple factory, the client is decoupled from the specific implementation class. For example, in the above example, the implementation logic of the object creation cannot be known, and the client only obtains the interface it needs through the factory.

Disadvantages:
1. It may increase the complexity of the client.
Because the simple factory needs to pass the type when creating the object, the corresponding object can be created. This increases the difficulty of using the client and exposes the internal implementation.
2. Inconvenient to expand sub factory
Privatize the construction method of the simple factory and use the static method to create the interface, so you can't change the behavior of the method of creating the interface by writing the subclass of the simple factory class. However, in general, you do not need to create subclasses for simple factories

Abstract factory

Each pattern is a solution to a certain problem. The biggest difference between abstract factory pattern and factory method pattern is that factory method pattern is aimed at a product hierarchy; The abstract factory pattern needs to face multiple product hierarchy.

If you need to define a tool class to connect to the database, but in order to adapt to various types of databases, you need to define a set of regulations to combine a series of products.

Define two methods

public interface Commond {
    void sendCommond();
}


public interface Connection {
    void getConnect();
}

Mysql and Oracle implementation

public class MysqlCommond implements Commond {
    @Override
    public void sendCommond() {
        System.out.println("mysql send commond.");
    }
}

public class MysqlConnect implements Connection {
    @Override
    public void getConnect() {
        System.out.println("mysql is connection");
    }
}

public class OracleCommond implements Commond {
    @Override
    public void sendCommond() {
        System.out.println("orecle send commond.");
    }
}

public class OracleConnect implements Connection{
    @Override
    public void getConnect() {
        System.out.println("oracle is connection.");
    }
}

Connection tool class

public class MysqlConnectionUtils  implements AbstractFactory{
    @Override
    public Connection getConnection() {
        return new MysqlConnect();
    }

    @Override
    public Commond sendCommond() {
        return new MysqlCommond();
    }
}

public class OracleConnectionUtils implements AbstractFactory {
    @Override
    public Connection getConnection() {
        return new OracleConnect();
    }

    @Override
    public Commond sendCommond() {
        return new OracleCommond();
    }
}

test method

public class AbstractFactoryTest {
    public static void main(String[] args) {
        //Connect to Mysql database
        AbstractFactory abstractFactory = new OracleConnectionUtils(); // new MysqlConnectionUtils();
        Connection connection = abstractFactory.getConnection();
        connection.getConnect();
        Commond commond = abstractFactory.sendCommond();
        commond.sendCommond();
    }
}

Advantages and disadvantages of abstract factory

advantage:
1. The client uses the abstract factory to create the required objects, and the client does not know who the specific implementation is. The client is just product-oriented interface programming. That is, the client is decoupled from the specific product implementation.
2. Because a specific factory represents a product family, you can switch to a specific factory only by switching products.

Disadvantages:
If you need to add a new product to the whole product, you need to modify the abstract factory, which will lead to the modification of all factory implementation classes.

Keywords: Java

Added by kincet7 on Wed, 12 Jan 2022 20:19:14 +0200