区别对待观察者场景
import java.util.Observable; import java.util.Observer; public class ConcreateObserver implements Observer{ private String observerName; @Override public void update(Observable o, Object arg) { String weather = (String)arg; if(observerName.equals("女朋友")){ if(weather.equals("下雨")){ System.out.println("女朋友呆在家里"); } } if(observerName.equals("妈妈")){ if(weather.equals("下雨") || weather.equals("下雪")){ System.out.println("妈妈呆在家里"); } } } public void setObserverName(String observerName) { this.observerName = observerName; } }
这样不是也可以达到分情况吗,为什么还要重新定义Observer接口?