Dahua Design Mode Notebook Appearance Mode

Take a chestnut

Problem Description

Stockholders speculate in stocks

Simple implementation

Stock 1

/**
 * Stock 1
 * Created by callmeDevil on 2019/7/20.
 */
public class Stock1 {

    public void sell() {
        System.out.println("Stock 1 Sell");
    }

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

}

Other stocks

/**
 * Stock 2
 * Created by callmeDevil on 2019/7/20.
 */
public class Stock2 {
    // The code is similar to Stock 1
}


/**
 * Stock 3
 * Created by callmeDevil on 2019/7/20.
 */
public class Stock3 {
    // The code is similar to Stock 1
}


/**
 * National debt 1
 * Created by callmeDevil on 2019/7/20.
 */
public class NationalDebt1 {
    // The code is similar to Stock 1
}


/**
 * Real Estate 1
 * Created by callmeDevil on 2019/7/20.
 */
public class Realty1 {
    // The code is similar to Stock 1
}

test

/**
 * Stock speculation test
 * Created by callmeDevil on 2019/7/20.
 */
public class Test {

    public static void main(String[] args) {
        Stock1 stock1 = new Stock1();
        Stock2 stock2 = new Stock2();
        Stock3 stock3 = new Stock3();
        NationalDebt1 debt1 = new NationalDebt1();
        Realty1 realty1 = new Realty1();

        // Users need to understand the situation of stocks, treasury bonds and real estate, and they need to participate in the specific sale of these projects, which is highly coupled.
        stock1.buy();
        stock2.buy();
        stock3.buy();
        debt1.buy();
        realty1.buy();

        stock1.sell();
        stock2.sell();
        stock3.sell();
        debt1.sell();
        realty1.sell();
    }

}

test result

Stock 1 Buy
 Stock 2 Buy
 Stock 3 Buy
 Buying Treasury Bond 1
 Real Estate 1 Purchase
 Stock 1 Sell
 Stock 2 sold
 Stock 3 sold
 Treasury bond 1 sold
 Real Estate 1 Sell

Appearance pattern

Definition

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

UML diagrams

code implementation

Facade

/**
 * Facade
 * Created by callmeDevil on 2019/7/20.
 */
public class Fund {

    private Stock1 stock1;
    private Stock2 stock2;
    private Stock3 stock3;
    private NationalDebt1 debt1;
    private Realty1 realty1;

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

    // Buying Funds
    public void buyFund(){
        stock1.buy();
        stock2.buy();
        stock3.buy();
        debt1.buy();
        realty1.buy();
    }

    // Selling Fund
    public void sellFund(){
        stock1.sell();
        stock2.sell();
        stock3.sell();
        debt1.sell();
        realty1.sell();
    }

}

test

/**
 * Fund Testing (Appearance Model)
 * Created by callmeDevil on 2019/7/20.
 */
public class Test {

    public static void main(String[] args) {
        // At this time, users do not need to know about stocks, or even know nothing about stocks, buy funds and go home to sleep.
        // After a period of time redemption can be a lot of money, participate in the specific trading of stocks are completed by fund companies.
        // The client code is very concise and straightforward
        Fund fund = new Fund();
        // Purchase Fund
        fund.buyFund();
        // Redemption
        fund.sellFund();
    }

}

The test results are the same as above, and are omitted here.

summary

  • First of all, in the early stage of design, we should consciously separate the two different layers and establish the appearance Facade between them.
  • Secondly, in the development stage, subsystems tend to become more and more complex because of the continuous reconstruction and evolution. Increasing the appearance of Facade can provide a simple interface to reduce the dependence between them.
  • Third, when maintaining a large legacy system, it may be very difficult to maintain and expand the system. Develop a facade class for the new system to provide a relatively clear and simple interface for the design of rough or highly complex legacy code, so that the new system interacts with the Facade object, and the Facade interacts with the legacy code. All the complicated work of each other.

Keywords: PHP

Added by shane85 on Sat, 20 Jul 2019 09:40:52 +0300