Similar to the Adapter
pattern is the Facade design pattern. These two patterns work in much the same
way, but they have different purposes. The Adapter pattern adapts code to work
with other code. But the Facade pattern gives you a wrapper that makes the
original code easier to deal with.
Facade design pattern
simplifies the interface to a complex system. For that it is usually composed
of all the classes, which make up the subsystems of the complex system.
A Facade shields the user from the complex details of the system
and provides them with a simplified view of it which is easy to
use. It also decouples the code that uses the system from the details
of the subsystems, making it easier to modify the system later.
For
example, say that someone’s designed a printer and shows it to you proudly. “How do I make it print?” you ask.
“First,” he tells you,
“call the initialize method.”
“Okay,” you say. “Now it
prints?”
“No, you have to call
the turnFanOn method.”
“Okay. Now it’ll print?”
you ask.
“Nope. Call the warmUp
method.”
“Alright. Now it prints,
right?”
“Not yet. You have to
call the getData method to get the data from the computer to print.”
“Okay, the getData
method. And next?”
“The formatData method.”
“And?”
“The checkToner method,
the checkPaperSupply method, the run
InternalDiagnostics
method, the checkPaperPath method, the . . . .”
“Hold on,” you say,
writing a facade for the whole mess. Your facade calls all
those methods behind the
scenes and dramatically simplifies the interface.
“Here you go.”
“What’s this?” the
printer designer asks.
“The print method,” you
say. “Just call the print method, and the printer
prints. No more to do.”
Example of complex System.. To

package facade.hometheater;
public class HomeTheaterFacade {
Speakers
amp;
Radio
tuner;
DvdPlayer
dvd;
CdPlayer
cd;
Projector
projector;
TheaterLights
lights;
Screen
screen;
PopcornPopper
popper;
public HomeTheaterFacade(Speakers amp,
Radio tuner,
DvdPlayer dvd,
CdPlayer cd,
Projector projector,
Screen screen,
TheaterLights lights,
PopcornPopper popper) {
this.amp = amp;
this.tuner = tuner;
this.dvd = dvd;
this.cd = cd;
this.projector = projector;
this.screen = screen;
this.lights = lights;
this.popper = popper;
}
public void watchMovie(String movie) {
System.out.println("Get
ready to watch a movie...");
popper.on();
popper.pop();
lights.dim(10);
screen.down();
projector.on();
projector.wideScreenMode();
amp.on();
amp.setDvd(dvd);
amp.setSurroundSound();
amp.setVolume(5);
dvd.on();
dvd.play(movie);
}
public void endMovie() {
System.out.println("Shutting
movie theater down...");
popper.off();
lights.on();
screen.up();
projector.off();
amp.off();
dvd.stop();
dvd.eject();
dvd.off();
}
public void listenToCd(String cdTitle) {…
}
public void endCd() {…
}
public void listenToRadio(double
frequency) {…
}
public void endRadio() {…
}
}
package facade.hometheater;
public class HomeTheaterTestDrive {
public static void
main(String[] args) {
Speakers
amp = new
Speakers("Top-O-Line Amplifier");
Radio
tuner = new Radio("Top-O-Line AM/FM Tuner", amp);
DvdPlayer
dvd = new
DvdPlayer("Top-O-Line DVD Player", amp);
CdPlayer
cd = new
CdPlayer("Top-O-Line CD Player", amp);
Projector
projector = new
Projector("Top-O-Line Projector", dvd);
TheaterLights
lights = new
TheaterLights("Theater Ceiling Lights");
Screen
screen = new Screen("Theater Screen");
PopcornPopper
popper = new
PopcornPopper("Popcorn Popper");
HomeTheaterFacade
homeTheater =
new HomeTheaterFacade(amp, tuner, dvd, cd,
projector,
screen, lights, popper);
homeTheater.watchMovie("Raiders of the Lost Ark");
homeTheater.endMovie();
}
}
package facade.hometheater;
public class PopcornPopper {
String
description;
public PopcornPopper(String description) {
this.description = description;
}
public void on() {
System.out.println(description + " on");
}
public void off() {
System.out.println(description + " off");
}
public void pop() {
System.out.println(description + " popping popcorn!");
}
public String
toString() {
return description;
}
}
package facade.hometheater;
public class Projector {
String
description;
DvdPlayer
dvdPlayer;
public Projector(String description, DvdPlayer
dvdPlayer) {
this.description = description;
this.dvdPlayer = dvdPlayer;
}
public void on() {
System.out.println(description + " on");
}
public void off() {
System.out.println(description + " off");
}
public void wideScreenMode() {
System.out.println(description + " in widescreen mode (16x9 aspect
ratio)");
}
public void tvMode() {
System.out.println(description + " in tv mode (4x3 aspect
ratio)");
}
public String
toString() {
return description;
}
}
package facade.hometheater;
public class Screen {
String
description;
public Screen(String description) {
this.description = description;
}
public void up() {
System.out.println(description + " going up");
}
public void down() {
System.out.println(description + " going down");
}
public String
toString() {
return description;
}
}
ArraList.remove
ReplyDeleteRemoves the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their indices
In WS while authenticating the user we fist authenticate LDAP , then mainframe and then by SG
ReplyDeleteclass Ldap {
ReplyDeletevoid connetLDAP() {
System.out.println("connecting LDAP");
}
void authenticate(String id, String password) {
System.out.println("authenticateing LDAP");
}
void disconnectDAP() {
System.out.println("disconnecting LDAP");
}
}
class Mainframe {
void connetMF() {
System.out.println("connecting MF");
}
void authenticate(String id, String password) {
System.out.println("authenticateing LDAP");
}
void disconnectMF() {
System.out.println("disconnecting MF");
}
}
public class AuthincateFacade {
ReplyDeleteLdap ldap;
Mainframe mainframe;
public AuthincateFacade() {
ldap = new Ldap();
mainframe = new Mainframe();
}
boolean authincateUser(String id, String password) {
ldap.connetLDAP();
ldap.authenticate(id, password);
ldap.disconnectDAP();
mainframe.connetMF();
mainframe.authenticate(id, password);
mainframe.disconnectMF();
return false;
}
public static void main(String[] args) {
AuthincateFacade authincateFacade = new AuthincateFacade();
authincateFacade.authincateUser("", "");
}
}