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:
Create an interface called AlarmListener. This is the observer interface. In AlarmListener, there is a void alarm method defined.
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.
In SensorSystem, define a method void registerAlarmListener alarmListener What it does is to add alarmListener to the ArrayList listeners.
In SensorSystem, define another method void soundTheAlarm What it does is to use a for loop to loop through all the listenersobservers in the ArrayList listeners, and call their alarm method.
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:".
Use the following client code to try it
public class ObservserDemo
public static void mainString args
SensorSystem sensorSystem new SensorSystem;
sensorSystemregisternew Gatesl;
sensorSystemregisternew Lightingl;
sensorSystemregisternew Surveillance;
sensorSystemsoundTheAlarm;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
