总体设计
包com.xzit.aop
Dynlnvoke
ProducerProxy
包com.xzit.db
db
包com.xzit.entity
Client
Department
Employee
Goods
Producer
包com.xzit.interfaces
DepartmentDaoImp
EmployeeDaoImp
IGoods
IObject
包com.xzit.proxy
TeatDynProxy
TestStaticProxy
咱直接上代码,附一张图,如果看到这里,我希望你看完,我是歌谣,期待一起共同进步
工具:IDEA
package:aop
Dynlnvoke类
package com.xzit.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DynInvoke implements InvocationHandler {
private Object target; // 被代理的对象
/**
* 重写实现动态代理的invoke方法
*/
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
System.out.println("动态代理方法");
Object obj = arg1.invoke(target, arg2);
return obj;
}
public void setTarget(Object target) {
this.target = target;
}
}
ProducerProxy类
package com.xzit.aop;
import java.util.List;
import com.xzit.entity.Client;
import com.xzit.entity.Goods;
import com.xzit.entity.Producer;
import com.xzit.interfaces.IGoods;
public class ProducerProxy implements IGoods {
private Producer producer; // 被代理对象
private Client client;// 购买对象
public List<Goods> produerGoodsList() {
boolean bool = validateIsVip();
if(bool){
List<Goods> goodsList = producer.produerGoodsList();
for(Goods g:goodsList){
g.setPrice(g.getPrice()*0.85);
}
return goodsList;
}
return producer.produerGoodsList();
}
public void setProducer(Producer producer) {
this.producer = producer;
}
public void setClient(Client client) {
this.client = client;
}
private boolean validateIsVip(){
if(client.isVip())
return true;
return false;
}
}
package:db
db类
package com.xzit.db;
import java.util.ArrayList;
import java.util.List;
import com.xzit.entity.Department;
import com.xzit.entity.Employee;
import com.xzit.entity.Goods;
public class DataBase {
public static final List<Department> DEPLIST = new ArrayList<Department>();
public static final List<Employee> EMPLIST = new ArrayList<Employee>();
public static final List<Goods> GOODSLIST = new ArrayList<Goods>();
static{
DEPLIST.add(new Department("dep100","事业部"));
DEPLIST.add(new Department("dep200","策划部"));
DEPLIST.add(new Department("dep300","市场部"));
DEPLIST.add(new Department("dep400","人事部"));
DEPLIST.add(new Department("dep500","研发部"));
DEPLIST.add(new Department("dep600","测试部"));
EMPLIST.add(new Employee("任我行","男","黑木崖"));
EMPLIST.add(new Employee("任盈盈","女","黑木崖"));
EMPLIST.add(new Employee("令狐冲","男","华山"));
EMPLIST.add(new Employee("岳不群","男","华山"));
EMPLIST.add(new Employee("丁春秋","男","南海"));
GOODSLIST.add(new Goods("goods100","加热器",123.50));
GOODSLIST.add(new Goods("goods200","胜利羽毛球lark4",54.00));
GOODSLIST.add(new Goods("goods300","联想笔记本T450",8900.50));
GOODSLIST.add(new Goods("goods400","小米麦克风",99.50));
}
}
package:entity
Client类
package com.xzit.entity;
/**
* 客户实体类
* @author Administrator
*
*/
public class Client {
private String name; // 客户姓名
private boolean isVip; // 是否是VIP客户
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isVip() {
return isVip;
}
public void setVip(boolean isVip) {
this.isVip = isVip;
}
}
Department类
package com.xzit.entity;
/**
* 部门信息实体类
* @author Administrator
*
*/
public class Department {
private String number;// 部门编号
private String name;// 部门名称
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department() {
}
public Department(String number, String name) {
this.number = number;
this.name = name;
}
}
Emploee类
package com.xzit.entity;
/**
* 员工实体类
* @author Administrator
*
*/
public class Employee {
private String name;
private String sex;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/**
* 无参构造器
*/
public Employee() {
}
/**
* 有参构造器
* @param name
* @param sex
* @param address
*/
public Employee(String name, String sex, String address) {
this.name = name;
this.sex = sex;
this.address = address;
}
}
Goods类
package com.xzit.entity;
/**
* 商品实体类
* @author Administrator
*
*/
public class Goods {
private String number;// 商品编号
private String name;// 商品名称
private Double price; // 商品单价
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Goods() {
}
public Goods(String number, String name, Double price) {
this.number = number;
this.name = name;
this.price = price;
}
}
Producer类
package com.xzit.entity;
import java.util.List;
import com.xzit.db.db.DataBase;
import com.xzit.interfaces.IGoods;
/**
* 生产商
* @author Administrator
*
*/
public class Producer implements IGoods {
public List<Goods> produerGoodsList() {
return DataBase.GOODSLIST;
}
}
package:interfaces
DepartmentDaoImp类
package com.xzit.interfaces;
import java.util.List;
import com.xzit.db.db.DataBase;
public class DepartmentDaoImp<T> implements IObject<T> {
public List<T> findList() {
System.out.println("获取部门列表");
return (List<T>) DataBase.DEPLIST;
}
}
EmployeeDaoImp类
package com.xzit.interfaces;
import java.util.List;
import com.xzit.db.db.DataBase;
public class EmployeeDaoImp<T> implements IObject<T> {
public List<T> findList() {
System.out.println("获取员工列表");
return (List<T>) DataBase.EMPLIST;
}
}
IGoods
package com.xzit.interfaces;
import java.util.List;
import com.xzit.entity.Goods;
/**
* 提供产品列表的接口
* 代理实现者和被代理对象都应该实现此接口
* @author Administrator
*
*/
public interface IGoods {
/**
* 提供产品的方法
* @return
*/
List<Goods> produerGoodsList();
}
IObject
package com.xzit.interfaces;
import java.util.List;
public interface IObject<T> {
/**
* 查找目标数据列表
* @return
*/
List<T> findList();
}
package:proxy
TeatDynProxy
package com.xzit.proxy;
import java.lang.reflect.Proxy;
import java.util.List;
import com.xzit.aop.DynInvoke;
import com.xzit.entity.Department;
import com.xzit.entity.Employee;
import com.xzit.interfaces.DepartmentDaoImp;
import com.xzit.interfaces.EmployeeDaoImp;
import com.xzit.interfaces.IObject;
public class TestDynProxy {
/**
* 动态代理测试
* @param args
*/
public static void main(String[] args) {
DynInvoke dynInovke = new DynInvoke();// 创建动态代理对象
DepartmentDaoImp<Department> depDao = new DepartmentDaoImp<Department>();// 创建被代理的目标对象
EmployeeDaoImp<Employee> empDao = new EmployeeDaoImp<Employee>();
//dynInovke.setTarget(depDao);// 设置被代理对象
dynInovke.setTarget(empDao);// 设置被代理对象
Class [] classes = depDao.getClass().getInterfaces();// 获取所有被代理对象的父接口数组
IObject iObject = (IObject)Proxy.newProxyInstance(empDao.getClass().getClassLoader(), classes,dynInovke);// 获取代理类实例
List<Employee> depList = iObject.findList();// 调用目标方法
for(Employee dep:depList){
System.out.println(dep.getName());
}
}
}
TestStaticProxy
package com.xzit.proxy;
import java.util.List;
import java.util.Scanner;
import com.xzit.aop.ProducerProxy;
import com.xzit.entity.Client;
import com.xzit.entity.Goods;
import com.xzit.entity.Producer;
public class TestStaticProxy {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ProducerProxy proxy = new ProducerProxy();// 代理实现者
proxy.setProducer(new Producer());
System.out.println("输入客户姓名");
String name = in.next();
System.out.println("您是VIP请输入1,否则输入 0");
int isVip = in.nextInt();
boolean bool = isVip == 1?true:false;
Client c = new Client();
c.setName(name);
c.setVip(bool);
proxy.setClient(c);
List<Goods> goodsList = proxy.produerGoodsList();// 调用代理实现方法
for(Goods g:goodsList){
System.out.println(g.getName()+"\t"+g.getNumber()+"\t"+g.getPrice());
}
}
}
运行结果
动态代理方法
动态代理方法
获取员工列表
任我行
任盈盈
令狐冲
岳不群
丁春秋
静态代理方法
输入客户姓名
歌谣
您是VIP请输入1,否则输入 0
1
加热器 goods100 104.975
胜利羽毛球lark4 goods200 45.9
联想笔记本T450 goods300 7565.425
小米麦克风 goods400 84.575
本人来博客已经两月有余,期待与大家共同进步。分享技术,分享快乐。我是歌谣,感谢关注。