当前位置:网站首页>Design pattern of facade and mediator

Design pattern of facade and mediator

2020-11-07 20:37:00 Stack

Mediator and facade design pattern

Today we're going to talk about two design patterns : Mediator design pattern and facade design pattern , Why put them together to explain , Because they are so much alike , It's just a different name because of the different place of action .

We use one for us 90 Let's start with the most difficult question . Suppose we're tired of city life , Looking for a quiet place to settle down , pig , By the way, write a pig face recognition to manage these pigs ( Life in dream ). And in the premise of doing these things , We have to build a house and a pigsty .

Let's take a look at what we used to do .

It takes workers to build a house , The brick , Cement and so on , We need someone to connect with . But I'm so smart that I wouldn't do it , So I got someone to help me with these things , So it's a process like this .

I found a contractor , The agent helps me with these things . This process is a complete facade pattern . Does it feel like a little bit of a proxy model , Help me with things . In fact, the design pattern will reach the same goal in the end , As the saying goes , Taiji Sword technique , Learn how much and forget how much , The last thing I remember is Tai Chi sword .

Let's go back to programming , Let's take a look at the implementation pattern of the facade

So let's take a look at the downloader design pattern .

The biggest difference between it and the facade mode is that the facade mode is blocked in the outer layer , And it's in the middle of all services . Let's take a look at the schematic .

Let's take a look at its class diagram implementation

Did you find out , The class diagram implementations of the two patterns are almost the same , So their code implementation is almost the same .

After understanding their principles , Let's talk about their application in real code .

Facade mode : Gateway for server deployment , Block all requests , The specific method of forwarding is decided by the gateway

Mediator mode : Coordination middleware , Microservices register all services to something similar to zookeeper In the coordination middleware of , Accessing other services through middleware ; Message middleware , What message is needed to get through message middleware .

For some older projects , The scheduling center of facade mode and mediator mode is likely to be a , Such as through Nginx management service .

Let's take a look at the specific code implementation .

Facade pattern code implementation ( Role composition )

  • Subsystem
class Cement{
    void cement(){
        System.out.println(" cement ");
    }
}

class Worker{
    void worker(){
        System.out.println(" Worker ");
    }
}

class Brick{
    void brick(){
        System.out.println(" The brick ");
    }
}
  • The facade
class Contractor{
    private Cement cement = new Cement();
    private Worker worker = new Worker();
    private Brick brick = new Brick();

    void cement(){
        cement.cement();
    }

    void worker(){
        worker.worker();
    }

    void brick(){
        brick.brick();
    }
}

The code implementation of mediator mode is almost the same as facade mode . Both were born out of coping and different positions , In essence .

More original articles, please pay attention to the official account @MakerStack

版权声明
本文为[Stack]所创,转载请带上原文链接,感谢