관찰자 패턴
14731 단어 behavioraljavadesignpatternspattern
참가자들
암호
public class Main {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.attach(new ConcreteObserver(s, "X"));
s.attach(new ConcreteObserver(s, "Y"));
s.attach(new ConcreteObserver(s, "Z"));
s.setSubjectState("ABC");
s.notifyObservers();
}
}
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer o : observers) {
o.update();
}
}
}
public class ConcreteSubject extends Subject {
private String subjectState;
public String getSubjectState() {
return subjectState;
}
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
}
}
public interface Observer {
void update();
}
public class ConcreteObserver implements Observer {
private String name;
private ConcreteSubject subject;
public ConcreteObserver(ConcreteSubject subject, String name) {
this.subject = subject;
this.name = name;
}
@Override
public void update() {
String observerState = subject.getSubjectState();
System.out.println("Observer " + name + " state is " + observerState);
}
}
산출
Observer X state is ABC
Observer Y state is ABC
Observer Z state is ABC
자바 빌트인 옵저버
public class Main {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.addObserver(new ConcreteObserver(s, "X"));
s.addObserver(new ConcreteObserver(s, "Y"));
s.addObserver(new ConcreteObserver(s, "Z"));
s.setSubjectState("ABC");
s.notifyObservers();
}
}
import java.util.Observable;
public class ConcreteSubject extends Observable {
private String subjectState;
public String getSubjectState() {
return subjectState;
}
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
setChanged();
}
}
import java.util.Observable;
import java.util.Observer;
public class ConcreteObserver implements Observer {
Observable subject;
private String name;
public ConcreteObserver(Observable subject, String name) {
this.subject = subject;
this.name = name;
}
@Override
public void update(Observable obs, Object arg) {
if(obs instanceof ConcreteSubject) {
ConcreteSubject observable = (ConcreteSubject)obs;
System.out.println("Observer " + name + " state is " + observable.getSubjectState());
}
}
}
eidherjulian61
/
디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(관찰자 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/eidher/observer-pattern-b18
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
public class Main {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.attach(new ConcreteObserver(s, "X"));
s.attach(new ConcreteObserver(s, "Y"));
s.attach(new ConcreteObserver(s, "Z"));
s.setSubjectState("ABC");
s.notifyObservers();
}
}
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer o : observers) {
o.update();
}
}
}
public class ConcreteSubject extends Subject {
private String subjectState;
public String getSubjectState() {
return subjectState;
}
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
}
}
public interface Observer {
void update();
}
public class ConcreteObserver implements Observer {
private String name;
private ConcreteSubject subject;
public ConcreteObserver(ConcreteSubject subject, String name) {
this.subject = subject;
this.name = name;
}
@Override
public void update() {
String observerState = subject.getSubjectState();
System.out.println("Observer " + name + " state is " + observerState);
}
}
Observer X state is ABC
Observer Y state is ABC
Observer Z state is ABC
자바 빌트인 옵저버
public class Main {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.addObserver(new ConcreteObserver(s, "X"));
s.addObserver(new ConcreteObserver(s, "Y"));
s.addObserver(new ConcreteObserver(s, "Z"));
s.setSubjectState("ABC");
s.notifyObservers();
}
}
import java.util.Observable;
public class ConcreteSubject extends Observable {
private String subjectState;
public String getSubjectState() {
return subjectState;
}
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
setChanged();
}
}
import java.util.Observable;
import java.util.Observer;
public class ConcreteObserver implements Observer {
Observable subject;
private String name;
public ConcreteObserver(Observable subject, String name) {
this.subject = subject;
this.name = name;
}
@Override
public void update(Observable obs, Object arg) {
if(obs instanceof ConcreteSubject) {
ConcreteSubject observable = (ConcreteSubject)obs;
System.out.println("Observer " + name + " state is " + observable.getSubjectState());
}
}
}
eidherjulian61
/
디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(관찰자 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/eidher/observer-pattern-b18
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
public class Main {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.addObserver(new ConcreteObserver(s, "X"));
s.addObserver(new ConcreteObserver(s, "Y"));
s.addObserver(new ConcreteObserver(s, "Z"));
s.setSubjectState("ABC");
s.notifyObservers();
}
}
import java.util.Observable;
public class ConcreteSubject extends Observable {
private String subjectState;
public String getSubjectState() {
return subjectState;
}
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
setChanged();
}
}
import java.util.Observable;
import java.util.Observer;
public class ConcreteObserver implements Observer {
Observable subject;
private String name;
public ConcreteObserver(Observable subject, String name) {
this.subject = subject;
this.name = name;
}
@Override
public void update(Observable obs, Object arg) {
if(obs instanceof ConcreteSubject) {
ConcreteSubject observable = (ConcreteSubject)obs;
System.out.println("Observer " + name + " state is " + observable.getSubjectState());
}
}
}
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(관찰자 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/eidher/observer-pattern-b18텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)