1 using System;
2
3 abstract class Component
4 {
5 public abstract void Draw();
6 }
7
8 class ConcreteComponent : Component
9 {
10 private string strName;
11 public ConcreteComponent(string s)
12 {
13 strName = s;
14 }
15
16 public override void Draw()
17 {
18 Console.WriteLine("ConcreteComponent - {0}", strName);
19 }
20 }
21
22 abstract class Decorator : Component
23 {
24 protected Component ActualComponent;
25
26 public void SetComponent(Component c)
27 {
28 ActualComponent = c;
29 }
30 public override void Draw()
31 {
32 if (ActualComponent != null)
33 ActualComponent.Draw();
34 }
35 }
36
37 class ConcreteDecorator : Decorator
38 {
39 private string strDecoratorName;
40 public ConcreteDecorator (string str)
41 {
42 // how decoration occurs is localized inside this decorator
43 // For this demo, we simply print a decorator name
44 strDecoratorName = str;
45 }
46 public override void Draw()
47 {
48 CustomDecoration();
49 base.Draw();
50 }
51 void CustomDecoration()
52 {
53 Console.WriteLine("In ConcreteDecorator: decoration goes here");
54 Console.WriteLine("{0}", strDecoratorName);
55 }
56 }
57
58
59 /// <summary>
60 /// Summary description for Client.
61 /// </summary>
62 public class Client
63 {
64 Component Setup()
65 {
66 ConcreteComponent c = new ConcreteComponent("This is the real component");
67
68 ConcreteDecorator d = new ConcreteDecorator("This is a decorator for the component");
69
70 d.SetComponent(c);
71
72 return d;
73 }
74
75 public static int Main(string[] args)
76 {
77 Client client = new Client();
78 Component c = client.Setup();
79
80 // The code below will work equally well with the real component,
81 // or a decorator for the component
82
83 c.Draw();
84
85 return 0;
86 }
87 }