Skip to content

Commit 8316d21

Browse files
author
liuxun
committed
代理模式
1 parent cf6a07b commit 8316d21

5 files changed

Lines changed: 81 additions & 1 deletion

File tree

src/main/java/com/algorithm/study/demo/mode/TestMode.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.algorithm.study.demo.mode.factory.HumanFactory;
44
import com.algorithm.study.demo.mode.factory.WhiteHuman;
5+
import com.algorithm.study.demo.mode.proxy.GamePlayer;
6+
import com.algorithm.study.demo.mode.proxy.IGamePlayer;
57
import com.algorithm.study.demo.mode.singleton.EnumSingleton;
68
import com.algorithm.study.demo.mode.strategy.MemberStrategy;
79
import com.algorithm.study.demo.mode.strategy.Price;
@@ -20,10 +22,17 @@ public static void main(String[] args) {
2022
EnumSingleton obj2 = EnumSingleton.getInstance();
2123
//输出结果:obj1==obj2?true
2224
System.out.println("obj1==obj2?" + (obj1==obj2));
23-
//策略模式
25+
/**
26+
* 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换,策略模式让算法独立于使用它的客户而独立变化。
27+
* 策略模式的缺点:
28+
* 1、客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
29+
* 2、策略模式造成很多的策略类,每个具体策略类都会产生一个新类。
30+
*/
2431
MemberStrategy memberStrategy=new PrimaryMemberStrategy();
2532
Price price=new Price(memberStrategy);
2633
price.quote(2222);
34+
35+
2736
//模板方法
2837
TemplateInterface subClassOne=new SubClassOne();
2938
TemplateInterface subClassTwo=new SubClassTwo();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.algorithm.study.demo.mode.proxy;
2+
3+
/**
4+
* 游戏者
5+
*/
6+
public class GamePlayer implements IGamePlayer{
7+
private String name;
8+
9+
public GamePlayer(String name) {
10+
this.name = name;
11+
}
12+
13+
public void login(String userName, String password) {
14+
System.out.println(name+"开始登录,用户名为:"+userName+",密码为:"+password);
15+
}
16+
17+
public void killBoss() {
18+
System.out.println(name+"开始杀怪");
19+
}
20+
21+
public void upgrade() {
22+
System.out.println(name+"开始升级");
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.algorithm.study.demo.mode.proxy;
2+
3+
/**
4+
* 游戏代练者
5+
*/
6+
public class GamePlayerProxy implements IGamePlayer{
7+
private IGamePlayer iGamePlayer;
8+
public GamePlayerProxy(IGamePlayer iGamePlayer) {
9+
this.iGamePlayer=iGamePlayer;
10+
}
11+
12+
public void login(String userName, String password) {
13+
iGamePlayer.login(userName,password);
14+
}
15+
16+
public void killBoss() {
17+
iGamePlayer.killBoss();
18+
}
19+
20+
public void upgrade() {
21+
iGamePlayer.upgrade();
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.algorithm.study.demo.mode.proxy;
2+
3+
/**
4+
* 游戏者接口
5+
*/
6+
public interface IGamePlayer {
7+
void login(String userName,String password);
8+
void killBoss();
9+
void upgrade();
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.algorithm.study.demo.mode.proxy;
2+
3+
public class MainTest {
4+
public static void main(String[] args) {
5+
/**
6+
* 为其他对象提供一种代理以控制对这个对象的访问
7+
*/
8+
IGamePlayer gamePlayer=new GamePlayer("刘勋");
9+
GamePlayerProxy proxy=new GamePlayerProxy(gamePlayer);
10+
proxy.login("liuxun","xxxxx");
11+
proxy.killBoss();
12+
proxy.upgrade();
13+
}
14+
}

0 commit comments

Comments
 (0)