Development

Java의 관찰자 설계 패턴 :이 기사에서는 Observer Design 패턴을 설명 할 수 있습니다.이 기사는 객체 간의 일대일 종속성을 정의하여 한 객체가 상태를 변경하면 모든 부양 가족이 자동으로 알리..

sonpro 2023. 3. 11. 01:36
반응형

Observer Design Pattern

Observer Design Pattern in Java

Summary

The Observer design pattern is a behavioral pattern that defines a one-to-many dependency between objects. It allows one object to notify other objects automatically when its state changes. In this article, we will explore the Observer design pattern in Java and its implementation using code examples.

Introduction

The Observer design pattern is a widely used pattern in software development. It is used to establish a communication channel between objects, where one object notifies other objects about its state changes. This pattern is useful in situations where multiple objects need to be updated when a particular object changes its state.

Implementation

The Observer design pattern consists of two main components: the Subject and the Observer. The Subject is the object that is being observed, and the Observer is the object that is notified when the Subject's state changes.

Subject

The Subject is the object that is being observed. It maintains a list of its Observers and provides methods to add and remove Observers from the list. When the Subject's state changes, it notifies all its Observers by calling a method on each Observer.

public interface Subject {
    public void registerObserver(Observer observer);
    public void removeObserver(Observer observer);
    public void notifyObservers();
}

Observer

The Observer is the object that is notified when the Subject's state changes. It has a method that is called by the Subject when its state changes.

public interface Observer {
    public void update();
}

Concrete Subject

A Concrete Subject is a class that implements the Subject interface. It maintains its state and notifies its Observers when its state changes.

public class ConcreteSubject implements Subject {
    private List<Observer> observers = new ArrayList<Observer>();
    private int state;

    public void setState(int state) {
        this.state = state;
        notifyObservers();
    }

    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}

Concrete Observer

A Concrete Observer is a class that implements the Observer interface. It is notified by the Subject when its state changes.

public class ConcreteObserver implements Observer {
    private int state;
    private Subject subject;

    public ConcreteObserver(Subject subject) {
        this.subject = subject;
        subject.registerObserver(this);
    }

    public void update() {
        this.state = subject.getState();
    }
}

Conclusion

The Observer design pattern is a powerful pattern that allows objects to communicate with each other in a decoupled manner. It is widely used in software development to implement event-driven systems. In this article, we explored the Observer design pattern in Java and its implementation using code examples. We hope this article has been helpful in understanding the Observer design pattern.

반응형