preface

Make writing a habit together! This is the 10th day of my participation in the “Gold Digging Day New Plan ยท April More text Challenge”. Click here for more details.

๐ŸŠ author introduction: refused to cross the river east, a programmer from the second-tier city, committed to using “obscene” method to solve tedious problems, so that complex problems become easy to understand. ๐ŸŠ Supported by: like ๐Ÿ‘, follow ๐Ÿ’–, leave a message ๐Ÿ’Œ~

Smart people find design patterns everywhere in the process of writing code, and design patterns are also solutions to common problems faced by software developers in the process of software development. Great wisdom in line with the purpose of “happy alone is better than all happy” to share with you the learning experience of design mode.

The appearance model

๐Ÿ“๐Ÿ“ What is appearance mode ๐Ÿ“๐Ÿ“

Before explaining the appearance pattern, let’s take a look at its definition ๐Ÿ‘‡

The Facade Pattern hides the complexity of the system and provides clients with an interface through which they can access the system. This type of design pattern is structural in that it adds an interface to an existing system to hide the complexity of the system.

In plain English, appearance patterns help us to mask the complex logic behind the code and instead provide the user with a simple interface to the system to implement the specific complex logic. Here are two simple examples to help you understand the meaning of appearance patterns

๐Ÿฅ Example 1 ๐Ÿฅ : We often use Java three-tier architecture (MVC) when developing application systems. We provide controller externally for the page to call, but many services may be called inside the controller, which in turn calls some Mapper. In short, the internal logic of the code is very complex, but externally it is just a simple and clean interface.

๐Ÿฅ Example 2 ๐Ÿฅ : When we go to the bank to apply for a bank card, there will always be a reception staff to help us with the process, because the process of applying for a bank card is relatively complicated, and we need to fill in forms, input information and other processes to complete the bank card. Therefore, relying on users to operate by themselves is very time-consuming, and the user experience will be greatly reduced. Therefore, the bank provides us with a receptionist, who helps us avoid most of the tedious operations in the process, so that we can feel efficient and convenient when handling cards.

From the above two examples, we can see that when the client does not need to know the complex relationships inside the system but only cares about how to use them, the whole system needs to provide a “receptionist” to circumvent the complex business logic inside the system. This process of providing a receptionist uses the appearance pattern. The facade provides a consistent interface for a set of interfaces in a subsystem. The facade defines a high-level interface that makes the subsystem easier to use. It helps us reduce the complexity of accessing the internal subsystems of a complex system, simplify the interface between clients, and make it easier to make calls between systems (or before and after).

๐Ÿ“๐Ÿ“ Implementation of the appearance mode ๐Ÿ“๐Ÿ“

Through the example of bank card processing mentioned above, we can see that the appearance mode is mainly divided into three roles:

  • Facade role: A combination of multiple subsystem functions to provide a common external interface, namely the bank receptionist
  • The role of the subsystem: to achieve some of the functions of the system, that is, when the bank card is responsible for the input of information, business card printing and other operations of the machine
  • Customer role: Access the functions of each subsystem through the appearance role, that is, the user who handles the bank card

Below we use the code to simply implement the appearance pattern ๐Ÿ‘‡

โ‘  Abstract each operation of bank card into a component

/** * Abstracts each operation of a bank card into a component *@description: Component
 * @author: Zhuang Ba. Liziye *@create: the 2022-04-07 09:43 * * /
public interface Component {
    public void work(a);
}
Copy the code

(2) Define input information, making cards and other components, and implement interfaces respectively

/** * implements the interface to define each operation step *@author: Zhuang Ba. Liziye *@create: 2022-04-07 09:44 * * /
class InputInfo implements Component{
    @Override
    public void work(a) {
        System.out.println("Now we are inputting basic user information ~"); }}class InputPassword implements Component{
    @Override
    public void work(a) {
        System.out.println("Now inputting the bank card password ~"); }}class MakingCard implements Component{
    @Override
    public void work(a) {
        System.out.println("Fill in the information, now start to make bank cards ~"); }}Copy the code

โ‘ข The above components need to work together, the following simulation of the process of bank card, so that the operating system in the machine to call them respectively

/ * * *@description: OperationSystem
 * @author: Zhuang Ba. Liziye *@create: the 2022-04-07 probably * * /
public class OperationSystem {

    private Component inputInfo;
    private Component inputPassword;
    private Component makingCard;

    public OperationSystem(a) {
        this.inputInfo = new InputInfo();
        this.inputPassword = new InputPassword();
        this.makingCard = new MakingCard();
    }

    public void startingWork(a){
        System.out.println("Start to apply for bank card ~"); inputInfo.work(); inputPassword.work(); makingCard.work(); }}Copy the code

โ‘ฃ Users apply for bank cards

/ * * *@description: Test
 * @author: Zhuang Ba. Liziye *@create: the 2022-04-07 10:01 * * /
public class Test {
    public static void main(String[] args){
        OperationSystem operationSystem = newOperationSystem(); operationSystem.startingWork(); }}Copy the code

โ‘ค The running results are as follows

๐Ÿ“๐Ÿ“ Advantages and disadvantages of the appearance mode ๐Ÿ“๐Ÿ“

Finally, we summarize the advantages and disadvantages of the appearance pattern ๐Ÿ‘‡

๐ŸŒ ๐ŸŒ advantages ๐ŸŒ ๐ŸŒ

Decorator patterns reduce system dependencies (external system dependencies); Improve flexibility; Improved security

๐ŸŒ ๐ŸŒ faults ๐ŸŒ ๐ŸŒ

Mixing a lot of things together can create unknown risks. Adding new subsystems may require modifications to the facade class or client source code, violating the “open closed rule”

summary

My experience is limited, some places may not be particularly in place, if you think of any questions when reading, welcome to leave a message in the comments section, we will discuss one by one ๐Ÿ™‡

Please take a thumbs up and a follow (โœฟโ—กโ€ฟโ—ก) for this article ~ a crab (โ—’โ—ก’โ—)

If there are mistakes in the article, welcome to comment correction; If you have a better, more unique understanding, you are welcome to leave your valuable ideas in the comments area.

Love what you love, do what you do, listen to your heart and ask nothing