当前位置:网站首页>【静态代理】
【静态代理】
2022-08-10 05:05:00 【liangjiayy】
先看一个类:Thread
- 接口类Runnable:

- Thread 类实现了 Runnable

- 有一个Runnable的成员变量

- 构造函数可以传Runnable

- 实现Runnable的run方法,调用成员变量的run:

这是静态代理。
现实现一个简单的静态代理:
- 有一个做饭的接口
- 厨师实现做饭接口
- 现在想要在厨师做饭前后做一些事(相当于对厨师做饭方法做增强)
- 定义一个饭店接口
- 在厨师做饭前准备好材料
- 在厨师做饭后把食品提供给用户
具体代码如下:
public class Tests {
public static void main(String[] args) {
Hotel hotel = new Hotel(new Chef());
hotel.cook();
}
}
//做饭
interface Cooking {
void cook();
}
//厨师,只管做饭
class Chef implements Cooking {
@Override
public void cook() {
System.out.println("Chef 正在做饭...");
}
}
//饭店,准备食材、厨师做饭、提供食品给顾客
class Hotel implements Cooking {
private Cooking cooking;
//构造函数传入接口对象
public Hotel(Cooking cooking) {
this.cooking = cooking;
}
@Override
public void cook() {
//对接口对象方法做增强
System.out.println("Hotel准备食材");
cooking.cook();
System.out.println("Hotel提供商品给用户");
}
}
边栏推荐
- flinksql怎么写redis的value只有最后一个字段?
- LeetCode 301. Remove Invalid Parentheses BFS
- SQLSERVER 2008 解析 Json 格式数据
- Nexus_仓库类型
- 【LeetCode】41、 缺失的第一个正数
- Hezhou ESP32C3 +1.8"tft network clock under Arduino framework
- Guys, the test in the idea uses FlinkCDC SQL to read Mysql data and write it into Kafka. The code creates
- 线程(上篇):线程的创建
- 【Web3 系列开发教程——创建你的第一个 NFT(7)】创建一个 NFT DApp,给你的 NFT 赋予属性,例如图片
- RadiAnt DICOM Viewer 2022.1 Crack
猜你喜欢
随机推荐
Joomla漏洞复现
[Web3 Series Development Tutorial - Create Your First NFT (7)] Create an NFT DApp and assign attributes to your NFT, such as pictures
Acwing 59. 把数字翻译成字符串 计数类DP
2022年T电梯修理考试题及模拟考试
LeetCode 2369. 检查数组是否存在有效划分 动态规划
`id` bigint(20) unsigned NOT NULL COMMENT '数据库主键',
栈与队列 | 用栈实现队列 | 用队列实现栈 | 基础理论与代码原理
Pulsar中游标的工作原理
JS获取当前时间的年、月、日、时间等
How cursors work in Pulsar
`id` bigint(20) unsigned NOT NULL COMMENT 'Database primary key',
十年架构五年生活-06 离职的冲动
一篇文章掌握整个JVM,JVM超详细解析!!!
开发智能硬件过程中需要掌握的方法之经典
重要转型升级
FPGA工程师面试试题集锦11~20
深度学习之-01
JS获取简单当前时间的年、月、日、时间等
When oracle cdc, set the parallelism to 2 and the number of slots to 1, and the final task has only one tm. Is it because oracle does not support concurrency
线性模型中的高级特征选择技术——基于R







