反射
/**
* 反射
*/
public class Test {
public static ReflectServiceImpl getInstance(){
ReflectServiceImpl obj = null;
try {
//getConstructor 获取构造函数
//newInstance 实例化
obj = (ReflectServiceImpl) Class.forName("proxy.demo.ReflectServiceImpl").getConstructor(String.class).newInstance("张三");
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static void main(String[] args){
ReflectServiceImpl instance = getInstance();
instance.sayHello();
}
}
动态代理:略
- jdk implements InvocationHandler
- cglib implements MethodInterceptor
jdk动态代理实现拦截器
public interface Interceptor {
public boolean before(Object proxy, Object target, Method method, Object[] args);
public void around(Object proxy, Object target, Method method, Object[] args);
public void after(Object proxy, Object target, Method method, Object[] args);
}
public class MyInterceptor implements Interceptor {
@Override
public boolean before(Object proxy, Object target, Method method, Object[] args) {
System.out.println("反射before...");
return true;//返回true执行原有方法
}
@Override
public void around(Object proxy, Object target, Method method, Object[] args) {
System.out.println("不执行原有方法,执行代理方法");
}
@Override
public void after(Object proxy, Object target, Method method, Object[] args) {
System.out.println("反射after...");
}
}
public class InterceptorJdkProxy implements InvocationHandler {
private Object target;
private String interceptorClass = null;
public InterceptorJdkProxy(Object target,String interceptorClass){
this.target = target;
this.interceptorClass = interceptorClass;
}
public static Object getInstance(Object target,String interceptorClass){
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InterceptorJdkProxy(target,interceptorClass));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (interceptorClass==null){
//没有拦截器,执行原有方法
return method.invoke(target,args);
}
Object result = null;
Interceptor interceptor = (Interceptor) Class.forName(interceptorClass).newInstance();
if (interceptor.before(proxy,target,method,args)){
//执行原有方法
result = method.invoke(target,args);
}else {
//执行拦截方法
interceptor.around(proxy,target,method,args);
}
interceptor.after(proxy,target,method,args);
return result;
}
}
public class Test {
public static void main(String[] args){
Inter inter = (Inter) InterceptorJdkProxy.getInstance(new Boss(),"proxy.Interceptor.MyInterceptor");
inter.findPerson();
}
}
观察者模式实例:通过java.util.Observer实现
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
public class ProductList extends Observable {
private List<String> productList = null;
private static ProductList instance;
private ProductList(){}
public static ProductList getInstance(){
if (instance == null){
instance = new ProductList();
instance.productList = new ArrayList<>();
}
return instance;
}
public void addProductListObserver(Observer observer){
this.addObserver(observer);
}
public void addProduct(String newProduct){
productList.add(newProduct);
System.out.println("新增产品"+newProduct);
this.setChanged();//设置被观察对象发生变化
this.notifyObservers(newProduct);//通知观察者
}
}
import java.util.Observable;
import java.util.Observer;
public class Observer1 implements Observer {
@Override
public void update(Observable o, Object arg) {
String newProduct = (String) arg;
System.out.println("有新产品了:"+newProduct);
}
}
public class Test {
public static void main(String[] args){
ProductList instance = ProductList.getInstance();
Observer1 observer1 = new Observer1();
Observer2 observer2 = new Observer2();
instance.addProductListObserver(observer1);
instance.addProductListObserver(observer2);
instance.addProduct("新增xxx");
}
}