[Design pattern] template method pattern and facade pattern

template method pattern

The template method pattern is very simple. It defines a fixed public process. The steps of the whole process are defined in advance, and the specific steps are implemented by subclasses. Belongs to behavioral design patterns.
To put it simply, it is to extract the public behaviors and put them in the parent class, and the different behaviors are handed over to the subclasses for their own implementation.

code example

The template mode is very simple. We use it to implement an example of going to the bank to handle business. No matter what business you go to the bank to handle, you need to go through several steps: taking the number, queuing, and handling the business. A hook method is also added here. For a certain In some special cases, additional processing steps can be added

public abstract class AbstractBank {
    
    

    public void doBiz() {
    
    
        if(!isVip()) {
    
    
            getNum();
            waitForCall();
        }
        doBusiness();
    }

    protected abstract boolean isVip();

    public abstract void doBusiness();

    private void waitForCall() {
    
    
        System.out.println("排队中,等待叫号...");
    }

    private void getNum() {
    
    
        System.out.println("取号,准备排队...");
    }

}

public class CrashBusiness extends AbstractBank{
    
    

    @Override
    protected boolean isVip() {
    
    
        return false;
    }

    @Override
    public void doBusiness() {
    
    
        System.out.println("办理现金业务...");
    }
}
public class DebitBusiness extends AbstractBank{
    
    

    @Override
    protected boolean isVip() {
    
    
        return true;
    }

    @Override
    public void doBusiness() {
    
    
        System.out.println("办理贷款业务.....");
    }
}
public class Test {
    
    
    public static void main(String[] args) {
    
    
        CrashBusiness crashBusiness = new CrashBusiness();
        crashBusiness.doBiz();
        new DebitBusiness().doBiz();
    }
}

facade mode

Facade pattern, also known as appearance pattern, the English full name is Facade Design Pattern.
In the GoF's "Design Patterns" book, the facade pattern is defined as follows:

Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.

The facade pattern provides a set of unified interfaces for the subsystem, and defines a set of high-level interfaces to make the subsystem easier to use. Belongs to the structural model.

A classic example is the interaction between the app and the server. The app has a complex module that needs to obtain corresponding data from three systems to assemble. If the app interacts with the three systems, it needs to perform three network transmissions and add an intermediate layer. Integrating and encapsulating the interfaces of the three systems, and providing an interface to the app for external calls, then the performance can be significantly improved

code example

We need to obtain the order information according to the user's account, then we first need to judge whether the user has logged in according to the user account, and the logged-in user can obtain the corresponding id according to the account, and then obtain the order information corresponding to the id


public class LoginService {
    
    

    public boolean isLogin(String account) {
    
    
        return true;
    }
}

public class GetUserInfo {
    
    

    public int getUserId(String account) {
    
    
        System.out.println("根据用户名称获取用户id");
        return 1;
    }
}
public class GetUserOrderList {
    
    

    public List<String> getOrderList(Integer id) {
    
    
        System.out.println("根据用户id找出其订单");
        return new ArrayList<>();
    }
}

public class OrderService {
    
    

    GetUserInfo userInfo = new GetUserInfo();
    GetUserOrderList userOrderList = new GetUserOrderList();
    LoginService loginService = new LoginService();

    public List<String> getOrderByUser(String account) throws Exception {
    
    
        if (loginService.isLogin(account)) {
    
    
            Integer id = userInfo.getUserId(account);
            return userOrderList.getOrderList(id);
        }
        throw new Exception("not login");
    }
}
public class Test {
    
    

    public static void main(String[] args) throws Exception {
    
    
        OrderService service = new OrderService();
        service.getOrderByUser("xiaoming");
    }
}

Application Scenarios of the Facade Pattern

  1. Fix usability issues

The facade mode can be used to encapsulate the underlying implementation of the system, hide the complexity of the system, and provide a set of easier-to-use, higher-level interfaces. For example, the calling function of the Linux system can be regarded as a kind of "facade". It is a set of "special" programming interfaces exposed to developers by the Linux operating system, which encapsulates the underlying more basic Linux kernel calls.
From the perspective of hiding the complexity of implementation and providing an easier-to-use interface, the facade pattern is somewhat similar to Dimiter's Law (the principle of least knowledge) and the principle of interface isolation: two interacting systems only expose limited The necessary interface.
In addition, the facade mode is somewhat similar to the design idea of ​​encapsulation and abstraction mentioned earlier, providing a more abstract interface and encapsulating the underlying implementation details.

  1. Solve performance problems
    By replacing multiple interface calls with one facade interface call, the cost of network communication is reduced and the response speed of the App client is improved.

Guess you like

Origin blog.csdn.net/qq_35448165/article/details/129451625