目录
  1. 1. Demo–静态代理模式
Demo--静态代理模式

Demo–静态代理模式

  • Person接口
1
2
3
public interface Person {
void speak();
}
  • Actor类(实现 Person 接口)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Actor implements Person{

private String content;

public Actor() {
}

public Actor(String content) {
this.content = content;
}

@Override
public void speak() {
System.out.println(content);
}
}
  • Agent类(代理Actor类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Agent implements Person{

private Actor actor;
private String before;
private String after;

public Agent(Actor actor, String before, String after) {
this.actor = actor;
this.before = before;
this.after = after;
}

public Agent() {
}

@Override
public void speak() {
System.out.println("Before Actor,Agent say: " + before);
actor.speak();
System.out.println("Before Actor,Agent say: " + after);
}
}
  • Test 测试
1
2
3
4
5
6
7
@Test
public void StaticAgentTest(){

Actor actor = new Actor("I am a Actor");
Agent agent = new Agent(actor,"before","after");
agent.speak();
}
  • 测试结果
1
2
3
Before Actor,Agent say: before
I am a Actor
Before Actor,Agent say: after
文章作者: Archiver
文章链接: https://www.kaiming66.com/2020/02/01/demo/Demo--%E9%9D%99%E6%80%81%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Archiver`s Blog
打赏
  • 微信
  • 支付寶

评论