File tree Expand file tree Collapse file tree 4 files changed +105
-0
lines changed
source/java/interview/delei-interview-designpattern/src/main/java/cn/delei/designpattern/template Expand file tree Collapse file tree 4 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments