代码实现:
步骤一:创建一个接口
Car.java
package FactoryMode;
public interface Car {
public void run();
}
步骤二:创建实现类
BaoMaCar.java
package FactoryMode;
public class BaoMaCar implements Car {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("这是宝马车的run方法");
}
}
AoDiCar.java
package FactoryMode;
public class AoDiCar implements Car {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("这是奥迪车的run方法");
}
}
步骤三:创建一个工厂类
CarFactory.java
package FactoryMode;
public class CarFactory {
public Car getCar(String carName) {
switch(carName) {
case "奥迪":
return new AoDiCar();
case "宝马":
return new BaoMaCar();
default:
System.out.println("该车厂不提供该品牌的车");
return null;
}
}
}
步骤四:测试
test.java
package FactoryMode;
public class Test {
public static void main(String[] args) {
CarFactory cf = new CarFactory();
//获取宝马品牌的车
Car BaoMa=cf.getCar("宝马");
BaoMa.run();
//获取奥迪品牌的车
Car AoDi = cf.getCar("奥迪");
AoDi.run();
Car BenChi=cf.getCar("奔驰");
System.out.println(BenChi);
}
}
静态代理在使用时,需要定义接口或者父类,被代理对象与代理对象一起实现相同的接口或者是继承相同父类
步骤一:创建共同的接口
IPerson.java
package StaticAgencyMode;
public interface IPerson {
public void study();
}
步骤二:创建代理对象的类
ProxyPerson.java
package StaticAgencyMode;
public class ProxyPerson implements IPerson {
private IPerson person;
public ProxyPerson(IPerson person) {
this.person = person;
}
@Override
public void study() {
// TODO Auto-generated method stub
System.out.println("这是代理person的study方法");
this.person.study();
}
}
步骤三:创建被代理对象的类
Person.java
package StaticAgencyMode;
public class Person implements IPerson {
@Override
public void study() {
// TODO Auto-generated method stub
System.out.println("这是person的study方法");
}
}
步骤四:创建测试类
Test.java
package StaticAgencyMode;
public class Test {
public static void main(String[] args) {
Person p = new Person();
ProxyPerson pp = new ProxyPerson(p);
pp.study();
}
}
代理类是jdk中自带的类,利用反射技术,动态生成代理对象
动态代理有以下特点:
1.代理对象,不需要实现接口
2.代理对象的生成,是利用JDK的API,动态的在内存中构建代理对象(需要我们指定创建代理对象/目标对象实现的接口的类型)
3.动态代理也叫做:JDK代理,接口代理
JDK中生成代理对象的API
代理类所在包:java.lang.reflect.Proxy
代码实现
步骤一:被代理对象的接口
Person.java
package DynamicAgencyMode;
//被代理对象的接口
public interface Person {
public void giveMoney();
}
步骤二:被代理对象的类
Student.java
package DynamicAgencyMode;
//被代理对象的类
public class Student implements Person{
private String name;
public Student(String name) {
super();
this.name = name;
}
@Override
public void giveMoney() {
// TODO Auto-generated method stub
System.out.println(name+" 上交班费100元");
}
}
步骤三:代理对象的类,实现了InvocationHandler接口
StuInvocationHandler.java
package DynamicAgencyMode;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
//代理对象的类
public class StuInvocationHandler implements InvocationHandler {
//invocationHandler持有的被代理对象
private Object target;
public StuInvocationHandler(Object target) {
super();
this.target = target;
}
/**
* proxy:代表动态代理对象
* method:代表正在执行的方法
* args:代表调用目标方法时传入的实参
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object ProxyStu = method.invoke(target,args);
// TODO Auto-generated method stub
return ProxyStu;
}
}
步骤四:测试类
Test.java
package DynamicAgencyMode;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String[] args) {
//创建一个实例对象
Person stu = new Student("小刘");
//创建一个与代理对象相关联的InvocationHandler
InvocationHandler stuHandler = new StuInvocationHandler(stu);
//创建一个代理对象stuProxy来代理stu,代理对象的每个执行方法都会替换执行Invocation的invoke方法
Person ProxyStu = (Person)Proxy.newProxyInstance(Person.class.getClassLoader(), new Class<?>[]{Person.class}, stuHandler);
ProxyStu.giveMoney();
}
}