RSS

Dependency Injection Structure

13 Apr

Highly coupled dependency

public class VerySimpleStockTraderImpl : IAutomatedStockTrader
{
    private IStockAnalysisService analysisService = new StockAnalysisServiceImpl();
    private IOnlineBrokerageService brokerageService = new NewYorkStockExchangeBrokerageServiceImpl();

    public void executeTrades()
    {
        for (String stockSymbol : brokerageService.getStockSymbols())
        {
             double askPrice = brokerageService.getAskingPrice(stockSymbol);
             double estimatedValue = analysisService.getEstimatedValue(stockSymbol);
             if (askPrice < estimatedValue)
             {
                  brokerageService.putBuyOrder(stockSymbol, 100, askPrice);
             }
        }
    }
}

public class MyApplication
{
    public static void main(String[] args)
    {
        IAutomatedStockTrader stockTrader = new VerySimpleStockTraderImpl();
        stockTrader.executeTrades();
    }
}

Manually injected dependency

public class VerySimpleStockTraderImpl : IAutomatedStockTrader
{
    private IStockAnalysisService analysisService;
    private IOnlineBrokerageService brokerageService;

    public VerySimpleStockTraderImpl(IStockAnalysisService analysisService, IOnlineBrokerageService brokerageService)
    {
        this.analysisService = analysisService;
        this.brokerageService = brokerageService;
    }

    public void 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();
    }
}

Automatically injected dependency

public class VerySimpleStockTraderImpl : IAutomatedStockTrader
{
    private IStockAnalysisService analysisService;
    private IOnlineBrokerageService brokerageService;

    public VerySimpleStockTraderImpl(IStockAnalysisService analysisService, IOnlineBrokerageService brokerageService)
    {
        this.analysisService = analysisService;
        this.brokerageService = brokerageService;
    }

    public void executeTrades()
    {
        ....
    }
}

public class MyApplication
{
    public static void main(String[] args)
    {
        IAutomatedStockTrader stockTrader = (IAutomatedStockTrader)DependencyManager.create(typeof(IAutomatedStockTrader));
        stockTrader.executeTrades();
    }
}

Source: Wikipedia

Advertisements
 
Leave a comment

Posted by on April 13, 2012 in References

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: