Ensuring Correct Use of the Memoization Pattern
Lawrence D. Spencer, Seth H. Richards · 2015
This chapter covers how to implement and test the Memoization Pattern suggested by Charlotte and shows how the pattern can be applied using AOP to reduce the costs incurred through the use of the third-party restaurantApi. The chapter then focuses on unit-testing implementations of the Memoization Pattern. It covers the application of aspect that caches results from web service calls to the third-party restauarantApi.The first step in implementing the memoizedRestaurantApi is to write unit tests that verify the aspect's functionality. One of the more interesting aspects of the unit tests is the mechanism used to keep track of the number of times the decorated function is executed. The natural choice is to create a spy on testObject.testFunction, and then apply the aspect to the spied-upon function. The next step is to decorate the appropriate method of restaurantApi with the returnValueCache aspect.