主程序调用处理线程,然后线程处理完,又要调用主程序的方法进行结束,感觉处理线程调用主程序方法进行,又返回到原程序,感觉有点关联性太强,不易于模块话,所以想到利用Observer的机制,将两个关联起来,这样处理线程就不用呼叫主程序。
[@more@] 最近公司有个项目:主程序调用处理线程,然后线程处理完,又要调用主程序的方法进行结束,感觉处理线程调用主程序方法进行,又返回到原程序,感觉有点关联性太强,不易于模块话,所以想到利用Observer的机制,将两个关联起来,这样处理线程就不用呼叫主程序。
由于程序大体已经完成,所以我不可能去修改处理线程的extends关系,所以我想自己包装一下Observer,具 体代码如下:
1. BaseObserver 类:
import java.util.Observer;
import java.util.Observable;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Method;
public class BaseObserver implements Observer {
private String methodName = "";
private Class list = null;
//设置需要激发的类
public void setObserverObject(Class observer) {
if (list == null)
list = observer;
}
//设置需要激发的方法名
public void setObserverMethod(String methodName) {
this.methodName = methodName;
}
public void update(Observable obj, Object arg) {
if (obj instanceof BaseObservable) {
try {
Method method = list.getClass().getDeclaredMethod(methodName);
……………………
……………………
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
}
}
}
2.BaseObservable 类
import java.util.Observable;
import java.lang.reflect.Method;
import java.lang.NoSuchMethodException;
public class BaseObservable extends Observable {
private Class className = null;
private String methodName = "";
private Class[] parameterTypes ;
//设置被观察者名 和 方法名
public void setObserveable(Class className, String methodName)
throws NoSuchMethodException, SecurityException{
this.className = className;
this.methodName = methodName;
}
//设置被观察者名 和 方法名 及 参数
public void setObserveable(Class className, String methodName,Class[] arameterTypes)
throws NoSuchMethodException, SecurityException
{
this.className = className;
this.methodName = methodName;
this.parameterTypes = new Class[parameterTypes.length];
for(int i =0;i this.parameterTypes[i] = parameterTypes[i];
}
}
//被观察者的观察方法
public void doNotifyObserver()
throws NoSuchMethodException, SecurityException{
setChanged();
if(parameterTypes==null)
notifyObservers(className.getMethod(methodName));
else
notifyObservers(className.getMethod(methodName,parameterTypes));
}
}
3. Product 类:一般类
public class Product extends Observable {
private String name;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
4. Action 类:一般类
public class Action {
public void doAction(String str){
System.out.println(str);
System.out.println("action");
}
}
以上是所以得类,现在我想处理如下:
当Product 的 getName 被调用时,处理 Action 的 doAction 方法:
Product product = new Product();
Action action = new Action();
BaseObservable observable = new BaseObservable();
BaseObserver observer = new BaseObserver();
try{
//设置观察者
observer.setObserverObject(Action.class);
observer.setObserverMethod("doAction");
//观察者放入触发者内
observable.addObserver(observer);
//设置触发者
observable.setObserveable(Product.class, "getName");
//活动
product.setName("name");
product.getName();
//最后掉用 (不好的地方,感觉要改进)
observable.doNotifyObserver();
}catch(NoSuchMethodException e){
…………
…………
}catch(SecurityException e){
…………
…………
}
以上是我的所有思路,但是在实际中不能运行,不知道怎么回事,感觉原理上还是有问题,
小弟是初学设计模式,希望大家指正,不慎感激。
欢迎扔鸡蛋,丢石头,>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9879276/viewspace-983064/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9879276/viewspace-983064/