Five ‘s Weblog

October 21, 2007

Facade Pattern

Filed under: Five's thought — by powerdream5 @ 9:39 pm
Tags:

       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.

10_21_1_2007.jpg

       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();
    }
}

10_21_2_2007.jpg

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: