Appearance mode of design mode (structural type)

[TOC]

I. model definition

Appearance mode: appearance mode is to provide a unified interface to access a group of interfaces of subsystem. The facade pattern defines a high-level interface that makes subsystems easier to use. Appearance mode, also known as facade mode, is an object structural design mode.

Insert picture description here

II. Model role

As you can see from the pattern definition, the appearance pattern should include the following roles:

  • Frcade: appearance character
  • SubSystem: SubSystem role
  • Client: client role

Classic example:

public class Facade
{
    private SubSystemA obj1 = new SubSystemA();
    private SubSystemB obj2 = new SubSystemB();
    private SubSystemC obj3 = new SubSystemC();
    public void method()
    {
        obj1.method();
        obj2.method();
        obj3.method();
    }
} 

3. Simple analysis of the model

The appearance mode provides convenience for the client class. The client class does not need to pay attention to the design of the subsystem. It is good to provide appearance class access directly

The appearance mode conforms to "Dimiter's law", introduces a single simple interface to call the client, thus reducing the coupling degree between the client and the subsystem

However, appearance pattern also has some disadvantages. Each design pattern has its disadvantages and advantages. It needs to be selected according to complex business scenarios. If an abstract appearance class is not referenced, the appearance class and client class code need to be adjusted once the business changes

Insert picture description here

For some very complex business systems, sometimes multiple appearance classes can be designed for system decoupling

IV. simple example practice

JDBC database operation example, this example comes from Design mode A Book

import java.sql.*;

public class JDBCFacade {
    
    private Connection conn=null;
    private Statement statement=null;

    public void open(String driver,String jdbcUrl,String userName,String userPwd) {
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(jdbcUrl,userName,userPwd);
            statement = conn.createStatement();
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int executeUpdate(String sql) {
        try {
            return statement.executeUpdate(sql);
        } 
        catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public ResultSet executeQuery(String sql) {
        try {
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void close() {
        try {
            conn.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

V. mode application

Appearance mode is suitable for complex system and can be used for system decoupling. Here are some application scenarios of appearance mode

  • Session in Java EE framework uses appearance mode

  • Learning JSP's JDBC database operation is also often using appearance mode

Tags: SQL JDBC Database Java

Posted on Mon, 02 Dec 2019 11:57:16 -0500 by Myke