代理模式中的静态代理
舉個(gè)例子:人到了適婚年齡,父母總是迫不及待希望早點(diǎn)抱孫子。而現(xiàn)在社會(huì)的人在各種壓力之下,都選擇晚婚晚育。于是著急的父母就開始到處為自己的子女相親,比子女自己還著急。這個(gè)相親的過(guò)程,就是一種我們?nèi)巳硕加蟹莸拇怼?lái)看代碼實(shí)現(xiàn):頂層接口Person:?
public interface Person {void findLove(); }兒子要找對(duì)象,實(shí)現(xiàn)Son類:?
public class Son implements Person{public void findLove(){System.out.println("兒子要求:膚白貌美大長(zhǎng)腿");}public void findJob(){}public void eat(){}}父親要幫兒子相親,實(shí)現(xiàn) Father類:?
public class Father implements Person {private Son person;public Father(Son person){this.person = person;}public void findLove(){System.out.println("父親物色對(duì)象");this.person.findLove();System.out.println("雙方父母同意,確立關(guān)系");}public void findJob(){}}來(lái)看測(cè)試代碼:?
public class FatherProxyTest {public static void main(String[] args) {Father father = new Father(new Son());father.findLove();}}這里小伙伴們可能會(huì)覺(jué)得還是不知道如何講代理模式應(yīng)用到業(yè)務(wù)場(chǎng)景中,那么我們?cè)賮?lái)舉例一個(gè)實(shí)際的業(yè)務(wù)場(chǎng)景。在分布式業(yè)務(wù)場(chǎng)景中,我們通常會(huì)對(duì)數(shù)據(jù)庫(kù)進(jìn)行分庫(kù)分表,分庫(kù)分表之后使用 Java操作時(shí),就可能需要配置多個(gè)數(shù)據(jù)源,我們通過(guò)設(shè)置數(shù)據(jù)源路由來(lái)動(dòng)態(tài)切換數(shù)據(jù)源。先創(chuàng)建 Order訂單實(shí)體:?
public class Order {private Object orderInfo;//訂單創(chuàng)建時(shí)間進(jìn)行按年分庫(kù)private Long createTime;private String id;public Object getOrderInfo() {return orderInfo;}public void setOrderInfo(Object orderInfo) {this.orderInfo = orderInfo;}public Long getCreateTime() {return createTime;}public void setCreateTime(Long createTime) {this.createTime = createTime;}public String getId() {return id;}public void setId(String id) {this.id = id;} }創(chuàng)建 OrderDao持久層操作類:?
public class OrderDao {public int insert(Order order){System.out.println("OrderDao創(chuàng)建Order成功!");return 1;} }創(chuàng)建 IOrderService接口:?
public interface IOrderService {int createOrder(Order order); }創(chuàng)建 OrderService實(shí)現(xiàn)類:?
public class OrderService implements IOrderService {private OrderDao orderDao;public OrderService(){//如果使用Spring應(yīng)該是自動(dòng)注入的//我們?yōu)榱耸褂梅奖?#xff0c;在構(gòu)造方法中將orderDao直接初始化了orderDao = new OrderDao();}public int createOrder(Order order) {System.out.println("OrderService調(diào)用orderDao創(chuàng)建訂單");return orderDao.insert(order);} }接下來(lái)使用靜態(tài)代理,主要完成的功能是,根據(jù)訂單創(chuàng)建時(shí)間自動(dòng)按年進(jìn)行分庫(kù)。根據(jù)開閉原則,原來(lái)寫好的邏輯我們不去修改,通過(guò)代理對(duì)象來(lái)完成。先創(chuàng)建數(shù)據(jù)源路由對(duì)象,我們使用 ThreadLocal的單例實(shí)現(xiàn),DynamicDataSourceEntry類:
public class DynamicDataSourceEntity {public final static String DEFAULE_SOURCE = null;private final static ThreadLocal<String> local = new ThreadLocal<String>();private DynamicDataSourceEntity(){}public static String get(){return local.get();}public static void restore(){local.set(DEFAULE_SOURCE);}//DB_2018//DB_2019public static void set(String source){local.set(source);}public static void set(int year){local.set("DB_" + year);}}創(chuàng)建切換數(shù)據(jù)源的代理 OrderServiceSaticProxy類:?
public class OrderServiceStaticProxy implements IOrderService {private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");private IOrderService orderService;public OrderServiceStaticProxy(IOrderService orderService) {this.orderService = orderService;}public int createOrder(Order order) {Long time = order.getCreateTime();Integer dbRouter = Integer.valueOf(yearFormat.format(new Date(time)));System.out.println("靜態(tài)代理類自動(dòng)分配到【DB_" + dbRouter + "】數(shù)據(jù)源處理數(shù)據(jù)" );DynamicDataSourceEntity.set(dbRouter);this.orderService.createOrder(order);DynamicDataSourceEntity.restore();return 0;} }現(xiàn)在我們?cè)賮?lái)回顧一下類圖,看是不是和我們最先畫的類結(jié)構(gòu)一致:
?
總結(jié)
以上是生活随笔為你收集整理的代理模式中的静态代理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 代理模式的应用场景
- 下一篇: 代理模式中的动态代理