2- Design Patterns - Simple Factory Pattern and Factory Method Pattern

Simple Factory Pattern

As a kind of creational pattern, the factory pattern first abstracts the products that need to be produced, and extracts a public interface, and each product is the implementation class of this interface. Then create a factory class. When our factory class produces products, we only need to return the interface.
Refer to this blog here

http://blog.csdn.net/qq_28055429/article/details/51628921

The simple factory pattern is a creational pattern, also known as the static factory method (Static Factory Method) pattern, but it is not one of the 23 GOF design patterns

advantage:

The factory class contains the necessary logic, and dynamically instantiates the relevant classes according to the conditions required by the customer. For the client, the dependence on the specific product is removed.

shortcoming:

The factory class concentrates the creation logic of all instances, violating the principle of high cohesion responsibility distribution, and when adding new stuff, it will violate the open-closed principle

accomplish

Three databases are known, MySQL, Oracle, and SQLServer. Now there is a project. The project is not sure which database to use. At this time, we use the factory mode. The factory provides a public interface for the project to connect to the database, and the specific Connections are hidden.

Product interface - database connection interface

public interface DBConnect {
    /**
     * 数据库连接方法
     * @return void
     * 时间:2018年4月24日
     */
    public void connect();
}

Specific Products - Three Database Connections

public class MySQLConnect implements DBConnect{

    @Override
    public void connect() {
        System.out.println("Mysql Connect");
    }

}
public class OracleConnect implements DBConnect{

    @Override
    public void connect() {
        System.out.println("Oracle Connect");
    }

}
public class SQLServerConnect implements DBConnect{

    @Override
    public void connect() {
        System.out.println("SQLServer Connect");
    }

}

Factory class - database connection factory

public class ConnectFactory {
    public static DBConnect getConnect(String type) {
        if (null == type || 0 == type.length()) {
            return null;
        }
        if ("mysql".equalsIgnoreCase(type)) {
            return new MySQLConnect();
        }
        if ("oracle".equalsIgnoreCase(type)) {
            return new OracleConnect();
        }
        if ("sqlserver".equalsIgnoreCase(type)) {
            return new SQLServerConnect();
        }
        return null;
    }
}

customer class

public class FactoryPatternDemo {

    public static void main(String[] args) {
        ConnectFactory connectFactory = new ConnectFactory();
        DBConnect dbConnect = connectFactory.getConnect("mysql");
        dbConnect.connect();
    }
}

At this point, if we need to connect to the Access database again, we need to modify the factory class, which violates the open-closed principle.

Factory Method Pattern

Define an interface for creating objects, let subclasses decide which class to instantiate, and factory methods defer instantiation of a class to its subclasses.

four elements

  • Factory interface: The factory interface is the core of the factory method pattern, which directly interacts with the caller to provide products. In actual programming, an abstract class is sometimes used as an interface for interacting with the caller, which is essentially the same.
  • Factory implementation: In programming, factory implementation determines how to instantiate a product, which is the way to achieve expansion. As many products are required, there are as many specific factory implementations as needed.
  • Product interface: The main purpose of the product interface is to define the specification of the product, and all product implementations must follow the specification defined by the product interface. The product interface is what the caller cares about most, and the quality of the product interface definition directly determines the stability of the caller's code. Similarly, the product interface can also be replaced by an abstract class, but be careful not to violate the Liskov Substitution Principle.
  • Product implementation: The concrete class that implements the product interface determines the specific behavior of the product in the client.

advantage

Create the interface of the object, let the subclass decide the specific instantiated object, and move the simple internal logic judgment to the client code. The factory method overcomes the shortcomings of simple factories that violate the open-closed principle, while maintaining the advantages of encapsulating the object creation process.

accomplish

We still take the case of a database connection to illustrate.

product interface

public interface DBConnect {

    /**
     * 数据库连接方法
     * @return void
     * 时间:2018年4月24日
     */
    public void getConnect();
}

specific product

public class MySQLConnect implements DBConnect{

    @Override
    public void getConnect() {
        System.out.println("MysqlConnect");
    }

}
public class SQLServerConnect implements DBConnect{

    @Override
    public void getConnect() {
        System.out.println("SQLServer Connect");
    }

}
public class OracleConnect implements DBConnect{

    @Override
    public void getConnect() {
        System.out.println("Oracle Connect");
    }

}

factory interface

public interface ConnectFactory {

    /**
     * 返回数据库连接对象
     * @return DBConnect
     * @return
     * 时间:2018年4月24日
     */
    public DBConnect getDBConnect();
}

Concrete Factory - MySQL Factory

public class MySQLConFactory implements ConnectFactory{

    @Override
    public DBConnect getDBConnect() {
        return new MySQLConnect();
    }

}

customer class

public class FactoryMethodPatternDemo {
    public static void main(String[] args) {
        DBConnect connect = new MySQLConFactory().getDBConnect();
        connect.getConnect();
    }
}

From here we can see the difference between the factory method pattern and the simple factory pattern

  • The simple factory pattern just abstracts the product, creating a public interface for the product, and a concrete implementation for the factory. When we need to add a new product, in addition to adding the implementation class of the product, we also need to modify the specific factory class and add corresponding judgments to the if
  • Compared with the simple factory, the factory method pattern is a clever step. It not only abstracts the product, but also abstracts the factory.
  • The product interface and implementation of the factory method pattern and the simple factory pattern can be said to have the same purpose, which can be regarded as the same, which is reflected in the above case code
  • But the factory method pattern abstracts the factory, it provides a factory interface, this factory interface only provides a method to create a product interface. Which product to generate is determined by the implementation class of the factory interface.
  • In this way, when we need to add a new Access database, we only need to create the corresponding product implementation class and factory implementation class, which satisfies the open-closed principle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325527112&siteId=291194637