Wikipedia says:
Facade is an object which provides a simplified interface to a larger body of code,such as a class library.A facade as the name suggests means the face of the building.People walking past the road only see this glass face of the building.The face as known hides the complexity of the building and displays the friendly face.
Intent:->
-> Provide a unified interface to a set of interfaces in a subsystem.Facade defines an higher level interface that makes the subsystem easier to use.(Gof,p185) This can be used to simplify a number of complicated object interactions into a single interface.
-> Wrap a complicated subsystem with a simpler interface.
Problem:->
A segment of the client community needs a simplified interface to an overall functionality of a complex sub system.
Advantages:-
-> Make a software library easier to use and understand,since the facade has convenient methods for common tasks.
-> Make code that uses more readable.
-> Reduce dependencies of outside code on the inner workings of a library,since most code uses the facade thus allowing more flexibility in developing the system.
-> Wrap a poorly designed collection of api's with a single well-designed API.
-> Reduces learning curve necessary to successfully leverage the subsystem.
-> Reduces compiler dependencies in large software systems.
-> Note that Facade does not add any functionality it just simplifies interfaces.
DisAdvantages:->
-> If facade is the only access point for the subsystem,it will limit the features and flexibility that "power users" may need.But this also depends on how facade is designed.
Example:
The Facade defines a unified,higher level interface to a subsystem that makes it easier to use.Customers encounter a facade when ordering from a catalog.Customer calls one number and speaks with a customer service representative.Customer Service Representative acts as a Facade,providing an interface to the order fulfillment department, the billing department, and the shipping department. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]
Just for thought:
Compilers that we use everyday to process the computer code that we have written is a prime example of the facade pattern in action.Do you think any other examples in java? Can we think in general sense that telnet,ftp,web browser's also are acting as facades?
Rules of thumb
Facade defines a new interface,where as adapter uses an old interface.Remember that adapter makes two existing interfaces work together as opposed to defining an new one. [GoF, p219]
Whereas Flyweight shows how to make lot of little objects,Facade shows how to make a single object represent an entire subsystem. [GoF, p138]
Mediator is similar to Facade in that it abstracts functionality of existing classes.Mediator abstracts/centralizes arbitary communications between colleague objects.it routinely "adds value" and it is known/referenced by the colleague objects.In contrast,facade defines a simpler interface to a subsystem,it doesnot add new functionality and it is not known by subsystem classes. [GoF. p193]
Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.[GoF,p193]
Facade objects are often singletons because only one Facade object is required. [GoF,p193]
Adapter and Facade are both wrappers,but they are different kinds of wrappers.The intent of Facade is to produce a simpler interface,and the intent of Adapter is to design to an existing interface.While facade routinely wraps multiple objects,and adapter wraps a single object.Facade could front-end a single complex object and Adapter could wrap several legacy objects[Design Patterns Explained, p105]
The difference between the two is not in terms of how many classes they "wrap", it is in their intent. [Head First Design Patterns, p260]
However,An adapter is used when our wrapper must respect a particular interface and must support a polymorphic behavior.On the other hand a facade is used when one wants an easier/simpler interface to work with.
Facades are often implemented with containers,but the pattern is a specific sort of containership.Most containers donot expose the interfaces of contained objects to the world.Facades on the other hand exposes the interface of contained objects to the world and the point of it is that this way clients don't have to be concerned with exactly which object in a subsystem they're dealing with.They just call methods on the facade in blissful ignorance.
A facade is a semantic wrapper of existing objects,an extended form of Adapter Pattern.So you make your objects and attach the facade.The binding can occur at any time you like,not just at facade construction time. - Richard Henderson.
Session Facade is to simplify use of services,i.e
1) Coarse Grained Access of services available.If we have a service class and we want to access methods available in it,we have to use session facade to call those methods.
2) Hides complex implementation: How internal implementation is,used won't be aware of it.
3) Reduces Network calls: It takes remote calls once for a method of facade,and facade makes required calls to other methods locally and returns result.
4) Wrapper Around Service: If our service class is a plain java class,then using Stateless Session Bean will avail benefits of EJB container,transaction,security,persistance etc.
Finally let us see a small example of implementation of Facade Pattern.
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;
class Employee implements Serializable {
String firstName;
Date dob;
//......
/*
* For now our criteria is only comparing employees firstname,DOB....
*/
@Override
public boolean equals(Object rhs) {
if(rhs==null)
return false;
if(rhs instanceof Employee==false)
return false;
Employee otherEmployee=(Employee)rhs;
if(this==otherEmployee)
return true;
boolean result = new EqualsBuilder()
.append(getDob(),otherEmployee.getDob())
.append(getFirstName().toUpperCase(), otherEmployee.getFirstName().toUpperCase())
.isEquals();
return result;
}
@Override
public int hashCode() {
return this.firstName.hashCode();
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
class EmployeeHelper {
private List employeeList;
/*
* Acting as a simple facade hiding all the underlying details...
*/
public void addEmployee(Employee employee) {
// check other conditions before inserting...
// check if employee already exists before inserting through equals() method...
// creating hibernate Employee object and save it...
employeeList.add(employee);
}
}
Hope this explanation helps if any.
No comments:
Post a Comment