Exterior design pattern mode (the Facade) and code examples Detailed

Definition of a, the appearance model

  Define the appearance (the Facade) mode: Mode Facade known, is a through providing a consistent interface to multiple complex subsystems , subsystems which the pattern to be accessed more easily. The model has a unified interface to the external, external applications do not care about the specific details of the internal subsystems, it would greatly reduce the complexity of the application, improve the maintainability of the program.

Second, the advantages and disadvantages of model appearance

  advantage:

  • Simplify the calling process, without having to know in-depth subsystem, prevent risks
  • Reduction systems rely, loosely coupled
  • Better access division level
  • In line with the Law of Demeter, namely the principle of least know

  Disadvantages:

  • Increase subsystem, expansion subsystem easy to introduce risk behavior
  • Does not comply with the principle of opening and closing

Third, to achieve the appearance mode

  Appearance of the structure (Facade) model is relatively simple, is the definition of a high-level interface. It contains a reference to the various subsystems, the client can access it through the various subsystems. Now to analyze the basic structure and implementation.

  Appearance (Facade) model consists of the following major role.

  • Appearance (Facade) role: to provide a common multiple of external interface subsystem.
  • Subsystem (Sub System) role: to achieve part of the system functions, customers can access it through the appearance of the characters.
  • Customer (Client) Role: access by various subsystems of the appearance of a role.

  The structure shown in Figure:

              

  Codes are common in both cases belong to the appearance model, the following code:

  All subsystems to achieve a unified interface:

public  interface the System {
     public  void doSomething (); 
} 

public  class SubSystemA the implements the System { 

    public  void doSomething () { 
        System.out.println ( "Subsystem Method A" ); 
    } 
} 

public  class SubSystemB the implements the System { 

    public  void doSomething () { 
        System.out.println ( "subsystem method B" ); 
    } 
} 

public  class the Facade { 

    // delegated object of 
    the System a, B; 
    
    public Facade() {
        a = new SubSystemA();
        b = new SubSystemB();
    }
    
    //提供给外部访问的方法
    public void methodA() {
        this.a.dosomething();
    }
    
    public void methodB() {
        this.b.dosomething();
    }
}

public class Client {

    public static void main(String[] args) {
        Facade facade = new Facade();
        
        facade.methodA();
        facade.methodB();
    }
}

  Not all subsystems to achieve a unified interface:

public  class SubSystemA { 

    public  void dosomethingA () { 
        System.out.println ( "Subsystem Method A" ); 
    } 
} 

public  class SubSystemB { 

    public  void dosomethingB () { 
        System.out.println ( "Subsystem Method B" ); 
    } 
} 

public  class the Facade { 

    // delegated object 
    SubSystemA a; 
    SubSystemB B; 
    
    public the Facade () { 
        a = new new SubSystemA (); 
        B = new new SubSystemB (); 
    }
    
    //提供给外部访问的方法
    public void methodA() {
        this.a.dosomethingA();
    }
    
    public void methodB() {
        this.b.dosomethingB();
    }
}

public class Client {

    public static void main(String[] args) {
        Facade facade = new Facade();
        
        facade.methodA();
        facade.methodB();
    }
}

  Test results are as follows:

A method of subsystem 
subsystem Method B

Application Scene Four appearance mode

  Generally can be considered in the following using the appearance model.

  • Subsystem more complex, increasing the appearance of the model provides a simple interface calls
  • Construction of multi-layered system architecture, using the appearance of each object as an inlet, a simplified inter-layer calls

Extended V. appearance mode

  In appearance mode, when adding or removing the need to modify the appearance-based subsystem, which is contrary to the "on-off principle." If the introduction of the abstract class appearance, it is to some extent solve the problem, the structure shown in FIG. FIG:
           

  code show as below:

public class FacadePattern
{
    public static void main(String[] args)
    {
        Facade f=new FacadeImpl1();
        f.method();
        f=new FacadeImpl2();
        f.method();
    }
}

interface Facade {
    public void method();
}

//外观角色
class FacadeImpl1 implements Facede
{
    private SubSystem01 obj1=new SubSystem01();
    private SubSystem02 obj2=new SubSystem02();
    private SubSystem03 obj3=new SubSystem03();
    public void method()
    {
        obj1.method1();
        obj2.method2();
        obj3.method3();
    }
}
//外观角色
class FacadeImpl1 implements Facede
{
    private SubSystem02 obj2=new SubSystem02();
    private SubSystem03 obj3=new SubSystem03();
    private SubSystem04 obj4=new SubSystem04();
    public void Method () 
    { 
        obj2.method2 (); 
        obj3.method3 (); 
        obj4.method4 (); 
    } 
} 

// subsystem character 
class SubSystem01 
{ 
    public   void the method1 () 
    { 
        System.out.println ( "Subsystem 01 method1 () is called! " ); 
    }    
} 
// subsystems role 
class SubSystem02 
{ 
    public   void method2 () 
    { 
        System.out.println ( " sub-02 method2 () is called! " ); 
    }    
} 
// sub system role 
classSubSystem03 
{ 
    public   void method3 () 
    { 
        System.out.println ( "sub-03 method3 () is called!" ); 
    }    
} 
// subsystems role 
class SubSystem04 
{ 
    public   void method4 () 
    { 
        System.out.println ( "subsystem 04 method4 () is called!" ); 
    }    
}

Guess you like

Origin www.cnblogs.com/jing99/p/12602696.html