Search

Useful Links

Monday, September 19, 2011

Explain Facade Design Pattern in Java


The objective of Facade pattern is to make a complex system simple. It is achieved by providing a unified or general interface, which is a higher layer to these subsystems.Facade pattern decouples subsystems, reduce its dependency, and improve portability,makes an entry point to your subsystems and hides clients from subsystem components and their implementation.

JDBC design is a good example of Façade pattern. A database design is complicated. JDBC is used to connect the database and manipulate data without exposing details to the clients.

A client is exposed to certain set of methods like just getting a single instance of database connection or closing it when not required.

Another possibility is designing security of a system with Façade pattern. Clients' authorization to access information may be classified. General users may be allowed to access general information, special guests may be allowed to access more information,administrators and executives may be allowed to access the most important information. These subsystems may be generalized by oneinterface. The identified users may be directed to the related
subsystems.

interface PublicMethod{
public void accessPublicMethod();
}

interface SpecialMethod extends PublicMethod{

public void accessSpecialMethod();

}

interface PrivateMethod extends PublicMethod {

public void accessPrivateInfo();

}

class PublicInfo implements PublicMethod{

public void accessPublicMethod() {

//...
}

//...
}

class SpecialInfo extends PublicInfo implements SpecialMethod {

public void accessSpecialMethod() {

//...
}

}

class PrivateInfo extends SpecialInfo implements PrivateMethod {

public void accessPrivateMethod () {

// ...

}

//...
}


The above code example illustrates that the whole system is not exposed to the clients. It depends on the user classification.

When a person is exposed to special information, that person is allowed to access public information also. When a person is exposed to private information, that person is allowed to access public and special information as well.

No comments:

Post a Comment