平台中需要实现一个统一管理的功能
实现方式1:
平台中定义类 CommonQuery.java ,且有Map<key,Object> 如 put(1,new Query1())
个子类都注册 CommonQuery
在平台中执行的时候 ,会根据Map的值遍历 ,然后调用个子系统的方法
实现方式2:
采用观察者模式
示例代码如下:
QueryMain.java
package test02; import java.util.ArrayList; import java.util.List; import java.util.Observable; public class QueryMain extends Observable { private static QueryMain instance; public static QueryMain getInstance() { if (instance == null) { instance = new QueryMain(); } return instance; } private QueryMain() { } private String starTime; /** * 查询的结果 */ private List<String> listAll = new ArrayList<String>(); /** * * 查询的参数 * * @param startTime * @author * @date 2013-1-11 上午06:14:20 */ public void doQuery(String startTime) { this.starTime = startTime; this.listAll = new ArrayList<String>(); this.setChanged(); this.notifyObservers(); } /** * @return the starTime */ public String getStarTime() { return starTime; } /** * @param starTime * the starTime to set */ public void setStarTime(String starTime) { this.starTime = starTime; } /** * @return the listAll */ public List<String> getListAll() { return listAll; } /** * @param listAll * the listAll to set */ public void setListAll(List<String> listAll) { this.listAll = listAll; } }
AbsQuery.java
package test02; import java.util.Observable; import java.util.Observer; public abstract class AbsQuery extends Observable implements Observer { @Override public void update(Observable o, Object arg) { doQuery((QueryMain) o); } public abstract void doQuery(QueryMain queryMain); }
Query01.java
package test02; public class Query01 extends AbsQuery { @Override public void doQuery(QueryMain queryMain) { // System.out.println("Query01 doQuery :" + queryMain.getStarTime()); queryMain.getListAll().add("Query01....1"); queryMain.getListAll().add("Query01....2"); queryMain.getListAll().add("Query01....3"); } }
Query02.java
package test02; public class Query02 extends AbsQuery { @Override public void doQuery(QueryMain queryMain) { // System.out.println("Query02 doQuery :" + queryMain.getStarTime()); queryMain.getListAll().add("Query02....1"); queryMain.getListAll().add("Query02....2"); queryMain.getListAll().add("Query02....3"); } }
Test.java
package test02; public class Test { public static void main(String[] args) { QueryMain queryMain = QueryMain.getInstance(); Query01 query01 = new Query01(); Query02 query02 = new Query02(); queryMain.addObserver(query01); queryMain.addObserver(query02); QueryMain.getInstance().doQuery("2013"); System.out.println("-------------"); for (String s : queryMain.getListAll()) { System.out.println(s); } QueryMain.getInstance().doQuery("2014"); System.out.println("-------------"); for (String s : queryMain.getListAll()) { System.out.println(s); } } }