Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
58 lines (48 loc) · 1.85 KB

File metadata and controls

58 lines (48 loc) · 1.85 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
所谓静态代理就是代理类是由程序员自己编写的在编译期就确定好了的来看下下面的例子
```
public interface HelloSerivice {
public void say();
}
public class HelloSeriviceImpl implements HelloSerivice{
@Override
public void say() {
System.out.println("hello world");
}
}
```
上面的代码比较简单定义了一个接口和其实现类这就是代理模式中的目标对象和目标对象的接口接下类定义代理对象
```
public class HelloSeriviceProxy implements HelloSerivice{
private HelloSerivice target;
public HelloSeriviceProxy(HelloSerivice target) {
this.target = target;
}
@Override
public void say() {
System.out.println("记录日志");
target.say();
System.out.println("清理数据");
}
}
```
上面就是一个代理类他也实现了目标对象的接口并且扩展了say方法下面是一个测试类
```
public class Main {
@Test
public void testProxy(){
//目标对象
HelloSerivice target = new HelloSeriviceImpl();
//代理对象
HelloSeriviceProxy proxy = new HelloSeriviceProxy(target);
proxy.say();
}
}
```
// 记录日志
// hello world
// 清理数据
这就是一个简单的静态的代理模式的实现代理模式中的所有角色代理对象目标对象目标对象的接口等都是在编译期就确定好的
静态代理的用途
控制真实对象的访问权限 通过代理对象控制对真实对象的使用权限
避免创建大对象 通过使用一个代理小对象来代表一个真实的大对象可以减少系统资源的消耗对系统进行优化并提高运行速度
增强真实对象的功能 这个比较简单通过代理可以在调用真实对象的方法的前后增加额外功能
Morty Proxy This is a proxified and sanitized view of the page, visit original site.