To implement the Observer Pattern for a Time class in Java, we'll create a system where the Time class notifies its observers whenever the time changes. Here's how you might do it:
-
Define an Observer Interface: This interface will define the method that all observers must implement to receive updates.
public interface TimeObserver {
void updateTime(int hour, int minute, int second);
}
-
Create the Subject (Time Class): The Time class will be the subject that maintains a list of observers and notifies them of state changes.
import java.util.ArrayList;
import java.util.List;
public class Time {
private int hour;
private int minute;
private int second;
private List<TimeObserver> observers = new ArrayList<>();
public Time(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void attach(TimeObserver observer) {
observers.add(observer);
}
public void detach(TimeObserver observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (TimeObserver observer : observers) {
observer.updateTime(hour, minute, second);
}
}
// Method to update time and notify observers
public void setTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
notifyObservers();
}
// Getters for time
public int getHour() { return hour; }
public int getMinute() { return minute; }
public int getSecond() { return second; }
// Example method to simulate time passing
public void tick() {
second++;
if (second == 60) {
second = 0;
minute++;
if (minute == 60) {
minute = 0;
hour = (hour + 1) % 24;
}
}
notifyObservers();
}
}
-
Implement Concrete Observers: These classes will implement the TimeObserver interface to react to time updates.
public class DigitalClock implements TimeObserver {
@Override
public void updateTime(int hour, int minute, int second) {
System.out.println(String.format("Digital Clock: %02d:%02d:%02d", hour, minute, second));
}
}
public class AnalogClock implements TimeObserver {
@Override
public void updateTime(int hour, int minute, int second) {
System.out.println(String.format("Analog Clock: Hour hand at %d, Minute hand at %d", hour, minute));
}
}
-
Usage Example:
public class Main {
public static void main(String[] args) {
Time time = new Time(15, 30, 0);
DigitalClock digital = new DigitalClock();
AnalogClock analog = new AnalogClock();
time.attach(digital);
time.attach(analog);
for (int i = 0; i < 5; i++) {
time.tick(); // This will update time and notify observers
try {
Thread.sleep(1000); // Wait for a second to simulate real time
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
This setup allows the Time class to manage a list of observers, update them whenever the time changes, and provides a simple way to simulate time passing. Each observer can react differently to the time change, making the system flexible for various uses. Remember, this example uses Thread.sleep() for demonstration; in a real-world scenario, you might tie this to an actual system clock or timer.