java design patterns - facade pattern

Facade pattern

  Facade design pattern model belongs structural model, external communication with a subsystem needs to be unified through a facade, facade pattern provides a high-level interface, easy to use such that the subsystem.

 

The applicability of the facade pattern

  • Facade pattern applies when you want to provide a simple interface to a complex subsystem time. Subsystem often because of constant evolution and become more complex and difficult for the user to use the facade pattern may provide a simple call interface subsystem call, so for most users enough applied for special user demand is directly across the facade object directly access the underlying subsystems.

  • Facade mode is suitable for direct calls to the client so that both subsystems when there is a big dependency, can be greatly reduced by using mode coupling of the facade and the client subsystem, which can be improved and the independence of subsystem portability.

  • When you need to build a hierarchical system, you can use the facade pattern to define an interface for each subsystem, if these subsystems are interdependent define an interface so that they can communicate with each other.

 

Facade structure model of FIG.

 

As shown by the figure, a total of only two types of role models facade:

Role facade (Facade): The client interfaces by calling the facade role provides access to the subsystem, its facade roles and responsibilities of all functional subsystems, primarily delegated to the client's request to subsystem.

Roles Subsystem (Subsystem): the role of sub-section is the actual implementation of specific functions, roles and processes facade delegated tasks.

 

Examples facade pattern

First, we declare a subsystem module A, B, C three modules, each module perform their respective functions:

Module A:

public  class ModuleA {
     public  void Operation () { 
        System.out.println ( "A functional operation of the module" ); 
    } 
}

Module B:

public  class ModuleB {
     public  void Operation () { 
        System.out.println ( "operating function module B" ); 
    } 
}

Module C:

public  class ModuleC {
     public  void Operation () { 
        System.out.println ( "operating function module C" ); 
    } 
}

The following defines a facade object can access subsystem modules ABC three respective functions according to established ways:

public class Facade {
    public void Operation() {
        ModuleA moduleA = new ModuleA();
        ModuleB moduleB = new ModuleB();
        ModuleC moduleC = new ModuleC();
        moduleA.Operation();
        moduleB.Operation();
        moduleC.Operation();
    }
}

Define a client to call the facade interfaces provided by the object:

public class Client {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.Operation();
    }
}

结果如下:

  A functional operation of modules of the
  functional operation of block B
  operating function module C

 

  Facade pattern through each module to invoke the function subsystem, so that the client does not need to personally call subsystem A, B, C three, so that the client does not need to know the module A, B, C of specific implementation details, the client only needs to interact with objects facade facade with enough.

Advantage facade pattern

Facade of a total of the following advantages:

1, reducing the customer - the degree of coupling between subsystems.

By using facade pattern, it can be more easily extended subsystem various small modules without concern for what if the client needs to change.

2, such that the facade subsystem easier and simpler pattern

By using the facade pattern, the client does not need to be concerned about how complex subsystems, the interface is simple and requires only the facade of the role of providing interactive enough.

3, better division of the level of access

Use the facade pattern, the subsystem can provide a common interface and a private interface, common interface for part of the facade of the role of the client to use, while the private bead can be expanded internal subsystem functions, so not only facilitates the use of the client, but also a good It hides the implementation details of the subsystem.

Guess you like

Origin www.cnblogs.com/rhodesis/p/11209825.html