当前位置:网站首页>【Static proxy】
【Static proxy】
2022-08-10 05:11:00 【liangjiayy】
先看一个类:Thread
- 接口类Runnable:
- Thread 类实现了 Runnable
- 有一个Runnable的成员变量
- Constructor can be passedRunnable
- 实现Runnable的run方法,调用成员变量的run:
This is a static proxy.
Now implement a simple static proxy:
- There is an interface for cooking
- Chef implements the cooking interface
- Now want to do something before and after the chef cooks(It is equivalent to enhancing the chef's cooking method)
- Define a restaurant interface
- Prepare the ingredients before the chef cooks
- The food is served to the user after the chef has cooked it
具体代码如下:
public class Tests {
public static void main(String[] args) {
Hotel hotel = new Hotel(new Chef());
hotel.cook();
}
}
//做饭
interface Cooking {
void cook();
}
//厨师,just cook
class Chef implements Cooking {
@Override
public void cook() {
System.out.println("Chef 正在做饭...");
}
}
//饭店,准备食材、厨师做饭、Provide food to customers
class Hotel implements Cooking {
private Cooking cooking;
//The constructor passes in the interface object
public Hotel(Cooking cooking) {
this.cooking = cooking;
}
@Override
public void cook() {
//Enhancements to interface object methods
System.out.println("Hotel准备食材");
cooking.cook();
System.out.println("HotelProvide products to users");
}
}
边栏推荐
- 看了几十篇轻量化目标检测论文扫盲做的摘抄笔记
- ctf-pikachu-file_inclusion
- 告诉你如何从keil工程知道使用了多少RAM和ROM空间
- 西门子Step7和TIA软件“交叉引用”的使用
- JS gets the year, month, day, time, etc. of the current time
- [Web3 Series Development Tutorial - Create Your First NFT (7)] Create an NFT DApp and assign attributes to your NFT, such as pictures
- FPGA engineer interview questions collection 1~10
- tensorflow分词深度学习——影评预测
- 剑指Offer 033.变位数组
- mysql常用命令有什么
猜你喜欢
随机推荐
Guys, the test in the idea uses FlinkCDC SQL to read Mysql data and write it into Kafka. The code creates
2022G3 Boiler Water Treatment Exam Mock 100 Questions and Mock Exam
【心理学·人物】第二期(学术X综艺)
栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记
RK3568处理器体验小记
SQLSERVER 2008 解析 Json 格式数据
Consulting cdc 2.0 for mysql does not execute flush with read lock. How to ensure bin
西门子Step7和TIA软件“交叉引用”的使用
Thread.sleep, Thread.yield 作用解释
Shield Alt hotkey in vscode
OAuth2 usage scenarios, common misunderstandings, use cases
元宇宙 | 你能通过图灵测试吗?
What is the relationship between legal representative and shareholders?
How to improve product quality from the code layer
oracle rac 11g安装执行root.sh时报错
leetcode每天5题-Day11
`id` bigint(20) unsigned NOT NULL COMMENT 'Database primary key',
awk of the Three Musketeers of Shell Programming
flinkcdc 读取pgsql 的时间被放大了 有大佬知道咋回事吗 gmt_create':1
Stacks and Queues | Implementing Queues with Stacks | Implementing Stacks with Queues | Basic Theory and Code Principles