Java Design Patterns: Facade Pattern

facade mode

definition:

To provide a consistent interface for a set of interfaces in a subsystem, the Facade pattern defines a high-level interface that makes the subsystem easier to use.

Application scenario:

The Facade pattern is used when a limited but direct interface to complex subsystems is required.

Use the Facade pattern when you want to organize subsystems into layers.

advantage:

1) Easier to use: The subsystem is easier to use. The client no longer needs to understand the internal implementation of the subsystem, and does not need to interact with the modules inside the subsystem. It only needs to interact with the Facade.

2) Loose coupling: decouple the client from the subsystem, so that the modules inside the subsystem can be expanded and maintained more easily.

3) Better division of access levels: By using Facade reasonably, access levels can be better divided. Some methods are used outside the system, and some methods are used inside the system. Concentrate the functions that need to be exposed to the outside into the facade, which is convenient for the client to use and well hides the internal details.

Code example:

Let's take the computer on and off as an example, in which CPU, Disk, and Memory are computer subsystems, and Computer is equivalent to the Facade class. With this Facade class, the client does not need to call the CPU, Disk, and Memory modules in the subsystem, nor does it need to know the internal implementation details of the system, or even know the existence of the CPU, Disk, and Memory modules. The client only needs to interact with the Facade class, which better realizes the decoupling of the CPU, Disk, and Memory modules in the client and subsystems, making it easier for the client to use the system.

package com.test;

public class Facade {
    
    

    public static void main(String[] args) {
    
    
        Computer computer = new Computer();
        computer.startup();
        computer.shutdown();
    }

}

class Computer {
    
    

    private CPU cpu;

    private Memory memory;

    private Disk disk;

    Computer(){
    
    
        cpu = new CPU();
        memory = new Memory();
        disk = new Disk();
    }


    public void startup(){
    
    
        cpu.startup();
        memory.startup();
        disk.startup();
        System.out.println("computer startup complete...");
    }

    public void shutdown(){
    
    
        cpu.shutdown();
        memory.shutdown();
        disk.shutdown();
        System.out.println("the computer is powered off...");
    }

}


class CPU {
    
    

    public void startup(){
    
    
        System.out.println("startup cpu...");
    }

    public void shutdown(){
    
    
        System.out.println("shutdown cpu...");
    }

}


class Memory {
    
    

    public void startup(){
    
    
        System.out.println("startup memory...");
    }

    public void shutdown(){
    
    
        System.out.println("shutdown memory...");
    }

}

class Disk {
    
    

    public void startup(){
    
    
        System.out.println("startup disk...");
    }

    public void shutdown(){
    
    
        System.out.println("shutdown disk...");
    }

}

Guess you like

Origin blog.csdn.net/qq_40042416/article/details/128515296