This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

The appearance model

Also known as the facade pattern, it is a pattern that makes multiple complex subsystems more accessible by providing a consistent interface to them. This mode has a unified external interface, external applications do not care about the details of the internal subsystem, which will greatly reduce the complexity of the application, improve the maintainability of the programCopy the code

structure

Facade Role: Provides a common interface externally for multiple subsystems.

Sub System roles: Implement part of the functions of the System and can be accessed by customers through appearance roles.

Client role: Access the functionality of each subsystem through a facade role.

demo

1. Appearance system

The appearance system is an e-commerce platform, with clothing supply from Li Ning brand and Hongxing Erke brandCopy the code
public class Facade {
    private LiNingSub liNingSub;
    private ErkeSub erkeSub;
    public Facade(a) {
        this.liNingSub = new LiNingSub();
        this.erkeSub = new ErkeSub();
    }
    public void buyClothes(String name) {
        if ("lining".equals(name)) {
            this.liNingSub.buyClothes();
        } else if ("erke".equals(name)) {
            this.liNingSub.buyClothes();
        } else {
            System.out.println("There is no such brand!");
            return;
        }
        System.out.println("Clothes purchase success!"); }}Copy the code

2. Subsystem

Supply subsystem of e-commerce systemCopy the code
public class LiNingSub {
    public void buyClothes(a) {
        System.out.println("Li Ning clothes for sale!"); }}public class ErkeSub {
    public void buyClothes(a) {
        System.out.println("Hongxing Erke clothes for sale!"); }}Copy the code

3. Client

public class Client {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.buyClothes("lining"); 
        // Li Ning for sale!
        // Successful purchase of clothes!
        facade.buyClothes("erke");
        // Li Ning for sale!
        // Successful purchase of clothes!
        facade.buyClothes("1234");
        // This brand is not available!}}Copy the code

As can be seen from the above demonstration, the appearance mode is very simple. The client only needs to pay attention to whether the clothing is purchased successfully, rather than where the clothing comes from

conclusion

advantages

  • The coupling degree between subsystem and client is reduced, so that the change of subsystem does not affect the client class that calls it.
  • Shielding subsystem components from the customer reduces the number of objects the customer processes and makes the subsystem easier to use.
  • This reduces compilation dependencies in large software systems and simplifies the migration of systems from platform to platform, because compiling one subsystem does not affect other subsystems, nor does it affect appearance objects.

disadvantages

The disadvantages are also obvious. Adding a new subsystem may require modifying the source code of the appearance class, violating the open close principle.

Usage scenarios

  1. When a complex system has many subsystems, facade can design a simple interface for the system to be accessed by the outside world.
  2. When there is a large connection between the client and multiple subsystems, the introduction of facade patterns can separate them, thus improving subsystem independence and portability.
  3. When building a hierarchical system, using a facade pattern to define entry points for each layer in a subsystem simplifies dependencies between subsystems.