Adapter — wraps the external API, translates its shape into your domain shape.
java
@Componentpublic class WeatherApiAdapter { private final RestClient restClient; public WeatherApiAdapter(RestClient restClient) { this.restClient = restClient; } public WeatherInfo getWeather(String city) { ExternalWeatherResponse raw = restClient.get() .uri("/weather?city={city}", city) .retrieve() .body(ExternalWeatherResponse.class); return new WeatherInfo(raw.temp_c(), raw.condition().text()); }}
Service — contains business logic, uses the adapter, doesn’t know about HTTP.
java
@Servicepublic class WeatherService { private final WeatherApiAdapter weatherApiAdapter; public WeatherService(WeatherApiAdapter weatherApiAdapter) { this.weatherApiAdapter = weatherApiAdapter; } public String getFriendlyForecast(String city) { WeatherInfo info = weatherApiAdapter.getWeather(city); return info.temp() > 25 ? "It's hot in " + city : "It's mild in " + city; }}
Facade — combines multiple services/adapters behind one simple entry point.
java
@Componentpublic class TripPlannerFacade { private final WeatherService weatherService; private final FlightService flightService; private final HotelService hotelService; public TripPlannerFacade(WeatherService weatherService, FlightService flightService, HotelService hotelService) { this.weatherService = weatherService; this.flightService = flightService; this.hotelService = hotelService; } public TripPlan planTrip(String city) { return new TripPlan( weatherService.getFriendlyForecast(city), flightService.findFlights(city), hotelService.findHotels(city) ); }}
In one line:
- Adapter → translates an external API into your domain model.
- Service → business logic built on top of adapters.
- Facade → a single simplified entry point over multiple services.

Hinterlasse einen Kommentar