Hello, I am Xiao Huang, a Java development engineer of a unicorn enterprise. I have received dozens of offers from our school, with an annual salary of 20W~40W. Thank you for meeting us in the vast sea of people. When your talent and ability are not enough to support your dream, please calm down and study. I hope you can study with me and work hard together to realize your dream. Welcome to pay attention to my public number: “Love to knock code yellow”

What is appearance mode

The Facade Pattern hides the complexity of the system and provides clients with an interface through which they can access the system.

This type of design pattern is structural in that it adds an interface to an existing system to hide the complexity of the system.

The facade pattern involves a single class that provides simplified methods for client requests and delegate calls to existing system class methods.

The UML diagram is as follows:

Where, Facade is the Facade role, also known as the Facade role, which can be invoked by the client and which is aware of subsystems (SubsystemClasses1, SubsystemClasses2……) Proxy client requests to a subsystem that implements the functionality.

For example, if you go to a hospital to see a doctor, you may have to go to registration, outpatient service, pricing, and taking medicine, which makes patients or their families feel very complicated. If you provide reception staff, it is very convenient to only let the reception staff deal with it.

  • Facade: Reception staff
  • Register SubsystemClasses1:
  • SubsystemClasses2: outpatient
  • SubsystemClasses3: accurately the

At this time, we just need to find a reception staff to lead us to the process of registration, outpatient service, pricing and so on. The whole process will be extremely convenient, which is also the biggest benefit of the appearance mode.

Why use appearance mode

Now, if you have 100,000 yuan in Yu ‘ebao, and you feel that the interest rate is too low, you want to get more profits, you can check online that you have 100,000 yuan in deposit, how can you maximize the profit?

At this point, one thing might catch your eye: stocks

Of course, most of us don’t know anything about stocks, we don’t know anything about finance, we don’t know how to analyze the future of the company, but what if I just want to participate?

There’s something called a fund

A fund is someone else buying stocks for you, so people skills are key, you have to find a person or team who can choose good stocks for you to invest in.

So we give our money to a fund to buy better stocks on our behalf.

We can find that if we choose stocks ourselves, each stock can take a great deal of time

If we go to buy funds, we can pick a reliable fund, through this fund to buy some stocks, at this time we don’t need to care about these stocks, just need to find the fund.

Similar to our appearance model, we only need to find the appearance role of the fund, so that the fund can execute the subsystems of stocks, bonds and foreign exchange, and finally become a winner in life.

Let’s compare the differences between the two approaches:

  • The appearance model

  • Normal mode

Three, the realization of the appearance mode

The overall structure is shown as follows: Fund:

public class Fund {
    Stock1 stock1;
    Stock2 stock2;
    Stock3 stock3;
    NationalDebt1 nationalDebt1;
    Realty1 realty1;

    public Fund(a) {
        stock1 = new Stock1();
        stock2 = new Stock2();
        stock3 = new Stock3();
        nationalDebt1 = new NationalDebt1();
        realty1 = new Realty1();
    }

    public void buyFund(a) {
        stock1.buy();
        stock2.buy();
        stock3.buy();
        nationalDebt1.buy();
        realty1.buy();
    }

    public void sellFund(a) { stock1.sell(); stock2.sell(); stock3.sell(); nationalDebt1.sell(); realty1.sell(); }}Copy the code

Stock1:

public class Stock1 {


    public void buy(a) {
        System.out.println("Stock 1 buy");
    }


    public void sell(a) {
        System.out.println("Stock 1 sold."); }}Copy the code

TestMain:

public class TestMain {
    public static void main(String[] args) throws InterruptedException {
        Fund fund = new Fund();
        // Buy funds
        fund.buyFund();

        // Wait a while
        System.out.println("Waiting......");
        Thread.sleep(200);


        // Sell funds
        fund.sellFund();

		// Buy stock 1
        // Stock 2 bought
        // Stock 3 buy
        // Bond 1 bought
        // Real estate 1 buy
        // Waiting......
        // Stock 1 is sold
        // Stock 2 is sold
        // Stock 3 is sold
        // Bond 1 is sold
        // Real estate 1 sold}}Copy the code

Four,

We can see that the unified purchase and sale of stocks, bonds and real estate can be realized by directly calling buyFund() and sellFund() of fund

We only need Fund, the receptionist, to get more profits

Overall, the appearance mode is not complex, multiple applications on the other side only need one interface, do not need to know your internal operation logic

For example, when we communicate with other business parties, they may need to call one of your interfaces. At this time, we expose this interface to them, but the internal logic is encapsulated in us to prevent business leakage.

The content of this issue is over here, I am a Unicorn enterprise Java development engineer, you can leave a message or private message to add my wechat, we will see you next time!