Facade design pattern in Java and how to realize it with code

Keywords: Java network Tomcat Shiro

Facade design mode is also called appearance design mode. Its core idea is just like its literal meaning. It provides users with a portal. Users only need to access the portal to get the data they want, without managing the internal structure of the portal, and without knowing the running process. For developers, using facade mode, we can only provide users with them What you want, not all the information.

 

The above summary briefly describes the idea of facade design mode. We can help you understand this idea through examples in daily life: as you all know, a computer host is composed of many parts, and the most important one is a video card, CPU , motherboard, network card, sound card, etc. If we need to use computers, then we need to let them coordinate operation, such as simple startup action, we need to open these parts, but in the actual operation, we just need to press the startup key, and the computer will start. This power on button is the front of the computer. We only need to send instructions to this front, and the computer will automatically run a series of operations to start all components, instead of manually starting one by one. And the most important thing is that the computer only exposes a switch to us, but does not expose its components to us, which increases its security. When the starting process of the computer changes (operation expansion), users cannot feel it. Let's turn the above example into a code implementation! Startup and shutdown of main board:

public class MainBoard {

  public void start(){
    System.out.println("main board is open");
  }
  
  public void end(){
    System.out.println("main board is close");
  }
}

 

 

Startup and shutdown of video card:

public class VideoCard {

  public void start(){
    System.out.println("video card is open");
  }
  
  public void end(){
    System.out.println("video card is close");
  }
}

 

 

Startup and shutdown of network card:

 

public class NetCard {

  public void start(){
    System.out.println("net card is open");
  }
  
  public void end(){
    System.out.println("net card is close");
  }
}

 

 

Encapsulate the above operations into a facade:

public class FacadeInstall {
  
  private MainBoard mainBoard;
  
  private NetCard netCard;
  
  private VideoCard videoCard;

  public FacadeInstall(){
    mainBoard = new MainBoard();
    netCard = new NetCard();
    videoCard = new VideoCard();
  }
  
  public void start(){
    mainBoard.start();
    netCard.start();
    videoCard.start();
  }
}

 

 

Test class:

public class FacadeTest {

  public static void main(String[] args) {
    FacadeInstall facadeInstall = new FacadeInstall();
    facadeInstall.start();
  }
}

 

 

Operation result:

main board is open

net card is open

video card is open

 

 

 

We only need to call the start method of the facade object, which is equivalent to calling the start method of three parts. We don't need to know how you call the facade. Moreover, if the components of the computer are extended, we only need to extend the function in the facade class, which the customer can't feel. In the above code, we found that only the start method is called in the facade class, and the method that is not closed is called for us. This is another advantage of facade design pattern. It can hide the methods that are not necessary to be open to customers to ensure security.

 

For example, in tomcat When executing doGet or doPost, the parameters request and response are already facade classes, because tomcat will call before this:

filterChain.doFilter(request.getRequest(), response.getResponse())
 

 

The source code of these two parameters is as follows:

public HttpServletRequest getRequest() {
        if (facade == null) {
            facade = new RequestFacade(this);
        }
        return facade;
    }


public HttpServletResponse getResponse() {
        if (facade == null) {
            facade = new ResponseFacade(this);
        }
        return (facade);
}
 

 

 

In fact, facade design mode is widely used. If the core component of shiro, SecurityManager, is a typical facade design mode.

I collated Java advanced materials for free, including Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo high concurrency distributed and other tutorials, a total of 30G, which needs to be collected by myself.
Transfer gate: https://mp.weixin.qq.com/s/igMojff-bbmQ6irCGO3mqA

Posted by jrose83 on Mon, 23 Dec 2019 01:47:58 -0800