"Design Patterns" Study Notes 8 - Appearance Patterns

definition

The definitions in the Appearance Mode Reference Book are as follows:

Provides a unified entry for a set of interfaces in a subsystem. The Facade pattern defines a high-level interface that makes this subsystem easier to use.
Facade mode, also known as facade mode, is an object-structured mode. Appearance mode
is . By introducing a new appearance role, the complexity of the original system can be reduced, and the coupling between client classes and subsystems can be reduced
.

understand

For the understanding of this mode, I think the focus is on a few words, namely 一组接口, 统一入口, 降低复杂度.
Combined with our current system to illustrate, first of all what is a set of interfaces.
In our system, due to business needs, there are three kinds of logs, namely system log, alarm log, and monitoring log. I use informal code to simply express it as follows:
System log class interface definition:

/**
 * Log unified management class, appearance class
 *
 * @author tzx
 * @date January 4, 2018
 */
public class LogHandle {
    private SysLogService sysLogService = new SysLogService();
    private AlarmLogService alarmLogService = new AlarmLogService();
    private MonitorLogService monitorLogService = new MonitorLogService();

    public void error(String msg, Object... args) {
        sysLogService.error(msg, args);
        alarmLogService.error(msg, args);
        monitorLogService.error(msg, args);
    }
}

Alarm log class interface definition:

/**
 * Alarm log interface
 *
 * @author tzx
 * @date January 4, 2018
 */
public class AlarmLogService {
    private Logger sysLog = LoggerFactory.getLogger(this.getClass());

    public void error(String msg, Object... args) {
        String sys = "alarm";
        msg = "#" + msg + "#" + sys + "#";
        sysLog.error(msg, args);
    }
}

Monitoring log class interface:

/**
 * Monitoring log interface
 *
 * @author tzx
 * @date January 4, 2018
 */
public class MonitorLogService {
    private Logger sysLog = LoggerFactory.getLogger(this.getClass());

    public void error(String msg, Object... args) {
        String sys = "monitor";
        msg = "|" + msg + "|" + sys + "|";
        sysLog.error(msg, args);
    }
}

The above three log interfaces simply spliced ​​the log content differently in order to express the difference in functions, which is naturally not the case in actual projects.
These three log interfaces are all log operations, but the printed log content is collected and used by different systems, and they are basically used at the same time, so it can be said that they belong to the appearance mode definition. 一组接口.
Without using the appearance mode, when we need to print the log in the business code, it may look like the following:

/**
 * Blog operation class
 *
 * @author tzx
 * @date January 4, 2018
 */
public class BlogService {
    private SysLogService sysLogService = new SysLogService();
    private AlarmLogService alarmLogService = new AlarmLogService();
    private MonitorLogService monitorLogService = new MonitorLogService();

    public void addBlog() {
        try{
        System.out.println("");
        }catch (Exception e) {
            sysLogService.error("System exception{}", new Object[] { e.getMessage() });
            alarmLogService.error("System exception{}", new Object[] { e.getMessage() });
            monitorLogService.error("System exception{}", new Object[] { e.getMessage() });
        }
    }
}
/**
 * User action class
 *
 * @author tzx
 * @date January 4, 2018
 */
public class UserService {
    private SysLogService sysLogService = new SysLogService();
    private AlarmLogService alarmLogService = new AlarmLogService();
    private MonitorLogService monitorLogService = new MonitorLogService();

    public void addUser() {
        try {
            System.out.println("");
        } catch (Exception e) {
            sysLogService.error("System exception{}", new Object[] { e.getMessage() });
            alarmLogService.error("System exception{}", new Object[] { e.getMessage() });
            monitorLogService.error("System exception{}", new Object[] { e.getMessage() });
        }
    }
}

Obviously, there is a lot of duplicate code in the two services. There are only two classes to print logs, so a total of 6 log printing methods need to be called. Each additional class that needs to print such logs will increase the code in multiples of 3.
And the example here is almost the simplest, and it may be far more than three lines in actual business development.
At the same time, like the above approach, if the subsequent log system increases, it needs to be increased to four or even five or more, and each class that prints the log needs to be modified accordingly. Obviously, this is extremely unfavorable for maintenance and maintenance. expanded.
Therefore, according to the conventional thinking, there may be two approaches:
one is to extract the code for printing the log and encapsulate it as a method to call. However, this approach will have certain problems. First, this method itself has nothing to do with the business, and it is not suitable to be placed in the business class. Second, unless all classes that need to print logs have a common parent class and put this method in the parent class, you need to define this method multiple times.
So, there is a second approach, which is to extract this code into another class, then this class is the so-called appearance class:

/**
 * Log unified management class, appearance class
 *
 * @author tzx
 * @date January 4, 2018
 */
public class LogHandle {
    private SysLogService sysLogService = new SysLogService();
    private AlarmLogService alarmLogService = new AlarmLogService();
    private MonitorLogService monitorLogService = new MonitorLogService();

    public void error(String msg, Object... args) {
        sysLogService.error(msg, args);
        alarmLogService.error(msg, args);
        monitorLogService.error(msg, args);
    }
}

With such a class, our business code only needs to be called once, and this class that manages log operations is 统一接口.

/**
 * User action class
 *
 * @author tzx
 * @date January 4, 2018
 */
public class UserService {
    private LogHandle logHandle = new LogHandle ();
    public void addUser() {
        try {
            System.out.println("");
        } catch (Exception e) {
            logHandle.error("System exception{}", new Object[] { e.getMessage() });
        }
    }
}

At the same time, if the subsequent log system changes, whether it is adding a new log type or deleting an old log type, it is only necessary to change the logHandle class, and the code of each call log is also reduced a lot. That's great 降低了系统的复杂度.

gist

Then according to the above understanding and examples, the appearance mode has the following points:
1. There needs to be a set of interfaces, and they are almost all used in sets;
2. A facade class or interface, which actually operates the above set of interfaces, and provides a public interface
3. The client only needs to call the interface of the facade class once, and does not need to call each of the first group of interfaces separately .

When I didn't learn this pattern, I thought the singleton pattern was the simplest design pattern, but now, this seems to be the simplest design pattern.

The demo source code can be downloaded from github: https://github.com/tuzongxun/mypattern

Guess you like

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