-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecoratorExample.cs
More file actions
74 lines (64 loc) · 2.37 KB
/
DecoratorExample.cs
File metadata and controls
74 lines (64 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignPattern
{
// The Component abstract class defines an interface for objects that can have responsibilities added to them dynamically.
public abstract class Component
{
public abstract void Operation();
}
// The ConcreteComponent class is a basic implementation of the Component interface.
public class ConcreteComponent : Component
{
// The Operation method performs the basic operation.
public override void Operation()
{
Console.WriteLine("ConcreteComponent Operation Added");
}
}
// The Decorator abstract class inherits from Component and contains a reference to a Component object.
public abstract class Decorator : Component
{
protected Component _component;
// The SetComponent method sets the Component object to be decorated.
public void SetComponent(Component component)
{
_component = component;
}
// The Operation method calls the Operation method of the Component object if it exists.
public override void Operation()
{
if (_component != null)
{
_component.Operation();
}
}
}
// The ConcreteDecorator class inherits from Decorator and adds additional behavior to the Component object.
public class ConcreteDecorator : Decorator
{
// The Operation method calls the base Operation method and then adds additional behavior.
public override void Operation()
{
base.Operation();
Console.WriteLine("ConcreteDecorator Operation Added");
}
}
public class DecoratorExample
{
public static void Run()
{
// Create a ConcreteComponent object.
ConcreteComponent component = new ConcreteComponent();
// Create a ConcreteDecorator object.
ConcreteDecorator decorator = new ConcreteDecorator();
// Set the ConcreteComponent object to be decorated by the ConcreteDecorator.
decorator.SetComponent(component);
// Call the Operation method on the ConcreteDecorator, which adds additional behavior to the ConcreteComponent.
decorator.Operation();
}
}
}