Skip to content

Commit 3c8c3c7

Browse files
author
deleiguo
committed
add template pattern
1 parent 88a4609 commit 3c8c3c7

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.delei.designpattern.template;
2+
3+
/**
4+
* 抽象顶级类:游戏
5+
*
6+
* @author deleiguo
7+
*/
8+
public abstract class AbstractGame {
9+
protected String name;
10+
11+
public AbstractGame(String name) {
12+
this.name = name;
13+
}
14+
15+
abstract void init();
16+
17+
abstract void play();
18+
19+
abstract void quit();
20+
21+
/**
22+
* 顶级逻辑方法
23+
*/
24+
public void start() {
25+
init();
26+
play();
27+
quit();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.delei.designpattern.template;
2+
3+
/**
4+
* 子类:篮球
5+
*
6+
* @author deleiguo
7+
*/
8+
public class Basketball extends AbstractGame {
9+
10+
public Basketball() {
11+
super("Basketball");
12+
}
13+
14+
@Override
15+
void init() {
16+
System.out.println(Basketball.class.getName() + ": init()");
17+
}
18+
19+
@Override
20+
void play() {
21+
System.out.println(Basketball.class.getName() + ": play()");
22+
}
23+
24+
@Override
25+
void quit() {
26+
System.out.println(Basketball.class.getName() + ": quit()");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.delei.designpattern.template;
2+
3+
/**
4+
* 子类:足球
5+
*
6+
* @author deleiguo
7+
*/
8+
public class Football extends AbstractGame {
9+
10+
public Football() {
11+
super("Football");
12+
}
13+
14+
@Override
15+
void init() {
16+
System.out.println(Football.class.getName() + ": init()");
17+
}
18+
19+
@Override
20+
void play() {
21+
System.out.println(Football.class.getName() + ": play()");
22+
}
23+
24+
@Override
25+
void quit() {
26+
System.out.println(Football.class.getName() + ": quit()");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.delei.designpattern.template;
2+
3+
import cn.delei.util.PrintUtil;
4+
5+
/**
6+
* 测试Main方法
7+
*
8+
* @author deleiguo
9+
*/
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
AbstractGame football = new Football();
14+
football.start();
15+
PrintUtil.printDivider();
16+
AbstractGame basketball = new Basketball();
17+
basketball.start();
18+
}
19+
20+
}

0 commit comments

Comments
 (0)