Type: docs and refactoring Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
This commit is contained in:
@ -33,9 +33,9 @@ package com.iluwatar.business.delegate;
|
||||
* retrieved through service lookups. The Business Delegate itself may contain business logic too
|
||||
* potentially tying together multiple service calls, exception handling, retrying etc.
|
||||
*
|
||||
* <p>In this example the client ({@link Client}) utilizes a business delegate (
|
||||
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
|
||||
* service and makes the service call.
|
||||
* <p>In this example the client ({@link MobileClient}) utilizes a business delegate (
|
||||
* {@link BusinessDelegate}) to search for movies in video streaming services. The Business Delegate
|
||||
* then selects the appropriate service and makes the service call.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@ -46,18 +46,16 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// prepare the objects
|
||||
var businessDelegate = new BusinessDelegate();
|
||||
var businessLookup = new BusinessLookup();
|
||||
businessLookup.setEjbService(new EjbService());
|
||||
businessLookup.setJmsService(new JmsService());
|
||||
|
||||
businessLookup.setNetflixService(new NetflixService());
|
||||
businessLookup.setYouTubeService(new YouTubeService());
|
||||
businessDelegate.setLookupService(businessLookup);
|
||||
businessDelegate.setServiceType(ServiceType.EJB);
|
||||
|
||||
var client = new Client(businessDelegate);
|
||||
client.doTask();
|
||||
|
||||
businessDelegate.setServiceType(ServiceType.JMS);
|
||||
client.doTask();
|
||||
// create the client and use the business delegate
|
||||
var client = new MobileClient(businessDelegate);
|
||||
client.playbackMovie("Die Hard 2");
|
||||
client.playbackMovie("Maradona: The Greatest Ever");
|
||||
}
|
||||
}
|
||||
|
@ -23,24 +23,18 @@
|
||||
|
||||
package com.iluwatar.business.delegate;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* BusinessDelegate separates the presentation and business tiers.
|
||||
*/
|
||||
@Setter
|
||||
public class BusinessDelegate {
|
||||
|
||||
private BusinessLookup lookupService;
|
||||
private ServiceType serviceType;
|
||||
|
||||
public void setLookupService(BusinessLookup businessLookup) {
|
||||
this.lookupService = businessLookup;
|
||||
}
|
||||
|
||||
public void setServiceType(ServiceType serviceType) {
|
||||
this.serviceType = serviceType;
|
||||
}
|
||||
|
||||
public void doTask() {
|
||||
BusinessService businessService = lookupService.getBusinessService(serviceType);
|
||||
businessService.doProcessing();
|
||||
public void playbackMovie(String movie) {
|
||||
VideoStreamingService videoStreamingService = lookupService.getBusinessService(movie);
|
||||
videoStreamingService.doProcessing();
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
package com.iluwatar.business.delegate;
|
||||
|
||||
import java.util.Locale;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
@ -31,21 +32,21 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class BusinessLookup {
|
||||
|
||||
private EjbService ejbService;
|
||||
private NetflixService netflixService;
|
||||
|
||||
private JmsService jmsService;
|
||||
private YouTubeService youTubeService;
|
||||
|
||||
/**
|
||||
* Gets service instance based on service type.
|
||||
* Gets service instance based on given movie search string.
|
||||
*
|
||||
* @param serviceType Type of service instance to be returned.
|
||||
* @param movie Search string for the movie.
|
||||
* @return Service instance.
|
||||
*/
|
||||
public BusinessService getBusinessService(ServiceType serviceType) {
|
||||
if (serviceType.equals(ServiceType.EJB)) {
|
||||
return ejbService;
|
||||
public VideoStreamingService getBusinessService(String movie) {
|
||||
if (movie.toLowerCase(Locale.ROOT).contains("die hard")) {
|
||||
return netflixService;
|
||||
} else {
|
||||
return jmsService;
|
||||
return youTubeService;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,17 +24,17 @@
|
||||
package com.iluwatar.business.delegate;
|
||||
|
||||
/**
|
||||
* Client utilizes BusinessDelegate to call the business tier.
|
||||
* MobileClient utilizes BusinessDelegate to call the business tier.
|
||||
*/
|
||||
public class Client {
|
||||
public class MobileClient {
|
||||
|
||||
private final BusinessDelegate businessDelegate;
|
||||
|
||||
public Client(BusinessDelegate businessDelegate) {
|
||||
public MobileClient(BusinessDelegate businessDelegate) {
|
||||
this.businessDelegate = businessDelegate;
|
||||
}
|
||||
|
||||
public void doTask() {
|
||||
businessDelegate.doTask();
|
||||
public void playbackMovie(String movie) {
|
||||
businessDelegate.playbackMovie(movie);
|
||||
}
|
||||
}
|
@ -26,13 +26,13 @@ package com.iluwatar.business.delegate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Service EJB implementation.
|
||||
* NetflixService implementation.
|
||||
*/
|
||||
@Slf4j
|
||||
public class EjbService implements BusinessService {
|
||||
public class NetflixService implements VideoStreamingService {
|
||||
|
||||
@Override
|
||||
public void doProcessing() {
|
||||
LOGGER.info("EjbService is now processing");
|
||||
LOGGER.info("NetflixService is now processing");
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.business.delegate;
|
||||
|
||||
/**
|
||||
* Enumeration for service types.
|
||||
*/
|
||||
public enum ServiceType {
|
||||
|
||||
EJB,
|
||||
JMS
|
||||
}
|
@ -24,9 +24,9 @@
|
||||
package com.iluwatar.business.delegate;
|
||||
|
||||
/**
|
||||
* Interface for service implementations.
|
||||
* Interface for video streaming service implementations.
|
||||
*/
|
||||
public interface BusinessService {
|
||||
public interface VideoStreamingService {
|
||||
|
||||
void doProcessing();
|
||||
}
|
@ -26,13 +26,13 @@ package com.iluwatar.business.delegate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Service JMS implementation.
|
||||
* YouTubeService implementation.
|
||||
*/
|
||||
@Slf4j
|
||||
public class JmsService implements BusinessService {
|
||||
public class YouTubeService implements VideoStreamingService {
|
||||
|
||||
@Override
|
||||
public void doProcessing() {
|
||||
LOGGER.info("JmsService is now processing");
|
||||
LOGGER.info("YouTubeService is now processing");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user