In EJB, we recommend to use session facade pattern to let the present layer access the business layer. But in lightweight framework, we use POJO facade instead of session facade. There is no doubt that POJO facade is different from session facade. However, the common characteristic between them is that both session facade and POJO facade are based on the facade pattern. So today I am going to make a brief introduction to facade pattern according to my own understanding.
To be honest, compared with the other design patterns, facade pattern is easier to understand in my opinions. The general purpose of using facade pattern is to reduce the complexity of the system. The basic concept of facade pattern is to provide a simple interface to a complex subsystem, and the clients of the subsystem must access it through this interface, thereby promoting subsystem independence and portability.
People prefer to taking the car as an example to explain the facade pattern. If we compare the car to the system, the driver will be the client of the system and the steering wheel will be the facade interface. In order to drive the car, the driver just need to interact with the sterring wheel. He doesn’t need to knwo how the engine works and how the tires turn.
//the subsystem
public class Engine{
public void works(){…}
}
public class Tire{
public void turn(){…}
}
//the interface
public class SterringWheel{
private Engine engine;
private Tire tire;
//setter injection
public void set(Engine engine){
this.engine = engine;
}
public void set(Tire tire){
this.tire = tire;
}
//encapsulate the detail of the subsystem
public void driven(){
engine.works();
tire.turn();
}
}
//the client
public class Driver{
private SterringWheel sterringWheel;
public void setSterringWheel(SterringWheel sterringWheel)
{
this.sterringWheel = sterringWheel;
}
public void drive(){
SterringWheel.driven();
}
}