11.2. Pass the Buck

 <  Day Day Up  >  

Tim and I implemented ZipCodeVerificationService for Sam using an third-party provider . After a little while, Sam came to me with a complaint.

"I'm spending way too much money utilizing that third party. I can't believe that it costs so much," he said.

I thought about comparing the amount he spent on the service to what he spent on his double latte each day. I quickly dismissed the idea.

"Can you log the requests to the service so that I can double-check that the number of lookups the service says it performed is equal to the number it actually performed?" he asked.

"Of course," I replied.

We do not want to alter the implementation that calls the service. It is time for the Proxy pattern , as described in Chapter 6. In the Proxy pattern, multiple classes implement the same interface. The methods in one proxy class perform some functionality, such as encryption or checking for security access violations. Then they call the corresponding method in another proxy class. Figure 11-1 shows the sequence of calls. The Proxy pattern allows you to preprocess, or postprocess, the functionality in the interface you are hiding behind the proxy. Since the proxy interface and the original interface are identical, the caller can be blind to which one is really being called.

Figure 11-1. Proxy pattern for ZipCodeVerificationService

Example 11-5 shows the ZipCodeVerificationServiceImplementation . ZipCodeVerificationTracker also implements ZipCodeVerificationService , as shown in Example 11-6. It keeps track of the number of requested lookups.

Example 11-6. ZipCodeVerificationTracker
 ZipCodeVerificationTracker implements ZipCodeVerificationService     {     ZipCodeVerificationService next_implementation         = new ZipCodeVerificationImplementation( );     ZipCode find_zip_code_for_address(Address address_to_check)         throws ZipCodeNotFoundException         {         track_this_call( );         return next_implementation.find_zip_code_for_address(address_to_check);         }     } 

We need to alter the ZipCodeCorrectionServiceImplementation (Example 11-4) to call the logging proxy. [*] We change the initialization of zip_code_verification to:

[*] We would not need to change the method if we had implemented ZipCodeVerificationServiceFactory( ) , as discussed later in this chapter.

 static ZipCodeVerificationService zip_code_verification =             new ZipCodeVerificationTracker ( ); 

The only thing left is to decide what TRack_this_call( ) should do. We need to determine what we want to track. We could record each call to find_zip_code_for_address( ) in a log. Alternatively, we just could keep track of the number of calls and record that number at the end of the program.

DO A LITTLE AND PASS THE BUCK

Add proxies to interfaces to add functionality .


 <  Day Day Up  >  


Prefactoring
Prefactoring: Extreme Abstraction, Extreme Separation, Extreme Readability
ISBN: 0596008740
EAN: 2147483647
Year: 2005
Pages: 175
Authors: Ken Pugh

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net