Decorating a class means adding functionality to an existing class. For example, you have a SingingPerson class that has a talent for singing.
public class SingingPerson { public string Talent = "I can sing"; public string DoTalent() { return Talent; } }
You later decided that the SingingPerson class should also dance, but did not want to modify the existing class structure. What you do is decorate the SingingPerson class by creating another class that contains the added functionality. This new class that you create accepts a SinginPerson object.
public class SingingAndDancingPerson { SingingPerson person; public string Talent { get; set; } public SingingAndDancingPerson(SingingPerson person) { this.person = person; } public string DoTalent() { this.Talent = person.Talent; this.Talent += " and dance"; return this.Talent; } }
When trying to instantiate these classes, the output will be as follows:
SingingPerson person1 = new SingingPerson(); Console.WriteLine("Person 1: " + person1.DoTalent()); SingingAndDancingPerson person2 = new SingingAndDancingPerson(person1); Console.WriteLine("Person 2: " + person2.DoTalent()); Console.ReadLine();
source share