Facade Design Pattern
Facade design pattern is used when there is a set of interfaces to be used for the client. What we can do is create a single interface, that incorporates all the capabilities of all the interfaces, for the client. Using this interface, the client can interact with the system. This single interface works as a facade for the entire system. It reduces the system's complexity, making it easier for the client.
BoxOffice interface
package com.sks.facade;
public interface BoxOfficeI {
public void bookTickets();
}
FoodCounter interface
package com.sks.facade;
public interface FoodCounterI {
public void buyFood();
}
ToysCounter interface
package com.sks.facade;
public interface ToysCounterI {
void buyToys();
}
BoxOffice implementation
package com.sks.facade;
public class BoxOffice implements BoxOfficeI {
public void bookTickets() {
System.out.println("Your tickets have been booked.");
}
}
FoodCounter implementation
package com.sks.facade;
public class FoodCounter implements FoodCounterI {
public void buyFood() {
System.out.println("Food purchase successful. Please pay at the counter.");
}
}
ToysCounter implementation
package com.sks.facade;
public class ToysCounter implements ToysCounterI {
public void buyToys() {
System.out.println("Toy purchase successful. Please pay.");
}
}
Facade interface
package com.sks.facade;
public interface BookMyMovieKioskI {
public void justWatchAMovie();
public void enjoySomeFood();
public void buySomeToys();
}
Facade implmentation
package com.sks.facade;
public class BookMyMovieKiosk {
private BoxOfficeI boxOffice;
private FoodCounterI foodCounter;
private ToysCounterI toysCounter;
public void justWatchAMovie() {
boxOffice.bookTickets();
}
public void enjoyMovieWithSomeFood() {
boxOffice.bookTickets();
foodCounter.buyFood();
}
public void enjoyMovieWithFoodAndbuyToys() {
enjoyMovieWithSomeFood();
toysCounter.buyToys();
}
public BookMyMovieKiosk(BoxOfficeI boxOffice, FoodCounterI foodCounter, ToysCounterI toysCounter) {
super();
this.boxOffice = boxOffice;
this.foodCounter = foodCounter;
this.toysCounter = toysCounter;
}
}
Facade pattern test class
package com.sks.facade;
public class BookMyMovieKiosk {
private BoxOfficeI boxOffice;
private FoodCounterI foodCounter;
private ToysCounterI toysCounter;
public void justWatchAMovie() {
boxOffice.bookTickets();
}
public void enjoyMovieWithSomeFood() {
boxOffice.bookTickets();
foodCounter.buyFood();
}
public void enjoyMovieWithFoodAndbuyToys() {
enjoyMovieWithSomeFood();
toysCounter.buyToys();
}
public BookMyMovieKiosk(BoxOfficeI boxOffice, FoodCounterI foodCounter, ToysCounterI toysCounter) {
super();
this.boxOffice = boxOffice;
this.foodCounter = foodCounter;
this.toysCounter = toysCounter;
}
}
Output :