Study and summary of design mode appearance mode of Dahua

1, Concept

Appearance mode provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use.

2, Class diagram and basic code

//Four subsystem classes
class SubSystemOne{
    public void MethodOne(){
        System.out.println("Subsystem method 1");
    }
}
class SubSystemTwo{
    public void MethodTwo(){
        System.out.println("Subsystem method 2");
    }
}

class SubSystemThree{
    public void MethodThree(){
        System.out.println("Subsystem method 3");
    }
}

class SubSystemFour{
    public void MethodFour(){
        System.out.println("Subsystem method 4");
    }
}
//Appearance class
public class Facade {
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;

    public Facade(){
        one=new SubSystemOne();
        two=new SubSystemTwo();
        three=new SubSystemThree();
        four=new SubSystemFour();
    }

    public void MethodA(){
        System.out.println("Method group a");
        one.MethodOne();
        two.MethodTwo();
        three.MethodThree();
    }

    public void MethodB(){
        System.out.println("Method group b");
        three.MethodThree();
        four.MethodFour();
    }


}
//client
public static void main(String[] args) {
    Facade facade=new Facade();//The client only interacts with the appearance class and does not know the four subsystems
    facade.MethodA();
}

3, Example of stock speculators

(1) . appearance mode not used

//Stocks, real estate, treasury bonds

/*
 * Stock
 */
public class Stock1 {
    public void Sell() {
        System.out.println("Sale of share 1");
    }

    public void Buy() {
        System.out.println("Purchase of share 1");
    }
}

class Stock2 {
    public void Sell() {
        System.out.println("Sale of share 2");
    }

    public void Buy() {
        System.out.println("Purchase of stock 2");
    }

}


class Stock3 {
    public void Sell() {
        System.out.println("Sale of share 3");
    }

    public void Buy() {
        System.out.println("Purchase of share 3");
    }
}

class NationalDebt1{
    public void Sell() {
        System.out.println("Sales of national debt 1");
    }

    public void Buy() {
        System.out.println("Treasury bond 1 purchase");
    }

}

class Realty1{
    public void Sell() {
        System.out.println("Sales of real estate 1");
    }

    public void Buy() {
        System.out.println("Purchase of real estate 1");
    }

}
//client

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

        stock1.Buy();
        stock2.Buy();
        stock3.Buy();
        nationalDebt1.Buy();
        realty1.Buy();

        stock1.Sell();
        stock2.Sell();
        stock3.Sell();
        nationalDebt1.Sell();
        realty1.Sell();
    }

}

(2) . use appearance mode

//Funds
/*
 * Funds
 */
public class Fund {
    Stock1 stock1;
    Stock2 stock2;
    Stock3 stock3;
    NationalDebt1 nationalDebt1;
    Realty1 realty1;
    public Fund(){
        stock1=new Stock1();
        stock2=new Stock2();
        stock3=new Stock3();
        nationalDebt1=new NationalDebt1();
        realty1=new Realty1();
    }

    public void BuyFund(){
        stock1.Buy();
        stock2.Buy();
        stock3.Buy();
        nationalDebt1.Buy();
        realty1.Buy();
    }

    public void SellFund(){
        stock1.Sell();
        stock2.Sell();
        stock3.Sell();
        nationalDebt1.Sell();
        realty1.Sell();

    }

}
//Stocks, real estate, treasury bonds
public class Stock1 {
    public void Sell() {
        System.out.println("Sale of share 1");
    }

    public void Buy() {
        System.out.println("Purchase of share 1");
    }
}

class Stock2 {
    public void Sell() {
        System.out.println("Sale of share 2");
    }

    public void Buy() {
        System.out.println("Purchase of stock 2");
    }

}


class Stock3 {
    public void Sell() {
        System.out.println("Sale of share 3");
    }

    public void Buy() {
        System.out.println("Purchase of share 3");
    }
}

class NationalDebt1{
    public void Sell() {
        System.out.println("Sales of national debt 1");
    }

    public void Buy() {
        System.out.println("Treasury bond 1 purchase");
    }

}

class Realty1{
    public void Sell() {
        System.out.println("Sales of real estate 1");
    }

    public void Buy() {
        System.out.println("Purchase of real estate 1");
    }

}
//Client: reduce the coupling between shareholders and stocks, real estate, and national debt
public class Show {
    public static void main(String[] args) {
        Fund fund=new Fund();
        fund.BuyFund();
        fund.SellFund();
    }

}

4, Summary

Through appearance pattern, a simple interface can be provided to reduce the coupling between customer class and subsystem class.
1. In the early stage of design, the system is layered and a simple interface is provided for the complex subsystem through the appearance mode.
2. In the initial stage, the difficulty of using the external calling subsystem is reduced.
3. For the complex old system, there may be problems in the direct modification or expansion. At this time, a appearance class is needed. This class is responsible for the interaction with the old system and provides a simpler interface. When developing a new system, the interface can be called directly.

No matter how familiar we used to be with each other,as long as we are apart then we become strangers

Posted on Sun, 03 May 2020 02:29:35 -0700 by mwichmann4