Strategy Pattern -last example variation

				
					namespace strategypattern
{
    public interface IDownloadOrder
    {
        void Download(Order order);
    }

    public class XMLDownloadOrder : IDownloadOrder
    {
        public void Download(Order order)
        {
            Console.WriteLine("downloading order as xml");
        }
    }

    public class JSONDownloadOrder : IDownloadOrder
    {
        public void Download(Order order)
        {
            Console.WriteLine("downloading order as JSON");
        }
    }

    public class CSVDownloadOrder : IDownloadOrder
    {
        public void Download(Order order)
        {
            Console.WriteLine("downloading order as CSV");
        }
    }
}

				
			
				
					namespace strategypattern
{
    public class Order
    {
        public string OrderId { get; set; }
        public string ProductName { get; set; }
        public string ProductDisplayName { get; set; }
        public string ProductPrice { get; set; }
        public Order(string orderId, string productName, string productDisplayName, string productPrice)
        {
            OrderId = orderId;
            ProductName = productName;
            ProductDisplayName = productDisplayName;
            ProductPrice = productPrice;
            //you can set default value in the constructor
            //downloadOrderService = new JSONDownloadOrder;

            //or

            //directly pass as parameter to the constructor 
            //public Order(string orderId, string productName, string productDisplayName, string productPrice,IDownloadOrder downloadOrderService)
        }

        public void Download(IDownloadOrder downloadOrderService)
        {
            downloadOrderService.Download(this);
        }
    }

}

				
			
				
					using strategypattern;

var order = new Order("123", "ps5", "ps5 console", "54990");
order.Download(new XMLDownloadOrder());

				
			

downloading order as XML

Leave a Comment