Core Java Design Pattern : Structural Pattern : Facade Design Pattern
Facade Design Pattern |
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Use the Facade pattern when
- you want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve.
- there are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.
- you want to layer your subsystems. Use a facade to define an entry point to each subsystem level
Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. One way to achieve this goal is to introduce a facade object that provides a single, simplified interface to the more general facilities of a subsystem.
Examples |
RoomType.java
package com.javaskool.model;
public enum RoomType {
SINGLE, DOUBLE;
}
Room.java
package com.javaskool.model;
public class Room {
private int rno;
private RoomType type;
private boolean vacant;
public int getRno() {
return rno;
}
public void setRno(int rno) {
this.rno = rno;
}
public RoomType getType() {
return type;
}
public void setType(RoomType type) {
this.type = type;
}
public boolean isVacant() {
return vacant;
}
public void setVacant(boolean vacant) {
this.vacant = vacant;
}
public Room(int rno, RoomType type, boolean vacant) {
super();
this.rno = rno;
this.type = type;
this.vacant = vacant;
}
public Room() {
super();
}
@Override
public String toString() {
return rno+" "+type+" "+vacant;
}
}
HotelSearchService.java
package com.javaskool.model;
import java.util.LinkedList;
import java.util.List;
public class HotelSearchService {
private List<Room> rooms;
public HotelSearchService() {
// TODO Auto-generated constructor stub
rooms=new LinkedList<Room>();
}
public void addRoom(Room room){
rooms.add(room);
}
public List<Room> search(RoomType rt){
List<Room> searchList=new LinkedList<Room>();
for(Room r:rooms)
if(r.isVacant() && r.getType().equals(rt))
searchList.add(r);
return searchList;
}
public void reserve(int rno){
for(Room r:rooms){
if(r.getRno()==rno)
{
r.setVacant(false);
return;
}
}
}
}
HotelFacade.java
package com.javaskool.facade;
import java.util.List;
import com.javaskool.model.HotelSearchService;
import com.javaskool.model.Room;
import com.javaskool.model.RoomType;
public class HotelFacade {
HotelSearchService service;
public HotelFacade() {
super();
service=new HotelSearchService();
for(int i=0;i<20;i++)
if(i%2==0)
service.addRoom(new Room(i+1,RoomType.SINGLE,true));
else
service.addRoom(new Room(i+1,RoomType.DOUBLE,true));
}
public String reserve(RoomType rt){//choice from reception
List<Room>r=service.search(rt);
if(r.size()>0)
{
service.reserve(r.get(0).getRno());
return "success";
}
else
return "failure";
}
public String reserve(RoomType rt, int i){//choice from customer
List<Room>r=service.search(rt);
if(r.size()>0)
{
service.reserve(i);//r.get(0).getRno());
return "success";
}
else
return "failure";
}
public void displayAvailableRooms(){
System.out.println(service.search(RoomType.SINGLE));
System.out.println(service.search(RoomType.DOUBLE));
}
}
TestDrive.java
package com.javaskool;
import java.util.Scanner;
import com.facade.HotelFacade;
import com.model.RoomType;
public class TestDrive {
public static void main(String arg[]){
HotelFacade facade=new HotelFacade();
facade.displayAvailableRooms();
//Automatically from facade
System.out.println(facade.reserve(RoomType.DOUBLE));
//For Customer Choice
/*Scanner sc=new Scanner(System.in);
System.out.println("Enter Room No :");
int i=sc.nextInt();
System.out.println(facade.reserve(RoomType.SINGLE,i));*/
//System.out.println(facade.reserve(RoomType.DOUBLE));
System.out.println(facade.reserve(RoomType.SINGLE));
facade.displayAvailableRooms();
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
[1 SINGLE true, 3 SINGLE true, 5 SINGLE true, 7 SINGLE true, 9 SINGLE true, 11 SINGLE true, 13 SINGLE true, 15 SINGLE true, 17 SINGLE true, 19 SINGLE true]
[2 DOUBLE true, 4 DOUBLE true, 6 DOUBLE true, 8 DOUBLE true, 10 DOUBLE true, 12 DOUBLE true, 14 DOUBLE true, 16 DOUBLE true, 18 DOUBLE true, 20 DOUBLE true]
success
success
[3 SINGLE true, 5 SINGLE true, 7 SINGLE true, 9 SINGLE true, 11 SINGLE true, 13 SINGLE true, 15 SINGLE true, 17 SINGLE true, 19 SINGLE true]
[4 DOUBLE true, 6 DOUBLE true, 8 DOUBLE true, 10 DOUBLE true, 12 DOUBLE true, 14 DOUBLE true, 16 DOUBLE true, 18 DOUBLE true, 20 DOUBLE true]
Recent Comments