API facade

We knew the pattern called facade from GoF, which abstracts the complex subsystem from the callers and exposes only necessary details as interfaces to the end user. API facade' is also aligned with the same definitions and implementations.

Let’s have a look at the following diagram, which depicts a simple implementation of multiple service calls from a client with and without the API facade pattern implementation:

As we can see in the preceding diagram, the client is calling one API facade to make it simpler and more meaningful in cases where the clients need multiple service calls. However, that can be implemented with a single API endpoint instead of the client calling multiple endpoints. The API facades provide high scalability and high performance as well.

Our investor services have implemented a simple API facade implementation for its delete operations. As we saw earlier, the delete methods call the design for intent methods. However, we have made the design for the intent method abstract to the caller by introducing a simple interface to our investor services. That brings the facade to our API.

The interface for the delete service is shown as follows:

public interface DeleteServiceFacade {
boolean deleteAStock(String investorId, String stockTobeDeletedSymbol);
boolean deleteStocksInBulk(String investorId, List<String> stocksSymbolsList);
}

The implementation for the delete service interface is shown in the following code snippet:

@Component
public class DeleteServiceFacadeImpl implements DeleteServiceFacade {
private static final Logger logger = LoggerFactory.getLogger(InvestorService.class);
private InvestorServicesFetchOperations investorServicesFetchOperations = new InvestorServicesFetchOperations();
@Override
public boolean deleteAStock(String investorId, String stockTobeDeletedSymbol) {
boolean deletedStatus = false;
Stock stockTobeDeleted = investorServicesFetchOperations.fetchSingleStockByInvestorIdAndStockSymbol(investorId,
stockTobeDeletedSymbol);
if (stockTobeDeleted != null) {
Investor investor = investorServicesFetchOperations.fetchInvestorById(investorId);
deletedStatus = investor.getStocks().remove(stockTobeDeleted);
}
designForIntentCascadePortfolioDelete(investorId, deletedStatus);
return deletedStatus;
}
.....
.....

As a simple exercise, we encourage the reader to implement our circuit breaker service call as an API facade and to also complete the bulk delete method within the delete service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.135