Architect's internal mental skill, detailed explanation of the door mode of the only internal channel of the external access system

1, Application scenario of facade mode

Facade Pattern, also known as Facade Pattern, provides a unified interface to access a group of interfaces in the subsystem. The main feature is to define a high-level interface to make the subsystem easier to use. In our daily work, we use facade mode consciously or unconsciously. As long as high-level modules need to schedule multiple subsystems, we will encapsulate a new class, provide a simplified interface, and make high-level modules easily call the functions of these subsystems indirectly. Especially at this stage, all kinds of third-party API s, all kinds of open-source class libraries, are likely to use facade mode.

The facade mode is applicable to the following scenarios:

  • The subsystem is more and more complex, adding facade mode to provide a simple interface;
  • The multi-layer system structure is constructed, and the door object is used as the entrance of each layer to simplify the inter layer call.

There are two main roles in facade mode:

  • Facade: it also becomes the facade role, and the system's external unified interface;
  • SubSystem: can have one or more SubSystem roles.

1.1 general writing method of facade mode

The following is the general code of facade pattern. First, create the business logic of the three subsystems: subsystem a, subsystem B, and subsystem C

public class SubSystemA {
    public void doA() {
        System.out.println("doing A stuff");
    }
}

public class SubSystemB {
    public void doB() {
        System.out.println("doing B stuff");
    }
}

public class SubSystemC {
    public void doC() {
        System.out.println("doing C stuff");
    }
}

Then, create the Facade class for the appearance character:

public class Facade {
    private SubSystemA a = new SubSystemA();
    private SubSystemB b = new SubSystemB();
    private SubSystemC c = new SubSystemC();
    // External interface
    public void doA() {
        this.a.doA();
    }
    // External interface
    public void doB() {
        this.b.doB();
    }
    // External interface
    public void doC() {
        this.c.doC();
    }
}

Test the main method:

 public static void main(String[] args) {
    Facade facade = new Facade();
    facade.doA();
    facade.doB();
    facade.doC();
}

2, The embodiment of facade pattern in source code

2.1 JdbcUtils in spring JDBC

JdbcUtils encapsulates all JDBC related operations. It is a code fragment:

public abstract class JdbcUtils {
    public static final int TYPE_UNKNOWN = -2147483648;
    private static final Log logger = LogFactory.getLog(JdbcUtils.class);
    public JdbcUtils() {
    }
    public static void closeConnection(Connection con) {
    if(con != null) {
        try {
            con.close();
        } catch (SQLException var2) {
            logger.debug("Could not close JDBC Connection", var2);
        } catch (Throwable var3) {
            logger.debug("Unexpected exception on closing JDBC Connection", var3);
        }
    }
    }
    public static void closeStatement(Statement stmt) {
        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException var2) {
                logger.trace("Could not close JDBC Statement", var2);
            } catch (Throwable var3) {
                logger.trace("Unexpected exception on closing JDBC Statement", var3);
            }
        }
    }
    public static void closeResultSet(ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException var2) {
                logger.trace("Could not close JDBC ResultSet", var2);
            } catch (Throwable var3) {
                logger.trace("Unexpected exception on closing JDBC ResultSet", var3);
            }
        }
    }
...
}

2.2 RequestFacade class in Tomcat's source code

public class RequestFacade implements HttpServletRequest {
...
    @Override
    public String getContentType() {
        if (request == null) {
            throw new IllegalStateException(
                sm.getString("requestFacade.nullRequest"));
        }
        return request.getContentType();
    }
    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (request == null) {
            throw new IllegalStateException(
                sm.getString("requestFacade.nullRequest"));
        }
        return request.getInputStream();
    }
    @Override
    public String getParameter(String name) {
        if (request == null) {
            throw new IllegalStateException(
                sm.getString("requestFacade.nullRequest"));
        }
        if (Globals.IS_SECURITY_ENABLED){
            return AccessController.doPrivileged(
            new GetParameterPrivilegedAction(name));
        } else {
            return request.getParameter(name);
        }
    }
...
}

We can see from the name that it uses the facade mode. It encapsulates a lot of request operations and integrates a lot of content other than servlet API, which provides users with great convenience. Similarly, Tomcat also encapsulates the ResponseFacade and standardsessionfront classes for Response and Session. Interested partners can have a deeper understanding.

3, Advantages and disadvantages of facade mode

Advantage:

  • It simplifies the calling process and does not need to have a deep understanding of the subsystem in order to prevent risks to the subsystem;
  • Reduce system dependence and loose coupling;
  • The access level is better divided and the security is improved;
  • Follow Dimitar's law, the principle of least knowledge.

Disadvantages:

  • When adding subsystem and expanding subsystem behavior, it may bring unknown risk;
  • Does not conform to the opening and closing principle;
  • In some cases, the principle of single responsibility may be violated.

Tags: Programming JDBC Tomcat Spring Fragment

Posted on Sat, 07 Mar 2020 00:51:48 -0800 by ThaSpY