Oregon Institute of Technology

Observer Lab Fall
For Demonstration in class onOctober 16

Overview

The observer pattern lets objects be informed when something happens elsewhere. Traditionally, observers are implemented using interfaces—with the observer registering an interest with a subject. In this lab, you will start with a “Gamma” style codebase and then refactor the code to use C# delegates\events.

part 1- Using the observer pattern

In this lab you will work with an interface implementation of the observer pattern and then refactor to a C#delegate\event implementation of the observer pattern. The lab is a 'Console Clock' application. Using the Framework Class Library (FCL) Console APIs, various clocks are displayed in the console window. These clocks display time at various intervals ranging from once every hundredth of a second to once every hour. (Note: not all clocks are actually implemented.)

The clocks are implemented as observers. They implement an ITimerObserver interface and are registered with the Ticker class. The Ticker class runs on its own thread andcalls the observers. You will refactor this code to use delegates and events for observerregistration and notification

steps:

  1. Open the ObserverLabStarterCode solution and familiarize yourself with the GammaConsoleClockObserverproject.

Run the GammaConsoleClockObserverapplication to see the clocks in action. You will see three clocks in the top-left corner of the console. The first one is updated each second, the second clock every tenth of a second, and thethirdclock every hundredth of a second.

  1. The CSharpConsoleClockObserver application is currently a copy of the GammaConsoleClockObserver application. This is where you will be working. Close all the files that are currently open in Visual Studio and make sure you only edit the files from the CSharpConsoleClockObserver application.
  2. The first steps will be to update the Ticker class.

steps:

  1. In the ticker.cs file, add threeglobal public delegate class types—call them OnSecondsDelegate, OnTenthsDelegate, and OnHundredthsDelegate. Note: The signatures must match the methods you need to invoke in your clock classes.
    For example: public delegate void OnTenthsDelegate();
  2. Within the Ticker class:

Add 3 delegate instance variables, one for each delegate type. Call these instancesOnTenthsTick, OnHundredthsTick,and OnSecondsTick. Use the “event” key word to wrap the delegates for safer use in our Observer pattern implementation.

Delete the timers list and the code that uses it inside the Run method.

Remove the RegisterTimer and UnRegisterTimer methods.

Add a NullHandler() method. Add a Ticker c’tor and initialize the Ticker
delegates to the address of the NullHandler() method for thread safety.

Inside the Run method,add code to invoke the delegates.

  1. Refactor each of the clock classes:

Add a Ticker parameter to the c’tor. In the c’tor add the appropriate methodaddresses to the Ticker’s delegate invocation lists.

Add a private data member of type Ticker to use in the Dispose method.Initialize this private data member in the c’tor.

Remove the ITimerObserver inheritance. Also, remove any methods that will not be used. Change each of the remaining methods to private access sincethey will only be invoked by the delegate invocation list.

Implement the IDisposable interface to remove the appropriate method(s) from the Ticker’s delegate invocation lists.

  1. In Main, refactor the code to create an instance of Ticker. Then create an instance of each clock passing your Ticker instance as a parameter. The threading code should not be modified. Once this works, implement “using” statements to fire the Dispose() methods of each clock.

5. Remove any files the project no longer uses.

Questions:

1)Explain what the “event” keyword does

2)Explain what the “using” statement does