Example #1
0
        /**
         * Purspose is to demo the Adapter design pattern
         * The target is the StockData interface
         * the Adaptee is StockDataHTML which provides useful information
         * but is incompatible with client.
         * The Adapter is StockDataAdapter
         * */
        static void Main(string[] args)
        {
            // this is the stock symbol client wants to analyze
            string symbol = "CAT";
            // create an advisor to offer advice
            Advisor advisor = new Advisor();

            /**
             * the first data source is compatible with client
             * */
            IStockData stockData1 = new StockDataJSON();

            // gets the data in an array compatible with Advisor
            StockPrice[] prices1 = stockData1.GetStockData(symbol);
            Console.WriteLine("The Advisor offers the following recommendation for {0}: {1} from StockDataJSON", symbol, advisor.OfferAdvice(prices1));

            /*
             * the second data source is incompatible with Advisor
             * and needs to use the adapter
             * */
            StockDataHTML stockDataHTML = new StockDataHTML();
            IStockData    stockData2    = new StockDataAdapter(stockDataHTML);

            StockPrice[] prices2 = stockData2.GetStockData(symbol);
            Console.WriteLine("The Advisor offers the following recommendation for {0}: {1} from StockDataHTML", symbol, advisor.OfferAdvice(prices2));
        }
Example #2
0
 // Adapter could manage different data sources
 public StockDataAdapter(StockDataHTML stockDataHTML)
 {
     this._stockDataHTML = stockDataHTML;
 }