Question: Observer pattern steps: 1 . Create an interface called AlarmListener. This is the observer interface. In AlarmListener, there is a void alarm ( ) method

Observer pattern steps:
1. Create an interface called AlarmListener. This is the observer interface. In AlarmListener, there is a void alarm() method defined.
2. Create a class called SensorSystem. This is the publisher class. Define one instance variable
ArrayList AlarmListener > listeners = new ArrayList(); This ArrayList saves all the observers of this publisher.
3. In SensorSystem, define a method void register(AlarmListener alarmListener). What it does is to add alarmListener to the ArrayList listeners.
4. In SensorSystem, define another method void soundTheAlarm(). What it does is to use a for loop to loop through all the listeners/observers in the ArrayList listeners, and call their alarm() method.
5. Different three concrete observer classes: Lighting, Gates, and Surveillance. Make them implement the interface AlarmListener. Implement the alarm() method in all three classes. In Lighting, alarm() prints out "lights up". In Gates, alarm() prints out "gates close". In Surveillance, alarm() prints out "Surveillance - by the numbers:".
6. Use the following client code to try it.
```
public class ObservserDemo {
public static void main(String[] args ){
SensorSystem sensorSystem = new SensorSystem();
sensorSystem_register(new Gates[l);
sensorSystem_register(new Lighting(l);
sensorSystem_register(new Surveillance());
sensorSystem_soundTheAlarm();
}
}
```
Observer pattern steps: 1 . Create an interface

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!