代理模式1-静态代理

代理模式属于结构型模式,分为静态代理和动态代理。

静态代理

直接演示案例,数据库分库分表的时候,多个数据源之间的切换,需要用到数据源路由;

实体Order:

@Data
public class Order {

    private Object orderInfo;
    private Long createTime;
    private String id;

}

dao层:

public class OrderDao {

    public int insert(Order order) {
        System.out.println("创建新order");
        return 1;
    }

}

service层:

// 接口
public interface IOrderService {

    int createOrder(Order order);

}

// 实现类
public class OrderService implements IOrderService{

    private OrderDao orderDao;

    public OrderService(){
        orderDao = new OrderDao();
    }

    @Override
    public int createOrder(Order order) {
        System.out.println("orderService创建订单");
        return orderDao.insert(order);
    }
}

动态数据源:

public class DynamicDataSourceEntry {

    public final static String DEFAULT_SOURCE = null;

    private final static ThreadLocal LOCAL = new ThreadLocal<>();

    public DynamicDataSourceEntry() {
    }

    /**
     * 清空数据源
     */
    public static void clear(){
        LOCAL.remove();
    }

    /**
     * 获取当前数据源名称
     * @return
     */
    public static String get(){
        return LOCAL.get();
    }

    /**
     * 还原当前切换的数据源
     */
    public static void restore(){
        LOCAL.set(DEFAULT_SOURCE);
    }

    /**
     * 设置已知名字的数据源
     * @param source
     */
    public static void set(String source){
        LOCAL.set(source);
    }

    /**
     * 根据年份动态设置数据源
     * @param year
     */
    public static void set(int year){
        LOCAL.set("DB_"+year);
    }

}

静态代理类:

public class OrderServiceStaticProxy implements IOrderService {

    private IOrderService orderService;

    public OrderServiceStaticProxy(IOrderService orderService){
        this.orderService = orderService;
    }

    @Override
    public int createOrder(Order order){
        before();
        int result = 0;
        try {
            Long createTime = order.getCreateTime();
            SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
            Integer dbRouter = Integer.valueOf(yearFormat.format(new Date(createTime)));
            System.out.println("静态代理怼到【DB_"+dbRouter+"】数据源");
            DynamicDataSourceEntry.set(dbRouter);
            result = orderService.createOrder(order);
            return result;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            after();
        }
        return result;
    }

    private void before(){
        System.out.println("代理前执行");
    }

    private void after(){
        System.out.println("代理后执行");
    }

}

测试:

    @Test
    public void test() throws ParseException {
        try {
            Order order = new Order();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date date = sdf.parse("2019/02/01");
            order.setCreateTime(date.getTime());
            order.setId("1");
            order.setOrderInfo(new ArrayList());

            // 创建订单
            OrderServiceStaticProxy orderService = new OrderServiceStaticProxy(new OrderService());
            int order1 = orderService.createOrder(order);
            System.out.println("order1 = " + order1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

运行结果:

代理前执行
静态代理怼到【DB_2019】数据源
orderService创建订单
创建新order
代理后执行
order1 = 1

你可能感兴趣的:(代理模式1-静态代理)