Skip to content

Commit a5a3821

Browse files
committed
设计模式
1 parent efc1088 commit a5a3821

File tree

22 files changed

+325
-1
lines changed

22 files changed

+325
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.heibaiying.behavioral.chain_of_responsibility;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class Application {
9+
private String title;
10+
/*请假天数*/
11+
private int dayNum;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.heibaiying.behavioral.chain_of_responsibility;
2+
3+
public class DepartManager extends Leader {
4+
5+
@Override
6+
public void approval(Application application) {
7+
System.out.println(application.getTitle() + "被部门经理审批通过");
8+
if (application.getDayNum() >= 5) {
9+
leader.approval(application);
10+
}
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.heibaiying.behavioral.chain_of_responsibility;
2+
3+
public class GroupLeader extends Leader {
4+
5+
@Override
6+
public void approval(Application application) {
7+
System.out.println(application.getTitle() + "被组长审批通过");
8+
if (application.getDayNum() >= 3) {
9+
leader.approval(application);
10+
}
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.heibaiying.behavioral.chain_of_responsibility;
2+
3+
public abstract class Leader {
4+
5+
protected Leader leader;
6+
7+
public Leader setNextLeader(Leader leader) {
8+
this.leader = leader;
9+
return leader;
10+
}
11+
12+
public abstract void approval(Application application);
13+
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.heibaiying.behavioral.chain_of_responsibility;
2+
3+
public class President extends Leader {
4+
5+
@Override
6+
public void approval(Application application) {
7+
System.out.println(application.getTitle() + "被总经理审批通过");
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.heibaiying.behavioral.chain_of_responsibility;
2+
3+
public class ZTest {
4+
public static void main(String[] args) {
5+
GroupLeader groupLeader = new GroupLeader();
6+
DepartManager departManager = new DepartManager();
7+
President president = new President();
8+
groupLeader.setNextLeader(departManager).setNextLeader(president);
9+
groupLeader.approval(new Application("事假单", 3));
10+
groupLeader.approval(new Application("婚假单", 10));
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.heibaiying.behavioral.command;
2+
3+
public interface Command {
4+
void execute();
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.heibaiying.behavioral.command;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Manager {
7+
8+
private List<Command> commandList = new ArrayList<>();
9+
10+
public void addCommand(Command command) {
11+
commandList.add(command);
12+
}
13+
14+
public void executeCommands() {
15+
for (Command command : commandList) {
16+
command.execute();
17+
}
18+
commandList.clear();
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.heibaiying.behavioral.command;
2+
3+
public class MuteCommand implements Command {
4+
5+
private Player player;
6+
7+
public MuteCommand(Player player) {
8+
this.player = player;
9+
}
10+
11+
12+
@Override
13+
public void execute() {
14+
player.mute();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.heibaiying.behavioral.command;
2+
3+
public class PauseCommand implements Command {
4+
5+
private Player player;
6+
7+
public PauseCommand(Player player) {
8+
this.player = player;
9+
}
10+
11+
12+
@Override
13+
public void execute() {
14+
player.pause();
15+
}
16+
}

0 commit comments

Comments
 (0)