
 |
Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.
Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.
eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.
Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.
eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
|
|
 |
|
  |
|
|
|
|

Working With Design Patterns: Adapter
By Jeff Langr
Go to page: 1 2 Next
The Portfolio class provides the basis for an application that allows users to track stock purchases. Of course, the most desired portfolio functionality is the ability to calculate the worth of all those stock purchases.
The PortfolioTest class, shown in Listing 1, demonstrates use of a simple stub to assist in verifying that the Portfolio can correctly obtain a value. The Portfolio class (see Listing 2) derives the price for each symbol by calling out through a StockLookupService interface. StockLookupService is as simple as it gets: Given a stock symbol, the service returns the symbol's current price (in dollars):
public interface StockLookupService {
int currentPrice(String symbol);
}
Listing 1: PortfolioTest.
import static org.junit.Assert.*;
import org.junit.*;
public class PortfolioTest {
private static final String MICROSOFT = "MSFT";
private static final int MICROSOFT_VALUE = 100;
private static final String IBM = "IBM";
private static final int IBM_VALUE = 80;
private Portfolio portfolio;
@Before
public void initialize() {
StockLookupService service = new StockLookupService() {
@Override
public int currentPrice(String symbol) {
if (symbol.equals(MICROSOFT))
return MICROSOFT_VALUE;
if (symbol.equals(IBM))
return IBM_VALUE;
return 0;
}
};
portfolio = new Portfolio(service);
}
@Test
public void isEmptyOnCreation() {
assertSize(0);
assertEquals(0, portfolio.value());
}
@Test
public void storesSharesPerSymbol() {
portfolio.purchase(MICROSOFT, 2);
assertSize(1);
assertEquals(2, portfolio.shares(MICROSOFT));
assertEquals(2 * MICROSOFT_VALUE, portfolio.value());
}
@Test
public void sumsSharesPurchasedForSameSymbol() {
portfolio.purchase(MICROSOFT, 1);
portfolio.purchase(MICROSOFT, 2);
assertSize(1);
assertEquals(3, portfolio.shares(MICROSOFT));
assertEquals(3 * MICROSOFT_VALUE, portfolio.value());
}
@Test
public void segregatesPurchasesBySymbol() {
portfolio.purchase(MICROSOFT, 5);
portfolio.purchase(IBM, 10);
assertSize(2);
assertEquals(5, portfolio.shares(MICROSOFT));
assertEquals(10, portfolio.shares(IBM));
int expectedValue = 5 * MICROSOFT_VALUE + 10 * IBM_VALUE;
assertEquals(expectedValue, portfolio.value());
}
void assertSize(int expected) {
assertEquals(0 == expected, portfolio.isEmpty());
assertEquals(expected, portfolio.size());
}
}
Listing 2: Portfolio.
import java.util.*;
public class Portfolio {
private Map<String, Integer> symbols =
new HashMap<String, Integer>();
private StockLookupService service;
public Portfolio(StockLookupService service) {
this.service = service;
}
public boolean isEmpty() {
return 0 == size();
}
public int size() {
return symbols.size();
}
public void purchase(String symbol, int shares) {
symbols.put(symbol, shares(symbol) + shares);
}
public int shares(String symbol) {
if (!symbols.containsKey(symbol))
return 0;
return symbols.get(symbol);
}
public int value() {
int total = 0;
for (Map.Entry<String, Integer> holding:
symbols.entrySet()) {
String symbol = holding.getKey();
int shares = holding.getValue();
total += service.currentPrice(symbol) * shares;
}
return total;
}
}
Go to page: 1 2 Next
|