How to use Delegate as Observer Pattern

Delegate as Observer Pattern:

The delegate can also be used as Observer pattern, Observer pattern keeps and maintains a list of dependents. Whenever a change occurs, it informs every dependent about the change. Let’s consider an example.

We have three devices which need the weather update whenever a change in weather occurs. So they need to register themselves with any authority which provides weather forecast or any weather update. That authority will have a subscribe method. Through which it maintains records of the dependents and updates them whenever a change occurs in weather.

Daylight Saving Example for Different Devices:

You have different electronic devices such as Mobile, watches, Samsung Gear, etc. that show date and time. At daylight saving months, all these devices change their time by incrementing hours.

Similarly, they decrement the hours when winter comes. You have to write a program that enables these devices to automatically change their time. a. Write a Class GMTTimer that announces the updated time either by incrementing or decrementing hour. i. The class has

i. The class has the private attributes such as _hours, _minutes, and _seconds.
ii. The class also provides public attributes for each member variable of the class.
iii. A delegate method AnnounceTime is defined that has a parameter of type int and return type void.
iv. The class also declares the instance of delegate i.e. AnnounceTime Timer.
v. The Subscribe method which contains the parameter of type AnnounceTime. It uses to increments Timer each time the subscriber method is called. The logic of public Hour attribute is


GMTTimer class:


b. Write a class Mobile with private fields _hour, _minutes, and _seconds.
Write public attributes for class fields.

i. Write a public method UpdateMyTime(int hours) which prints hours, minutes and seconds. ii. Write a public method SubscribeToGMTTimer() which internally calls Subscribe methods and passes UpdateMyTime as a parameter. c. Also, write class MaxWatch and SamsungGear and repeat the logic of step b. d. Write the main class where you create the instance of GMTTimer, Mobile, MaxWatch and SamsungGear. i. Call SubscribeToGMTTimer() method from an instance of Mobile, MaxWatch, and SamsungGear. ii. Change the hour by the instance of GMTTimer and view the output.

Mobile class:


MaxWatch class:


SamsungGear class:


Main Program:



delegate output
delegate output