import java.util.ArrayList;
interface Watched{
public void addWatcher(Watcher watcher);
public void removeWatcher(Watcher watcher);
public void notifyWatchers(String str);
}
interface Watcher{
public void update(String str);
}
class House implements Watched{
ArrayList<Watcher> list =new ArrayList<Watcher>();
public void addWatcher(Watcher watcher){
list.add(watcher);
}
public void removeWatcher(Watcher watcher){
list.remove(watcher);
}
public void notifyWatchers(String str){
for(Watcher w :list){
w.update(str);
}
}
}
class Person implements Watcher{
private String name;
public Person(String name){
this.name=name;
}
public void update(String str){
System.out.println (name+"说"+str);
}
}
public class ObserverDemo02{
public static void main(String[]args){
Watched house=new House();
Watcher person1=new Person("张三");
Watcher person2=new Person("李四");
Watcher person3=new Person("王五");
house.addWatcher(person1);
house.addWatcher(person2);
house.addWatcher(person3);
house.notifyWatchers("房价上涨了......");
}
}
interface Watched{
public void addWatcher(Watcher watcher);
public void removeWatcher(Watcher watcher);
public void notifyWatchers(String str);
}
interface Watcher{
public void update(String str);
}
class House implements Watched{
ArrayList<Watcher> list =new ArrayList<Watcher>();
public void addWatcher(Watcher watcher){
list.add(watcher);
}
public void removeWatcher(Watcher watcher){
list.remove(watcher);
}
public void notifyWatchers(String str){
for(Watcher w :list){
w.update(str);
}
}
}
class Person implements Watcher{
private String name;
public Person(String name){
this.name=name;
}
public void update(String str){
System.out.println (name+"说"+str);
}
}
public class ObserverDemo02{
public static void main(String[]args){
Watched house=new House();
Watcher person1=new Person("张三");
Watcher person2=new Person("李四");
Watcher person3=new Person("王五");
house.addWatcher(person1);
house.addWatcher(person2);
house.addWatcher(person3);
house.notifyWatchers("房价上涨了......");
}
}