Decorator in VB.NET
Add responsibilities to objects dynamically
ReturnAttach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Decorator pattern dynamically adds extra functionality to an existing object.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Decorator Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/Decorator.htm
3: Class MainApp
4: ' Entry point into console application.
5: Public Shared Sub Main()
6: ' Create ConcreteComponent and two Decorators
7: Dim c As New ConcreteComponent()
8: Dim d1 As New ConcreteDecoratorA()
9: Dim d2 As New ConcreteDecoratorB()
10: ' Link decorators
11: d1.SetComponent(c)
12: d2.SetComponent(d1)
13: d2.Operation()
14: ' Wait for user
15: Console.ReadKey()
16: End Sub
17: End Class
18:
19: ' The 'Component' abstract class
20: MustInherit Class Component
21: Public MustOverride Sub Operation()
22: End Class
23:
24: ' The 'ConcreteComponent' class
25: Class ConcreteComponent
26: Inherits Component
27: Public Overrides Sub Operation()
28: Console.WriteLine("ConcreteComponent.Operation()")
29: End Sub
30: End Class
31:
32: ' The 'Decorator' abstract class
33: MustInherit Class Decorator
34: Inherits Component
35: Protected component As Component
36: Public Sub SetComponent(component As Component)
37: Me.component = component
38: End Sub
39: Public Overrides Sub Operation()
40: If component IsNot Nothing Then
41: component.Operation()
42: End If
43: End Sub
44: End Class
45:
46: ' The 'ConcreteDecoratorA' class
47: Class ConcreteDecoratorA
48: Inherits Decorator
49: Public Overrides Sub Operation()
50: MyBase.Operation()
51: Console.WriteLine("ConcreteDecoratorA.Operation()")
52: End Sub
53: End Class
54:
55: ' The 'ConcreteDecoratorB' class
56: Class ConcreteDecoratorB
57: Inherits Decorator
58: Public Overrides Sub Operation()
59: MyBase.Operation()
60: AddedBehavior()
61: Console.WriteLine("ConcreteDecoratorB.Operation()")
62: End Sub
63: Private Sub AddedBehavior()
64: End Sub
65: End Class
See also:
Creational Patterns
- Abstract Factory in VB.NET - Creates an instance of several families of classes.
- Builder in VB.NET - Separates object construction from its representation.
- Factory Method in VB.NET - Creates an instance of several derived classes.
- Prototype in VB.NET - A fully initialized instance to be copied or cloned.
- Singleton in VB.NET - A class of which only a single instance can exist.
- Adapter in VB.NET - Match interfaces of different classes
- Bridge in VB.NET - Separates an object???s interface from its implementation
- Composite in VB.NET - A tree structure of simple and composite objects
- Decorator in VB.NET - Add responsibilities to objects dynamically
- Facade in VB.NET - A single class that represents an entire subsystem
- Flyweight in VB.NET - A fine-grained instance used for efficient sharing
- Proxy in VB.NET - An object representing another object
- Chain of Resp. in VB.NET - A way of passing a request between a chain of objects
- Command in VB.NET - Encapsulate a command request as an object
- Interpreter in VB.NET - A way to include language elements in a program
- Iterator in VB.NET - Sequentially access the elements of a collection
- Mediator in VB.NET - Defines simplified communication between classes
- Memento in VB.NET - Capture and restore an object's internal state
- Observer in VB.NET - A way of notifying change to a number of classes
- State in VB.NET - Alter an object's behavior when its state changes
- Strategy in VB.NET - Encapsulates an algorithm inside a class
- Template Method in VB.NET - Defer the exact steps of an algorithm to a subclass
- Visitor in VB.NET - Defines a new operation to a class without change
Comments (
)
Link to this page:
//www.vb-net.com/ProgramTheory/Decorator.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |