Design Mode (2) - Appearance Mode

Appearance Mode

    1. Definition

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

    2. Sample code

    

/*A module interface*/
public interface AModuleApi{
   public void testA ();
}

/*A module implementation */
public class AModuleImpl implements AModuleApi{
   public void testA () {
       System.out.println("Now operate the testA method in the A module");
   }
}

/*B module interface*/
public interface BModuleApi{
   public void testB();
}

/* Implementation of the B module */
public class BModuleImpl implements BModuleApi{
   public void testB(){
       System.out.println("Now operate the testB method in module B");
   }
}

/*C module interface*/
public interface CModuleApi{
   public void testC();
}

/*C module implementation */
public class CModuleImpl implements CModuleApi{
   public void testC(){
       System.out.println("Now operate the testC method in the C module");
   }
}

 

   

/*Define the appearance object*/
public class Facade{
    public void test(){
       AModuleApi a = new AModuleImpl();
       each();
       BModuleApi b = new BModuleImpl();
       b.testB();
       CModuleApi c = new CModuleImpl();
       c.testC ();
    }
}

 

   

/* Client call */
public class Client{
    public static void main(String args[]){
        new Facade().test();
    }
}

 

    3. Practical application

     The purpose of the appearance mode is not to add new functional interfaces to the subsystem, but to reduce the external interaction with multiple modules inside the subsystem and loosely couple it, so that the external can use the subsystem more easily. Facades should wrap existing functionality, and it is primarily responsible for combining existing functionality to achieve customer needs, rather than adding new implementations.

 

The essence of the facade pattern: encapsulate interactions, simplify calls

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326378344&siteId=291194637