Mediator in VB.NET
Defines simplified communication between classes
ReturnDefine an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Mediator pattern facilitate loosely coupled communication between different objects and object types. The mediator is a central hub through which all interaction must take place.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Mediator Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/Mediator.htm
3: Class MainApp
4: ' Entry point into console application.
5: Public Shared Sub Main()
6: Dim m As New ConcreteMediator()
7: Dim c1 As New ConcreteColleague1(m)
8: Dim c2 As New ConcreteColleague2(m)
9: m.Colleague1 = c1
10: m.Colleague2 = c2
11: c1.Send("How are you?")
12: c2.Send("Fine, thanks")
13: ' Wait for user
14: Console.ReadKey()
15: End Sub
16: End Class
17:
18: ' The 'Mediator' abstract class
19: MustInherit Class Mediator
20: Public MustOverride Sub Send(message As String, colleague As Colleague)
21: End Class
22:
23: ' The 'ConcreteMediator' class
24: Class ConcreteMediator
25: Inherits Mediator
26: Private _colleague1 As ConcreteColleague1
27: Private _colleague2 As ConcreteColleague2
28: Public WriteOnly Property Colleague1() As ConcreteColleague1
29: Set(value As ConcreteColleague1)
30: _colleague1 = value
31: End Set
32: End Property
33: Public WriteOnly Property Colleague2() As ConcreteColleague2
34: Set(value As ConcreteColleague2)
35: _colleague2 = value
36: End Set
37: End Property
38: Public Overrides Sub Send(message As String, colleague As Colleague)
39: If colleague Is _colleague1 Then
40: _colleague2.Notify(message)
41: Else
42: _colleague1.Notify(message)
43: End If
44: End Sub
45: End Class
46:
47: ' The 'Colleague' abstract class
48: MustInherit Class Colleague
49: Protected mediator As Mediator
50: ' Constructor
51: Public Sub New(mediator As Mediator)
52: Me.mediator = mediator
53: End Sub
54: End Class
55:
56: ' A 'ConcreteColleague' class
57: Class ConcreteColleague1
58: Inherits Colleague
59: ' Constructor
60: Public Sub New(mediator As Mediator)
61: MyBase.New(mediator)
62: End Sub
63: Public Sub Send(message As String)
64: mediator.Send(message, Me)
65: End Sub
66: Public Sub Notify(message As String)
67: Console.WriteLine(Convert.ToString("Colleague1 gets message: ") & message)
68: End Sub
69: End Class
70:
71: ' A 'ConcreteColleague' class
72: Class ConcreteColleague2
73: Inherits Colleague
74: ' Constructor
75: Public Sub New(mediator As Mediator)
76: MyBase.New(mediator)
77: End Sub
78: Public Sub Send(message As String)
79: mediator.Send(message, Me)
80: End Sub
81: Public Sub Notify(message As String)
82: Console.WriteLine(Convert.ToString("Colleague2 gets message: ") & message)
83: End Sub
84: 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/Mediator.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |