Skip to content

Commit e0c6e1f

Browse files
author
sheng
committed
junit3.x
1 parent 30d4ab3 commit e0c6e1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2457
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.chain;
2+
3+
public class EmailLogger extends Logger{
4+
5+
EmailLogger(int level) {
6+
super(level);
7+
}
8+
9+
@Override
10+
protected void write(String message) {
11+
System.out.println("EmailLogger "+message);
12+
}
13+
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.chain;
2+
3+
public class FileLogger extends Logger {
4+
5+
FileLogger(int level) {
6+
super(level);
7+
}
8+
9+
@Override
10+
protected void write(String message) {
11+
System.out.println("FileLogger " + message);
12+
}
13+
14+
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderising.dp.chain;
2+
3+
public abstract class Logger {
4+
public static int DEBUG = 1;
5+
public static int NOTICE = 2;
6+
public static int ERR = 3;
7+
protected int level;
8+
9+
Logger(int level) {
10+
this.level=level;
11+
}
12+
protected Logger nextLogger;
13+
public Logger setNextLogger(Logger nextLogger) {
14+
this.nextLogger = nextLogger;
15+
return this;
16+
}
17+
18+
public void message(String message,int level){
19+
if (this.level<=level) {
20+
write(message);
21+
}
22+
if (nextLogger!=null) {
23+
nextLogger.message( message,level);
24+
}
25+
}
26+
abstract protected void write(String message);
27+
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderising.dp.chain;
2+
3+
public class StdoutLogger extends Logger{
4+
5+
StdoutLogger(int level) {
6+
super(level);
7+
}
8+
9+
@Override
10+
protected void write(String message) {
11+
System.out.println("StdoutLogger "+message);
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.chain;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
Logger logger = new StdoutLogger(Logger.DEBUG)
7+
.setNextLogger(new EmailLogger(Logger.NOTICE).setNextLogger(new FileLogger(Logger.ERR)));
8+
logger.message("进入函数计算", Logger.DEBUG);
9+
10+
logger.message("第一步已经完成", Logger.NOTICE);
11+
12+
logger.message("一个致命的错误发生了", Logger.ERR);
13+
}
14+
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.dp.command;
2+
3+
public interface Command {
4+
void order();
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coderising.dp.command;
2+
3+
public class Cook {
4+
void cookSteak(){
5+
System.out.println("Steak is ok");
6+
}
7+
8+
void cookPork(){
9+
System.out.println("Pork is ok");
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderising.dp.command;
2+
3+
public class OrderPorkCommand implements Command{
4+
private Cook cook;
5+
public OrderPorkCommand(Cook cook) {
6+
this.cook=cook;
7+
}
8+
@Override
9+
public void order() {
10+
cook.cookPork();
11+
}
12+
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.command;
2+
3+
public class OrderSteakCommand implements Command {
4+
private Cook cook;
5+
6+
public OrderSteakCommand(Cook cook) {
7+
this.cook = cook;
8+
}
9+
10+
@Override
11+
public void order() {
12+
cook.cookSteak();
13+
}
14+
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.coderising.dp.command;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Waiter {
7+
private List<Command> commands = new ArrayList<>();
8+
9+
public void addOrder(Command command) {
10+
commands.add(command);
11+
}
12+
13+
public void sendOrders(){
14+
for (Command command : commands) {
15+
command.order();
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
Cook cook=new Cook();
21+
22+
Waiter waiter=new Waiter();
23+
24+
Command command1=new OrderSteakCommand(cook);
25+
Command command2=new OrderPorkCommand(cook);
26+
27+
waiter.addOrder(command1);
28+
waiter.addOrder(command2);
29+
30+
waiter.sendOrders();
31+
}
32+
33+
}

0 commit comments

Comments
 (0)