Skip to content

Commit 8af5ef4

Browse files
authored
Merge pull request #6 from ahucoder/dev
The Facade Pattern
2 parents fec39d8 + 47731e4 commit 8af5ef4

11 files changed

+239
-0
lines changed

src/main/java/facade/App.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package facade;
2+
3+
import facade.dto.TripDetailsDto;
4+
import facade.facade.TravelFacade;
5+
6+
import java.time.LocalDate;
7+
8+
public class App {
9+
public static void main(String[] args) {
10+
TripDetailsDto trip = new TripDetailsDto.Builder()
11+
.customerName("Alice")
12+
.origin("Beijing")
13+
.destination("Shanghai")
14+
.departureDate(LocalDate.of(2025, 6, 7))
15+
.returnDate(LocalDate.of(2025, 6, 8))
16+
.build();
17+
18+
TravelFacade facade = new TravelFacade.Builder()
19+
.trip(trip)
20+
.build();
21+
22+
facade.bookAll();
23+
}
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package facade.dto;
2+
3+
import lombok.Getter;
4+
5+
import java.time.LocalDate;
6+
7+
@Getter
8+
public class TripDetailsDto {
9+
private final String passengerName;
10+
private final LocalDate departureDate;
11+
private final LocalDate returnDate;
12+
private final String origin;
13+
private final String destination;
14+
15+
private TripDetailsDto(Builder builder) {
16+
this.passengerName = builder.customerName;
17+
this.departureDate = builder.departureDate;
18+
this.returnDate = builder.returnDate;
19+
this.origin = builder.origin;
20+
this.destination = builder.destination;
21+
}
22+
23+
public static class Builder {
24+
private String customerName;
25+
private LocalDate departureDate;
26+
private LocalDate returnDate;
27+
private String origin;
28+
private String destination;
29+
30+
public Builder customerName(String name) {
31+
this.customerName = name;
32+
return this;
33+
}
34+
public Builder departureDate(LocalDate date) {
35+
this.departureDate = date;
36+
return this;
37+
}
38+
public Builder returnDate(LocalDate date) {
39+
this.returnDate = date;
40+
return this;
41+
}
42+
public Builder origin(String origin) {
43+
this.origin = origin;
44+
return this;
45+
}
46+
public Builder destination(String dest) {
47+
this.destination = dest;
48+
return this;
49+
}
50+
public TripDetailsDto build() {
51+
if (departureDate.isAfter(returnDate)) {
52+
throw new IllegalArgumentException("Departure must be before return date");
53+
}
54+
return new TripDetailsDto(this);
55+
}
56+
}
57+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package facade.facade;
2+
3+
import facade.dto.TripDetailsDto;
4+
import facade.service.FlightBookingService;
5+
import facade.service.HotelBookingService;
6+
import facade.service.PaymentService;
7+
import facade.service.TaxiBookingService;
8+
import facade.service.impl.FlightBookingServiceImpl;
9+
import facade.service.impl.HotelBookingServiceImpl;
10+
import facade.service.impl.PaymentServiceImpl;
11+
import facade.service.impl.TaxiBookingServiceImpl;
12+
13+
public class TravelFacade {
14+
private final FlightBookingService flightService;
15+
private final HotelBookingService hotelService;
16+
private final TaxiBookingService taxiService;
17+
private final PaymentService paymentService;
18+
private final TripDetailsDto trip;
19+
private double totalCost;
20+
21+
private TravelFacade(Builder builder) {
22+
this.flightService = new FlightBookingServiceImpl();
23+
this.hotelService = new HotelBookingServiceImpl();
24+
this.taxiService = new TaxiBookingServiceImpl();
25+
this.paymentService = new PaymentServiceImpl();
26+
this.trip = builder.trip;
27+
}
28+
29+
public static class Builder {
30+
private TripDetailsDto trip;
31+
32+
public Builder trip(TripDetailsDto trip) {
33+
this.trip = trip;
34+
return this;
35+
}
36+
37+
public TravelFacade build() {
38+
if (trip == null) {
39+
throw new IllegalStateException("TripDetails must be provided");
40+
}
41+
return new TravelFacade(this);
42+
}
43+
}
44+
45+
public void bookAll() {
46+
System.out.println("Start the itinerary booking process...");
47+
// 1. Flight
48+
String flightConf = flightService.bookFlight(
49+
trip.getPassengerName(), trip.getOrigin(), trip.getDestination(), trip.getDepartureDate());
50+
totalCost += 500.0;
51+
52+
// 2. Hotel
53+
String hotelConf = hotelService.bookHotel(
54+
trip.getPassengerName(), trip.getDestination(), trip.getDepartureDate(), trip.getReturnDate());
55+
totalCost += 300.0;
56+
57+
// 3. Taxi
58+
String taxiConf = taxiService.bookCar(
59+
trip.getPassengerName(), trip.getDestination(), trip.getDepartureDate(), trip.getReturnDate());
60+
totalCost += 200.0;
61+
62+
// 4. Pay
63+
boolean paid = paymentService.processPayment(trip.getPassengerName(), totalCost);
64+
if (!paid) {
65+
throw new RuntimeException("Payment failed, booking cancelled");
66+
}
67+
68+
System.out.println("Booking completed! The following is the confirmation information:");
69+
System.out.println("Flight info: " + flightConf);
70+
System.out.println("Hotel info: " + hotelConf);
71+
System.out.println("Taxi info: " + taxiConf);
72+
System.out.printf("Total cost: %.2f%n", totalCost);
73+
}
74+
75+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package facade.service;
2+
3+
import java.time.LocalDate;
4+
5+
public interface FlightBookingService {
6+
String bookFlight(String passenger, String from, String to, LocalDate date);
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package facade.service;
2+
3+
import java.time.LocalDate;
4+
5+
public interface HotelBookingService {
6+
String bookHotel(String passenger, String city, LocalDate from, LocalDate to);
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package facade.service;
2+
3+
public interface PaymentService {
4+
boolean processPayment(String user, double amount);
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package facade.service;
2+
3+
import java.time.LocalDate;
4+
5+
public interface TaxiBookingService {
6+
String bookCar(String passenger, String city, LocalDate from, LocalDate to);
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package facade.service.impl;
2+
3+
import facade.service.FlightBookingService;
4+
5+
import java.time.LocalDate;
6+
import java.util.UUID;
7+
8+
public class FlightBookingServiceImpl implements FlightBookingService {
9+
@Override
10+
public String bookFlight(String passenger, String from, String to, LocalDate date) {
11+
String flightInfo = STR."FLIGHT-\{UUID.randomUUID()}";
12+
System.out.printf("[%s] booking %s->%s flight for %s, date: %s%n", flightInfo, from, to, passenger, date);
13+
return flightInfo;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package facade.service.impl;
2+
3+
import facade.service.HotelBookingService;
4+
5+
import java.time.LocalDate;
6+
import java.util.UUID;
7+
8+
public class HotelBookingServiceImpl implements HotelBookingService {
9+
@Override
10+
public String bookHotel(String passenger, String city, LocalDate from, LocalDate to) {
11+
String hotelInfo = STR."HOTEL-\{UUID.randomUUID()}";
12+
System.out.printf("[%s] booking hotel in %s for %s, checkin: %s, checkout: %s%n",
13+
hotelInfo, city, passenger, from, to);
14+
return hotelInfo;
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package facade.service.impl;
2+
3+
import facade.service.PaymentService;
4+
5+
public class PaymentServiceImpl implements PaymentService {
6+
@Override
7+
public boolean processPayment(String user, double amount) {
8+
System.out.printf("Processing payment for %s, amount: %.2f%n", user, amount);
9+
return true;
10+
}
11+
}

0 commit comments

Comments
 (0)