public class BookService {
public void buy(){
System.out.println("购书业务...");
}
}
public class SBookService extends BookService{
public void buy(){
System.out.println("购书前业务...");//增加功能
super.buy();//使用父类业务功能
System.out.println("购书完成...");
}
}

package com.user.cailinhao.service;
/**
* 业务接口
*/
public interface Service {
//规定唱歌的业务功能
void sing();
}
package com.user.cailinhao.service.impl;
import com.user.cailinhao.service.Service;
/**
* 刘德华:目标对象1
*/
public class SuperStarLiu implements Service {
@Override
public void sing() {
System.out.println("我是刘德华,我正在表演");
}
}
package com.user.cailinhao.service.impl;
import com.user.cailinhao.service.Service;
/**
* 周润发:目标对象2
*/
public class SuperStarZhou implements Service {
@Override
public void sing() {
System.out.println("我是周润发,我正在表演");
}
}
package com.user.cailinhao.service.impl;
import com.user.cailinhao.service.Service;
/**
* 助理:代理对象
*/
public class Agent implements Service {
//面向接口编程,类中的成员变量设计为接口
public Service target;
//面向接口编程,方法的参数设计为接口,传入目标对象,完成代理功能
public Agent(Service target) {
this.target = target;
}
@Override
public void sing() {
System.out.println("预定时间");
System.out.println("预定场地");
//重点:业务功能必须由目标对象亲自实现
/*new SuperStarLiu().sing();这样就写死了,不灵活*/
//请谁谁唱,很灵活
target.sing();
System.out.println("结算费用");
}
}
package com.user.cailinhao.test;
import com.user.cailinhao.service.Service;
import com.user.cailinhao.service.impl.Agent;
import com.user.cailinhao.service.impl.SuperStarLiu;
import org.junit.Test;
public class MyTest {
@Test
public void testAgent(){
/*
Service agent = new Agent();
agent.sing();
*/
}
public void testAgent1(){
//面向客户,具体谁来唱由客户选择
Service agent = new Agent(new SuperStarLiu());
agent.sing();
}
}
public static Object newProxyInstance(ClassLoader lodaer,//类加载器,完成目标对象的加载
Class<?>[] interfaces,//目标对象实现的所有接口
InvocationHandler h,//类似于静态代理的Agent功能,代理的功能和目标对象功能的调用在这里
) throws IllegalArgumentException
{.......}
Method类:
反射机制,用来进行目标对象方法的反射调用。
InvocationHandler接口:
它是实现代理和业务功能的,我们在调用时使用匿名内部类实现。
package com.user.cailinhao.proxy;
import com.user.cailinhao.service.Service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 代理工厂用于动态生成代理对象Agent,通过传入target参数生成一个专门为target目标对象代理的Agent,通过实现接口的invoke方法实现代理对象Agent的额外功能
*/
public class ProxyFactory {
//面向接口编程,类中的成员变量设计为接口
Service target;
//面向接口编程,方法的参数设计为接口,传入目标对象,完成代理功能
public ProxyFactory(Service target) {
this.target = target;
}
//返回动态代理对象
public Object getAgent(){
return Proxy.newProxyInstance(//获取动态代理对象用这个方法
target.getClass().getClassLoader(),//类加载器,在JVM中完成目标对象的加载
target.getClass().getInterfaces(),//目标对象实现的所有接口,为了获取所有接口中的方法
new InvocationHandler() {//这是一个实现代理功能的接口,类似于静态代理的Agent功能,代理的功能和目标对象功能的调用在这里,采用匿名内部类实现
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//返回:目标对象方法的返回值
//proxy是创建好的代理对象
//method是目标对象的某个方法,可能是sing()、show()
//args是目标对象的方法的参数
//代理功能
System.out.println("预定时间");
System.out.println("预定场地");
//目标对象的功能
//target.sing();这么些不行,还是写死了,等同静态代理
Object obj = method.invoke(target,args);//反射机制给方法传参数,调用target对象的invoke方法并传参数args,来的演员想唱歌就唱歌,想跳舞就跳舞
//代理功能
System.out.println("结算费用");
return obj;//目标对象方法的返回值
}
}
);
}
}
package com.user.cailinhao.service;
/**
* 业务接口
*/
public interface Service {
void sing();
void show();
}
package com.user.cailinhao.service.impl;
import com.user.cailinhao.service.Service;
/**
* 目标对象1:刘德华
*/
public class SuperStarLiu implements Service {
@Override
public void sing() {
System.out.println("我是刘德华,我在唱歌");
}
@Override
public void show() {
System.out.println("我是刘德华,我在表演");
}
}
package com.user.cailinhao.service.impl;
import com.user.cailinhao.service.Service;
/**
* 目标对象2:周润发
*/
public class SuperStarZhou implements Service {
@Override
public void sing() {
System.out.println("我是周润发,我在唱歌");
}
@Override
public void show() {
System.out.println("我是周润发,我在表演");
}
}
package com.user.cailinhao.test;
import com.user.cailinhao.proxy.ProxyFactory;
import com.user.cailinhao.service.Service;
import com.user.cailinhao.service.impl.SuperStarLiu;
import org.junit.Test;
public class MyTest {
@Test
public void testJDK(){
ProxyFactory factory = new ProxyFactory(new SuperStarLiu());//目标对象创建代理对象
Service agent = (Service) factory.getAgent();//!!!!!强转成Service接口才能调用sing方法
agent.sing();
}
@Test
public void testJDK1(){
ProxyFactory factory = new ProxyFactory(new SuperStarLiu());//目标对象创建代理对象
Service agent = (Service) factory.getAgent();//!!!!!强转成Service接口才能调用sing方法
agent.show();
}
@Test
public void testJDK2(){
ProxyFactory factory = new ProxyFactory(new SuperStarLiu());//目标对象创建代理对象
Service agent = (Service) factory.getAgent();//!!!!!强转成Service接口才能调用sing方法
System.out.println(agent.getClass());//class jdk.proxy2.$Proxy4,这是一个JDK动态代理对象的类型
}
}
package com.cailinhao.service;
/**
* 业务接口
*/
public interface Service {
void sing();
}
package com.cailinhao.service.impl;
import com.cailinhao.service.Service;
/**
* 目标对象1:刘德华
*/
public class SuperStarLiu implements Service {
@Override
public void sing() {
System.out.println("我是刘德华,我在唱歌");
}
}
package com.cailinhao.service.sub;
import com.cailinhao.service.impl.SuperStarLiu;
/**
* 代理对象
*/
public class SubSuperStarLiu extends SuperStarLiu {
@Override
public void sing() {
System.out.println("预定时间");
System.out.println("预定地点");
super.sing();
System.out.println("结算费用");
}
}
package com.cailinhao;
import com.cailinhao.service.impl.SuperStarLiu;
import org.junit.Test;
public class MyTest {
@Test
public void testAgent(){
SuperStarLiu liu = new SuperStarLiu();
liu.sing();
}
}