设计模式之代理模式

Java30

本文由老王出租房子引出——代理设计模式,将从最简单的静态代理实现开始,后延伸使用jdk实现动态代理,最后扩展到Cglib实现动态代理。为了更深入理解代理模式,我们会对实际应用中的典型案例进行介绍,包括在Spring和Mybatis中的应用。

一、引出问题

在上一篇文章中,虽然老王和小王之间的电脑组装完美结束,但老王和小王之间的争吵并没有结束。老王决定把小王赶出去,把小王住的房子租出去。租金是用来支付游戏书的费用的。

[En]

In the last article, although the computer assembly between Lao Wang and Xiao Wang ended perfectly, the quarrel between Lao Wang and Xiao Wang did not end. Lao Wang decided to kick Xiao Wang out and rent out the house where Xiao Wang lived. The rent was used to cover the cost of the game book.

老王费尽心思摸清了各租房平台的规则,发布了房源信息,随后邻居提醒他:把房子租出去并不意味着躺着收房租,有一天租客提出了一些附加要求,在合同范围内,你也要尽量满足(为了了解,当然现实并不存在),房子租出去后,还要跟物业协调。

[En]

Lao Wang spent a lot of effort to figure out the rules of various rental platforms and released housing information, and then the neighbor reminded him: renting out the house does not mean lying down to collect rent, one day the tenant put forward some additional requirements, within the scope of the contract, you should also try to meet them (in order to understand, of course, reality does not exist), after the house is rented out, you have to coordinate with the property.

老王开始思考,如果我直租给租客,会面临两个问题:

①我需要了解租房的整个过程。我自己的生意和租房之间存在严重的耦合。

[En]

① I need to understand the whole process of renting. There is a serious coupling between my own business and renting.

我不得不参与②租户提出的一些要求,我不得不改变自己的日程安排。

[En]

I had to get involved in some of the demands made by ② tenants, and I had to change my own schedule.

我应该想到一种办法,

第一点是将业务与功能解耦,业务层专注于业务,比如网络接口请求。业务层只需要知道调用哪个接口请求方法,而不需要知道如何发起接口请求。

[En]

The first point is to decouple the business from the function, and the business layer focuses on the business, such as the network interface request. The business layer only needs to know which interface request method to call, but not how the interface request is initiated.

其次,创建一个小节,并在这个小节中添加一些一般性的附加操作,如注释解析、日志上报等,以避免在每个接口方法中编写这些一般性操作。

[En]

Second, create a section and add some general additional operations to this section, such as annotation parsing, log reporting, etc., to avoid writing these general operations in each interface method.

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长

相关文章
Java

冒泡排序

冒泡排序 原理 每一次遍历序列,只能确定一个元素的正确位置 第一次将末位元素归位,第二次将倒数第二位元素归位... 设(n)个元素排序,只需要将(n-1)个元素归位 每一次比较相邻元素,若相邻元素逆序...
Java

B树索引的相关概念

B树索引的相关概念 索引与表一样,也属于段(segment)的一种。里面存放了用户的数据,跟表一样需要占用磁盘空间。只 但是,索引中的数据存储形式与表中的数据格式非常不同。在理解索引时,您可以想象一本...
Java

Spring、SpringBoot面试题总结

开发框架面试题总结 1.spring是什么? 轻量级的开源的J2EE框架。它是⼀个容器框架,⽤来装javabean(java对象),中间层框架(万能胶) 可以起⼀个连接作⽤,⽐如说把Struts和hi...
Java

变量、常量、作用域(Java)

基本介绍 1.变量 定义:可以变化的量 2.变量声明 Java是一种强制类型语言,每一个变量必须声明类型 3.变量名,变量类型和作用域 Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型...
Java

java 5种IO模型

人的痛苦会把自己折磨到多深呢? You cannot swim for new horizons until you have courage to lose sight of the shore. ...
Java

设计模式之组合模式

本文通过对老王、小王对书房、书架及各类图书的管理进行探讨,引出结构设计纹样家族中的一个重要成员--组合纹样。本文将给出这两种组合模式的典型代码实现。为了加深理解,我们将在第三部分介绍组合模式在源代码中...
Java

Springmvc基础及应用

SpringMVC简介和环境搭建 SpringMVC简介 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一。在Spring3.0 后全面超越 S...
Java

java 异常类与自定义异常

目录 异常类 Exception 类的层次 throws/throw 关键字: throws: throw: try catch finally语句 声明自定义异常 异常类 在 Java 中一个异常的...
Java

【硬核】Dubbo常见面试题

Dubbo 整体介绍的差不多了,今天就开始面试环节了,我会列举一些常见的 Dubbo 面试题,只会抓着重的,一些太简单的我就不提了。 它不仅会给你面试问题的答案,而且会分析面试官问这个问题的原因,即他...