Skip to content

Commit

Permalink
添加代理模式
Browse files Browse the repository at this point in the history
  • Loading branch information
VentureQ committed Jul 9, 2021
1 parent 5949093 commit fb0df73
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 23种设计模式/代理模式的概念及Java实现.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
代理模式是指为对象提供一种通过代理方式来访问并控制对象行为的方法。在客户端不不适合或者不能够直接引用一个对象时,可以通过该对象的代理来实现对该对象的访问,可以将该代理对象理解为客户端和目标对象之间的中介者。

定义Company接口:

```java
public interface Company {
void findWorker(String title);
}
```

Company接口的实现类HR:

```java
public class HR implements Company{
@Override
public void findWorker(String title) {
System.out.println("I need worker,title is:"+title);
}
}
```

定义Proxy:

```java
import java.util.HashMap;
import java.util.Map;

public class Proxy implements Company {
private HR hr;

public Proxy() {
super();
this.hr = new HR();
}

@Override
public void findWorker(String title) {//{需要代理的方法}
hr.findWorker(title);
String worker = getWorker(title);
System.out.println(worker);
}

private String getWorker(String title) {
Map<String, String> workList = new HashMap<String, String>(){
{put("Java", "张三");put("C++", "李四");}
};
return workList.get(title);
}
}
```

代理模式使用:

```java
public class Test {
public static void main(String[] args) {
Company company=new Proxy();
company.findWorker("Java");
}
}
```

0 comments on commit fb0df73

Please sign in to comment.