This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

Colleagues recently two dogs to decorate a house, before decorate, he’ll have to find a designer, and also need to find a plumber to new house to water and electricity, and then, still have to find a gentleman, ceramic tile, paint the walls and the process to recruit people, talk about the price, with the schedule, and two dogs a programmer to write code, which meeting the ah, he is too much trouble, If only he had someone to do it for him.

Ah, this thing easy to do ah, look for a decoration company.

With the decoration company, two dogs only need to talk about a good price with the decoration company, and then wait for the house decoration, do not have to worry about some details.

This is the same as the facade design pattern we talked about today.

Facade pattern definition

GoF facade design pattern definition: Provides a uniform interface for a set of interfaces in a subsystem. Facade patterns define high-level interfaces that make subsystems easier to use.

Facade Design Pattern is one of the structural Design patterns, which helps clients to interact with the system more easily.

Facade pattern structure

Let’s see what elements are required in the above example.

The subsystem

In the above example, we first need to have interfaces corresponding to the functions of designers, plumbers and masons for the facade (decoration company) to use.

The designer

/ * * *@authorHei says Java *@ClassName Designer 
 * @DescriptionThe designer *@date2021/11/14 * * /
public class Designer {

    public void design(a) {
        System.out.println("Designer design"); }}Copy the code

Water electrician

/ * * *@authorHei says Java *@ClassName WaterElectricWorker
 * @DescriptionWater electrician *@date2021/11/14 * * /
public class WaterElectricWorker {

    public void work(a) {
        System.out.println("Plumber repairs water and electricity"); }}Copy the code

mason

/ * * *@authorHei says Java *@ClassName bricklayer
 * @DescriptionMason *@date2021/11/14 * * /
public class Bricklayer {

    public void work(a) {
        System.out.println("The mason lays the tile."); }}Copy the code

The facade

Then we set up a facade, that is, decoration company.

/ * * *@authorHei says Java *@ClassName DecorationCompany
 * @DescriptionDecoration company *@date2021/11/14 * * /
public class DecorationCompany {

    public static void decorate(a) {
        System.out.println("Decoration company integration ~");
        new Designer().design();
        new WaterElectricWorker().work();
        new Bricklayer().work();
        System.out.println("Decoration done."); }}Copy the code

In my colleague two dogs can be decorated, we have a look at two dogs using the facade and not using the facade of the different implementation.

public class DecorationTest {

    public static void main(String[] args) {
        // Do not use facade mode
        new Designer().design();
        new WaterElectricWorker().work();
        new Bricklayer().work();

        // Use facade modeDecorationCompany.decorate(); }}Copy the code

As you can see, using facade mode avoids a lot of implementation logic on the client side, and the client can use the service more easily and clearly.

Use the facade mode to follow the following characteristics.

  • The facade design pattern is more like a helper for the client application. It does not hide the subsystem interface from the client, and the use of the facade is entirely up to the client code.
  • Facade design patterns can be applied at any point in development, usually as the number of interfaces increases and the system becomes complex;
  • Subsystem interfaces do not know about the facade, and they should not have any references to the facade interface;

The pros and cons of the facade model

advantages

  • Loose coupling: The facade pattern loosens the coupling between the client and the subsystem, making it easier to extend and maintain modules within the subsystem.
  • Easy to use: Facade mode makes the subsystem easier to use. Clients no longer need to understand the implementation inside the subsystem or interact with many modules inside the subsystem, just need to interact with facade classes. : Facade mode makes the subsystem easier to use. Clients no longer need to understand the implementation inside the subsystem, nor do they need to interact with many modules inside the subsystem, just need to interact with the facade class.

disadvantages

  • Unknown risks may arise when adding subsystems;
  • The facade does not match the opening and closing principle;

The difference between facade mode and proxy mode

The proxy mode focuses on the access control of the original object.

The proxy mode implements the interface of the original object;

In proxy mode, only one interface is represented.

Facade mode focuses more on integrating subsystem resources.

Facade patterns and subsystem objects have different interface abstractions;

Facade mode represents a series of interfaces.


The above is the content of the facade mode, if it is helpful to you, the thumbs-up is the biggest encouragement to me!