public interface IOnlineBrokerageService { String[] getStockSymbols(); double getBidPrice(String stockSymbol); double getAskPrice(String stockSymbol); void putBuyOrder(String stockSymbol, int shares, double buyPrice); void putSellOrder(String stockSymbol, int shares, double sellPrice); }
public interface IStockAnalysisService { double getEstimatedValue(String stockSymbol); }
public interface IAutomatedStockTrader { void executeTrades(); }
DIを使わない場合のソース
public class VerySimpleStockTraderImpl implements IAutomatedStockTrader {
private IStockAnalysisService analysisService = new StockAnalysisServiceImpl(); private IOnlineBrokerageService brokerageService = new NewYorkStockExchangeBrokerageServiceImpl();
public void executeTrades() { …. } }
public class MyApplication { public static void main(String[] args) { IAutomatedStockTrader stockTrader = new VerySimpleStockTraderImpl(); stockTrader.executeTrades(); } }
public class MyApplication { public static void main(String[] args) { IStockAnalysisService analysisService = new StockAnalysisServiceImpl(); IOnlineBrokerageService brokerageService = new NewYorkStockExchangeBrokerageServiceImpl();
IAutomatedStockTrader stockTrader = new VerySimpleStockTraderImpl( analysisService, brokerageService); stockTrader.executeTrades(); } }