Design Pattern Examples
Overview of object-oriented design patterns
Observer_Class.cs
Go to the documentation of this file.
1
7
8using System;
9using System.Text;
10
12{
18 {
23
24
30 public ObserverForDecimal(INumberProducer numberProducer)
31 {
32 if (numberProducer == null)
33 {
34 throw new ArgumentNullException("numberProducer", "The ObserverForDecimal constructor requires a valid INumberProducer object.");
35 }
36 _numberProducer = numberProducer;
37 }
38
49 {
50 uint number = _numberProducer.FetchNumber();
51 Console.WriteLine(" Decimal : {0}", number);
52 }
53 }
54
55
56 //########################################################################
57 //########################################################################
58
59
65 {
70
77 {
78 if (numberProducer == null)
79 {
80 throw new ArgumentNullException("numberProducer", "The ObserverForHexaDecimal constructor requires a valid INumberProducer object.");
81 }
82 _numberProducer = numberProducer;
83 }
84
85
96 {
97 uint number = _numberProducer.FetchNumber();
98 Console.WriteLine(" Hexadecimal: 0x{0:X8}", number);
99 }
100 }
101
102
103 //########################################################################
104 //########################################################################
105
106
112 {
117
123 public ObserverForBinary(INumberProducer numberProducer)
124 {
125 if (numberProducer == null)
126 {
127 throw new ArgumentNullException("numberProducer", "The ObserverForBinary constructor requires a valid INumberProducer object.");
128 }
129 _numberProducer = numberProducer;
130 }
131
132
144 {
145 uint number = _numberProducer.FetchNumber();
146 StringBuilder output = new StringBuilder();
147 uint mask = (uint)1 << 31;
148
149 for (uint index = 0; index < 32; ++index)
150 {
151 if ((number & mask) != 0)
152 {
153 output.Append("1");
154 }
155 else
156 {
157 output.Append("0");
158 }
159 mask >>= 1;
160 }
161
162 Console.WriteLine(" Binary : {0}", output);
163 }
164 }
165}
Represents an observer that prints out the current number from the Subject in binary.
ObserverForBinary(INumberProducer numberProducer)
Constructor.
INumberProducer _numberProducer
The number producer from which to get the current number.
Represents an observer that prints out the current number from the Subject in decimal.
ObserverForDecimal(INumberProducer numberProducer)
Constructor.
INumberProducer _numberProducer
The number producer from which to get the current number.
Represents an observer that prints out the current number from the Subject in hexadecimal.
ObserverForHexaDecimal(INumberProducer numberProducer)
Constructor.
INumberProducer _numberProducer
The number producer from which to get the current number.
Represents the Subject to the observers. This is the minimum needed by observers to get access to the...
uint FetchNumber()
Return the current value from the Subject.
Represents an observer to the Subject class. An observer implements this interface and then subscribe...
void NumberChanged()
This is called whenever the number in the ObserverSubject_NumberProducer object is changed.
The namespace containing all Design Pattern Examples implemented in C#.