Decorator Design Pattern

Ayeshajaved
Posted by Ayeshajaved under C# category on | Points: 40 | Views : 267
Introduction
This is also known as Wrapper.It is a behavioral pattern, adds responsibilities to an object dynamically without changingthe behaviour of another object.


Using the code
In this we are adding functionality to existing object without changing its structure.It only add functionality to the original class.We are making classes on runtime and create objects dynamically,making different classes and inherit them is not a good approach. In decorator design pattern we can make changes but component class is same. here we are applying this pattern on online shopping website which contains an interface Online_shopping which is inherited by Mobile_Phone and Home_Appliances, interface class contains properties (Buy,Brand,Price) and we have an abstract class Online_Shopping_Decorator inheriting interface class and this abstract class is inherited by Special_Offer class and this class having method of discount and we are calling these methods on runtime.

using System;
namespace dp

{

public interface Online_Shopping

{

string Buy { get; }

string Brand { get; }

double Price { get; }

}



public class Mobile_Phone : Online_Shopping

{

public string Buy

{

get { return "Mobile_phone"; }

}



public string Brand

{

get { return "Windows Phone"; }

}



public double Price

{

get { return 1000000; }

}

public class Home_Appliances : Online_Shopping

{

public string Buy

{

get { return "Home_Appliances"; }

}



public string Brand

{

get { return "Braun"; }

}



public double Price

{

get { return 1000000; }

}



}



public abstract class Online_Shopping_Decorator : Online_Shopping

{

private Online_Shopping shop;



public Online_Shopping_Decorator (Online_Shopping shopp)

{

shop=shopp;

}



public string Buy

{

get { return shop.Buy; }

}



public string Brand

{

get { return shop.Brand; }

}



public double Price

{

get { return shop.Price; }

}



}



public class SpecialOffer : Online_Shopping_Decorator

{

public SpecialOffer(Online_Shopping shopp) : base(shopp) { }



public int DiscountPercentage { get; set; }

public string Offer { get; set; }



public double Price

{

get

{

double price = base.Price;

int percentage = 100 - DiscountPercentage;

return Math.Round((price * percentage) / 100, 2);

}

}



}





class Program

{

public static void Main(string[] args)

{

Mobile_Phone mp=new Mobile_Phone();

Home_Appliances home_app =new Home_Appliances();

Console.WriteLine("Windows Phone base price are : {0}", mp.Price);

Console.WriteLine("Braun Appliances base price are : {0}", home_app.Price);



// Special offer

SpecialOffer offer = new SpecialOffer(mp);

offer.DiscountPercentage = 25;

offer.Offer = "25 % discount";



Console.WriteLine("{1} @ Special Offer and price are : {0} ", offer.Price, offer.Offer);



Console.ReadKey();

SpecialOffer offer2 = new SpecialOffer(home_app);

offer2.DiscountPercentage = 25;

offer2.Offer = "25 % discount";



Console.WriteLine("{2} @ Special Offer and price are : {0} ", offer2.Price, offer2.Offer);



Console.ReadKey();}}





}

}

Comments or Responses

Login to post response