Dependency Injection

public class MyWeb

{

   Dao myDao=null;

   public MyWeb(Dao d)

   {

       myDao=d;

   }

}


- Self configuration of dependencies

{A{B{C}}}


DI contianer is going to create an object which contain all ABC dependency

DI Container:

   -- create c

   -- create B {C}

   -- create A {B{C}}


Dependency injection involves at least three elements:

  • a dependent consumer,

  • a declaration of a component's dependencies, defined asinterface contracts,

  • an injector (sometimes referred to as a provider or container) that creates instances of classes that implement a given dependency interface on request.

------------------------------------------

EXAMPLE:

constructor injection.


publicinterface IOnlineBrokerageService {String[] getStockSymbols();double getAskingPrice(String stockSymbol);double getOfferPrice(String stockSymbol);void putBuyOrder(String stockSymbol, int shares, double bidPrice);void putSellOrder(String stockSymbol, int shares, double offerPrice);}publicinterface IStockAnalysisService {double getEstimatedValue(String stockSymbol);}publicinterface IAutomatedStockTrader {void executeTrades();}


你可能感兴趣的:(Pattern)