Observer in VB.NET
A way of notifying change to a number of classes
ReturnDefine a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Observer pattern with registered objects are notified of and updated with a state change.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Observer Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/Observer.htm
3: Class MainApp
4: ' Entry point into console application.
5: Public Shared Sub Main()
6: ' Configure Observer pattern
7: Dim s As New ConcreteSubject()
8: s.Attach(New ConcreteObserver(s, "X"))
9: s.Attach(New ConcreteObserver(s, "Y"))
10: s.Attach(New ConcreteObserver(s, "Z"))
11: ' Change subject and notify observers
12: s.SubjectState = "ABC"
13: s.Notify()
14: ' Wait for user
15: Console.ReadKey()
16: End Sub
17: End Class
18:
19: ' The 'Subject' abstract class
20: MustInherit Class Subject
21: Private _observers As New List(Of Observer)()
22: Public Sub Attach(observer As Observer)
23: _observers.Add(observer)
24: End Sub
25: Public Sub Detach(observer As Observer)
26: _observers.Remove(observer)
27: End Sub
28: Public Sub Notify()
29: For Each o As Observer In _observers
30: o.Update()
31: Next
32: End Sub
33: End Class
34:
35: ' The 'ConcreteSubject' class
36: Class ConcreteSubject
37: Inherits Subject
38: Private _subjectState As String
39: ' Gets or sets subject state
40: Public Property SubjectState() As String
41: Get
42: Return _subjectState
43: End Get
44: Set(value As String)
45: _subjectState = value
46: End Set
47: End Property
48: End Class
49:
50: ' The 'Observer' abstract class
51: MustInherit Class Observer
52: Public MustOverride Sub Update()
53: End Class
54:
55: ' The 'ConcreteObserver' class
56: Class ConcreteObserver
57: Inherits Observer
58: Private _name As String
59: Private _observerState As String
60: Private _subject As ConcreteSubject
61: ' Constructor
62: Public Sub New(subject As ConcreteSubject, name As String)
63: Me._subject = subject
64: Me._name = name
65: End Sub
66: Public Overrides Sub Update()
67: _observerState = _subject.SubjectState
68: Console.WriteLine("Observer {0}'s new state is {1}", _name, _observerState)
69: End Sub
70: ' Gets or sets subject
71: Public Property Subject() As ConcreteSubject
72: Get
73: Return _subject
74: End Get
75: Set(value As ConcreteSubject)
76: _subject = value
77: End Set
78: End Property
79: 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/Observer.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |