This post continues from the previous post. The list of posts can be seen on the right side of this text.
Below is the XAML listing for the WPF program that I called ObserveWeather. The code in the book Head First Design Patterns is written in Java, but C# so similar that I was able to write it in C# easily. Feel free to use the code below to create your own program.
1
<
Window
x:Class
=
"ObserveWeather.MainWindow"
6
xmlns:local
=
"clr-namespace:ObserveWeather"
8
Title
=
"Head First Design Patterns - Chapter 2 Observer Pattern"
Height
=
"250"
Width
=
"480"
>
13
</
Grid.RowDefinitions
>
16
<
TextBlock
FontSize
=
"14"
FontWeight
=
"Bold"
>Current Conditions</
TextBlock
>
17
<
TextBlock
x:Name
=
"txtblk01"
></
TextBlock
>
18
<
TextBlock
x:Name
=
"txtblk02"
></
TextBlock
>
19
<
TextBlock
x:Name
=
"txtblk03"
></
TextBlock
>
24
<
TextBlock
FontSize
=
"14"
FontWeight
=
"Bold"
>Phone Display</
TextBlock
>
25
<
TextBlock
x:Name
=
"txtblk11"
></
TextBlock
>
26
<
TextBlock
x:Name
=
"txtblk12"
></
TextBlock
>
27
<
TextBlock
x:Name
=
"txtblk13"
></
TextBlock
>
Below is the code behind in the file MainWindow.xaml.cs.
2
namespace
ObserveWeather
4
public
partial
class
MainWindow : Window
10
WeatherData weatherData =
new
WeatherData();
11
CurrentConditionsDisplay currentDisplay =
new
CurrentConditionsDisplay(weatherData);
12
PhoneDisplay phoneDisplay =
new
PhoneDisplay(weatherData);
14
weatherData.setMeasurements(80f, 65f, 30.4f);
Below is the interface ISubject.cs.
1
namespace
ObserveWeather
3
public
interface
ISubject
5
public
void
registerObserver(IObserver o);
6
public
void
removeObserver(IObserver o);
7
public
void
notifyObservers();
Below is the interface IObserver.cs.
1
namespace
ObserveWeather
3
public
interface
IObserver
5
public
void
update(
float
temp,
float
humidity,
float
pressure);
Below is the interface IDisplayElement.cs.
1
namespace
ObserveWeather
3
public
interface
IDisplayElement
Below is the code for WeatherData.cs.
1
using
System.Collections.Generic;
2
namespace
ObserveWeather
4
public
class
WeatherData : ISubject
6
private
List<IObserver> observers;
7
private
float
temperature;
8
private
float
humidity;
9
private
float
pressure;
13
observers =
new
List<IObserver>();
15
public
void
registerObserver(IObserver o)
19
public
void
removeObserver(IObserver o)
21
int
i = observers.IndexOf(o);
22
if
(i >= 0) observers.RemoveAt(i);
24
public
void
notifyObservers()
31
foreach
(IObserver o
in
this
.observers)
33
o.update(temperature, humidity, pressure);
36
public
void
measurementsChanged()
40
public
void
setMeasurements(
float
temperature,
float
humidity,
float
pressure)
43
this
.temperature = temperature;
44
this
.humidity = humidity;
45
this
.pressure = pressure;
46
measurementsChanged();
CurrentConditionDisplay.cs
2
namespace
ObserveWeather
4
public
class
CurrentConditionsDisplay : IObserver, IDisplayElement
6
private
float
temperature;
7
private
float
humidity;
8
private
ISubject weatherData;
10
public
CurrentConditionsDisplay (ISubject weatherData)
12
this
.weatherData = weatherData;
13
weatherData.registerObserver(
this
);
15
public
void
update(
float
temperature,
float
humidity,
float
pressure)
17
this
.temperature = temperature;
18
this
.humidity = humidity;
23
foreach
(Window window
in
Application.Current.Windows)
25
if
(window.GetType() ==
typeof
(MainWindow))
27
(window
as
MainWindow).txtblk01.Text = temperature.ToString();
28
(window
as
MainWindow).txtblk02.Text = humidity.ToString();
PhoneDisplay.cs
2
namespace
ObserveWeather
4
class
PhoneDisplay : IObserver, IDisplayElement
6
private
float
temperature;
7
private
float
humidity;
8
private
ISubject weatherData;
10
public
PhoneDisplay(ISubject weatherData)
12
this
.weatherData = weatherData;
13
weatherData.registerObserver(
this
);
15
public
void
update(
float
temperature,
float
humidity,
float
pressure)
17
this
.temperature = temperature;
18
this
.humidity = humidity;
23
foreach
(Window window
in
Application.Current.Windows)
25
if
(window.GetType() ==
typeof
(MainWindow))
27
(window
as
MainWindow).txtblk11.Text = temperature.ToString();
28
(window
as
MainWindow).txtblk12.Text = humidity.ToString();
Series Navigation << Observer Pattern Weather Introduction