Break out common code from RoShamBo1.java, reuse in patterns

This commit is contained in:
Bruce Eckel 2021-03-05 02:28:04 -07:00
parent ede3954d86
commit a75a55dc81
8 changed files with 156 additions and 569 deletions

View File

@ -1,512 +1,128 @@
0: ForkJoinPool.commonPool-worker-1
0: main
1: main
2: main
2: ForkJoinPool.commonPool-worker-1
3: main
4: ForkJoinPool.commonPool-worker-1
4: main
5: main
6: ForkJoinPool.commonPool-worker-1
6: main
7: main
9: ForkJoinPool.commonPool-worker-1
8: main
9: main
11: ForkJoinPool.commonPool-worker-1
10: main
11: main
12: ForkJoinPool.commonPool-worker-1
12: main
13: main
15: ForkJoinPool.commonPool-worker-1
14: main
15: main
16: main
17: main
18: main
19: main
20: main
21: main
17: ForkJoinPool.commonPool-worker-1
22: main
23: main
23: ForkJoinPool.commonPool-worker-1
24: main
25: ForkJoinPool.commonPool-worker-1
25: main
26: main
27: ForkJoinPool.commonPool-worker-1
27: main
28: main
29: ForkJoinPool.commonPool-worker-1
29: main
30: main
31: main
32: main
32: ForkJoinPool.commonPool-worker-1
33: main
35: ForkJoinPool.commonPool-worker-1
36: ForkJoinPool.commonPool-worker-1
37: ForkJoinPool.commonPool-worker-1
38: ForkJoinPool.commonPool-worker-1
34: main
35: main
39: ForkJoinPool.commonPool-worker-1
41: ForkJoinPool.commonPool-worker-1
36: main
37: main
38: main
39: main
40: main
42: ForkJoinPool.commonPool-worker-1
41: main
42: main
43: main
44: ForkJoinPool.commonPool-worker-1
44: main
45: main
46: ForkJoinPool.commonPool-worker-1
46: main
47: main
48: main
49: main
50: main
50: ForkJoinPool.commonPool-worker-1
51: main
52: ForkJoinPool.commonPool-worker-1
52: main
53: main
55: ForkJoinPool.commonPool-worker-1
54: main
55: main
56: ForkJoinPool.commonPool-worker-1
58: ForkJoinPool.commonPool-worker-1
56: main
57: main
59: ForkJoinPool.commonPool-worker-1
58: main
59: main
60: main
61: ForkJoinPool.commonPool-worker-1
61: main
62: main
63: ForkJoinPool.commonPool-worker-1
63: main
64: main
65: ForkJoinPool.commonPool-worker-1
65: main
66: main
67: ForkJoinPool.commonPool-worker-1
67: main
68: main
69: ForkJoinPool.commonPool-worker-1
71: ForkJoinPool.commonPool-worker-1
72: ForkJoinPool.commonPool-worker-1
69: main
70: main
73: ForkJoinPool.commonPool-worker-1
75: ForkJoinPool.commonPool-worker-1
76: ForkJoinPool.commonPool-worker-1
71: main
72: main
73: main
74: main
77: ForkJoinPool.commonPool-worker-1
78: ForkJoinPool.commonPool-worker-1
75: main
76: main
77: main
78: main
79: main
80: ForkJoinPool.commonPool-worker-1
80: main
81: main
82: ForkJoinPool.commonPool-worker-1
82: main
83: main
84: ForkJoinPool.commonPool-worker-1
84: main
85: main
86: ForkJoinPool.commonPool-worker-1
86: main
87: main
89: ForkJoinPool.commonPool-worker-1
90: ForkJoinPool.commonPool-worker-1
88: main
89: main
91: ForkJoinPool.commonPool-worker-1
90: main
91: main
92: main
93: ForkJoinPool.commonPool-worker-1
93: main
94: main
95: ForkJoinPool.commonPool-worker-1
95: main
96: main
97: main
98: main
97: ForkJoinPool.commonPool-worker-1
99: main
100: ForkJoinPool.commonPool-worker-1
100: main
101: main
103: ForkJoinPool.commonPool-worker-1
102: main
103: main
104: ForkJoinPool.commonPool-worker-1
106: ForkJoinPool.commonPool-worker-1
107: ForkJoinPool.commonPool-worker-1
104: main
105: main
106: main
108: ForkJoinPool.commonPool-worker-1
110: ForkJoinPool.commonPool-worker-1
108: ForkJoinPool.commonPool-worker-2
111: ForkJoinPool.commonPool-worker-1
107: main
108: main
109: main
110: main
113: ForkJoinPool.commonPool-worker-1
112: ForkJoinPool.commonPool-worker-2
115: ForkJoinPool.commonPool-worker-1
111: main
112: main
113: main
114: main
115: main
116: main
116: ForkJoinPool.commonPool-worker-2
117: ForkJoinPool.commonPool-worker-1
117: main
118: main
119: ForkJoinPool.commonPool-worker-2
120: ForkJoinPool.commonPool-worker-1
119: main
120: main
121: main
123: ForkJoinPool.commonPool-worker-1
122: main
123: main
124: main
124: ForkJoinPool.commonPool-worker-2
125: main
126: main
126: ForkJoinPool.commonPool-worker-1
127: ForkJoinPool.commonPool-worker-2
128: main
130: ForkJoinPool.commonPool-worker-1
130: ForkJoinPool.commonPool-worker-2
131: main
133: ForkJoinPool.commonPool-worker-1
134: ForkJoinPool.commonPool-worker-2
134: main
136: ForkJoinPool.commonPool-worker-1
137: main
137: ForkJoinPool.commonPool-worker-2
138: ForkJoinPool.commonPool-worker-1
140: main
140: ForkJoinPool.commonPool-worker-2
141: ForkJoinPool.commonPool-worker-1
143: main
143: ForkJoinPool.commonPool-worker-2
145: ForkJoinPool.commonPool-worker-1
146: main
146: ForkJoinPool.commonPool-worker-2
148: ForkJoinPool.commonPool-worker-1
149: main
149: ForkJoinPool.commonPool-worker-2
151: ForkJoinPool.commonPool-worker-1
152: ForkJoinPool.commonPool-worker-2
154: ForkJoinPool.commonPool-worker-1
154: ForkJoinPool.commonPool-worker-2
152: main
156: ForkJoinPool.commonPool-worker-1
156: ForkJoinPool.commonPool-worker-2
159: ForkJoinPool.commonPool-worker-1
159: ForkJoinPool.commonPool-worker-2
161: ForkJoinPool.commonPool-worker-1
159: main
162: ForkJoinPool.commonPool-worker-1
161: ForkJoinPool.commonPool-worker-2
164: ForkJoinPool.commonPool-worker-1
163: main
166: ForkJoinPool.commonPool-worker-1
168: ForkJoinPool.commonPool-worker-1
168: main
169: ForkJoinPool.commonPool-worker-1
170: main
167: ForkJoinPool.commonPool-worker-2
171: ForkJoinPool.commonPool-worker-1
172: main
168: ForkJoinPool.commonPool-worker-3
174: ForkJoinPool.commonPool-worker-1
175: main
178: ForkJoinPool.commonPool-worker-1
178: main
178: ForkJoinPool.commonPool-worker-3
175: ForkJoinPool.commonPool-worker-2
179: ForkJoinPool.commonPool-worker-1
181: main
183: ForkJoinPool.commonPool-worker-1
183: ForkJoinPool.commonPool-worker-2
184: main
184: ForkJoinPool.commonPool-worker-3
185: ForkJoinPool.commonPool-worker-1
187: main
187: ForkJoinPool.commonPool-worker-2
189: ForkJoinPool.commonPool-worker-1
191: main
189: ForkJoinPool.commonPool-worker-3
191: ForkJoinPool.commonPool-worker-2
193: main
193: ForkJoinPool.commonPool-worker-1
195: ForkJoinPool.commonPool-worker-3
195: ForkJoinPool.commonPool-worker-2
197: main
199: ForkJoinPool.commonPool-worker-3
199: ForkJoinPool.commonPool-worker-2
197: ForkJoinPool.commonPool-worker-1
200: main
202: ForkJoinPool.commonPool-worker-3
203: ForkJoinPool.commonPool-worker-2
204: ForkJoinPool.commonPool-worker-1
204: main
205: ForkJoinPool.commonPool-worker-3
207: ForkJoinPool.commonPool-worker-2
208: ForkJoinPool.commonPool-worker-1
208: main
209: ForkJoinPool.commonPool-worker-3
211: ForkJoinPool.commonPool-worker-2
211: ForkJoinPool.commonPool-worker-1
213: main
213: ForkJoinPool.commonPool-worker-3
215: ForkJoinPool.commonPool-worker-2
215: ForkJoinPool.commonPool-worker-1
217: main
217: ForkJoinPool.commonPool-worker-3
219: ForkJoinPool.commonPool-worker-2
220: ForkJoinPool.commonPool-worker-1
221: main
221: ForkJoinPool.commonPool-worker-3
224: ForkJoinPool.commonPool-worker-1
225: main
225: ForkJoinPool.commonPool-worker-3
223: ForkJoinPool.commonPool-worker-2
227: ForkJoinPool.commonPool-worker-1
227: main
229: ForkJoinPool.commonPool-worker-3
229: ForkJoinPool.commonPool-worker-2
231: ForkJoinPool.commonPool-worker-1
231: main
233: ForkJoinPool.commonPool-worker-3
233: ForkJoinPool.commonPool-worker-2
235: ForkJoinPool.commonPool-worker-1
236: main
237: ForkJoinPool.commonPool-worker-3
237: ForkJoinPool.commonPool-worker-2
239: ForkJoinPool.commonPool-worker-1
241: main
241: ForkJoinPool.commonPool-worker-2
241: ForkJoinPool.commonPool-worker-3
243: ForkJoinPool.commonPool-worker-1
244: main
244: ForkJoinPool.commonPool-worker-2
245: ForkJoinPool.commonPool-worker-3
247: ForkJoinPool.commonPool-worker-1
249: ForkJoinPool.commonPool-worker-3
251: ForkJoinPool.commonPool-worker-3
250: ForkJoinPool.commonPool-worker-1
251: ForkJoinPool.commonPool-worker-2
253: ForkJoinPool.commonPool-worker-3
254: ForkJoinPool.commonPool-worker-1
254: ForkJoinPool.commonPool-worker-2
251: main
255: ForkJoinPool.commonPool-worker-3
257: ForkJoinPool.commonPool-worker-1
257: ForkJoinPool.commonPool-worker-2
259: ForkJoinPool.commonPool-worker-3
261: ForkJoinPool.commonPool-worker-1
261: ForkJoinPool.commonPool-worker-2
262: ForkJoinPool.commonPool-worker-3
263: ForkJoinPool.commonPool-worker-1
264: ForkJoinPool.commonPool-worker-2
265: ForkJoinPool.commonPool-worker-3
267: ForkJoinPool.commonPool-worker-1
267: ForkJoinPool.commonPool-worker-2
261: main
268: ForkJoinPool.commonPool-worker-3
269: ForkJoinPool.commonPool-worker-1
270: ForkJoinPool.commonPool-worker-2
272: ForkJoinPool.commonPool-worker-3
273: ForkJoinPool.commonPool-worker-1
276: ForkJoinPool.commonPool-worker-3
276: ForkJoinPool.commonPool-worker-1
274: ForkJoinPool.commonPool-worker-2
277: ForkJoinPool.commonPool-worker-3
279: ForkJoinPool.commonPool-worker-1
279: ForkJoinPool.commonPool-worker-2
275: main
281: ForkJoinPool.commonPool-worker-1
280: ForkJoinPool.commonPool-worker-3
282: ForkJoinPool.commonPool-worker-2
285: ForkJoinPool.commonPool-worker-1
286: ForkJoinPool.commonPool-worker-3
286: ForkJoinPool.commonPool-worker-2
288: ForkJoinPool.commonPool-worker-1
289: ForkJoinPool.commonPool-worker-3
289: ForkJoinPool.commonPool-worker-2
286: main
291: ForkJoinPool.commonPool-worker-1
292: ForkJoinPool.commonPool-worker-3
293: ForkJoinPool.commonPool-worker-2
293: main
296: ForkJoinPool.commonPool-worker-3
296: ForkJoinPool.commonPool-worker-2
297: main
297: ForkJoinPool.commonPool-worker-1
299: ForkJoinPool.commonPool-worker-3
299: ForkJoinPool.commonPool-worker-2
301: main
301: ForkJoinPool.commonPool-worker-1
303: ForkJoinPool.commonPool-worker-3
303: ForkJoinPool.commonPool-worker-2
305: main
305: ForkJoinPool.commonPool-worker-1
307: ForkJoinPool.commonPool-worker-3
307: ForkJoinPool.commonPool-worker-2
309: main
309: ForkJoinPool.commonPool-worker-1
311: ForkJoinPool.commonPool-worker-3
311: ForkJoinPool.commonPool-worker-2
313: main
313: ForkJoinPool.commonPool-worker-1
315: ForkJoinPool.commonPool-worker-3
315: ForkJoinPool.commonPool-worker-2
317: ForkJoinPool.commonPool-worker-1
319: ForkJoinPool.commonPool-worker-3
319: ForkJoinPool.commonPool-worker-2
320: ForkJoinPool.commonPool-worker-1
322: ForkJoinPool.commonPool-worker-3
317: main
323: ForkJoinPool.commonPool-worker-1
322: ForkJoinPool.commonPool-worker-2
324: ForkJoinPool.commonPool-worker-3
328: main
328: ForkJoinPool.commonPool-worker-3
328: ForkJoinPool.commonPool-worker-2
330: ForkJoinPool.commonPool-worker-3
329: main
331: ForkJoinPool.commonPool-worker-2
332: ForkJoinPool.commonPool-worker-3
334: ForkJoinPool.commonPool-worker-2
335: main
335: ForkJoinPool.commonPool-worker-3
337: ForkJoinPool.commonPool-worker-2
338: main
338: ForkJoinPool.commonPool-worker-3
340: ForkJoinPool.commonPool-worker-2
341: main
341: ForkJoinPool.commonPool-worker-3
343: ForkJoinPool.commonPool-worker-2
343: main
344: ForkJoinPool.commonPool-worker-3
346: ForkJoinPool.commonPool-worker-2
347: main
347: ForkJoinPool.commonPool-worker-3
349: ForkJoinPool.commonPool-worker-2
350: main
350: ForkJoinPool.commonPool-worker-3
351: ForkJoinPool.commonPool-worker-2
353: main
353: ForkJoinPool.commonPool-worker-3
355: ForkJoinPool.commonPool-worker-2
356: main
356: ForkJoinPool.commonPool-worker-3
358: ForkJoinPool.commonPool-worker-2
359: main
359: ForkJoinPool.commonPool-worker-3
361: ForkJoinPool.commonPool-worker-2
362: main
362: ForkJoinPool.commonPool-worker-3
365: main
364: ForkJoinPool.commonPool-worker-2
365: ForkJoinPool.commonPool-worker-3
366: main
368: ForkJoinPool.commonPool-worker-2
369: main
369: ForkJoinPool.commonPool-worker-3
372: ForkJoinPool.commonPool-worker-3
371: ForkJoinPool.commonPool-worker-2
372: main
374: ForkJoinPool.commonPool-worker-3
374: ForkJoinPool.commonPool-worker-2
376: ForkJoinPool.commonPool-worker-3
377: ForkJoinPool.commonPool-worker-2
378: ForkJoinPool.commonPool-worker-3
376: main
379: ForkJoinPool.commonPool-worker-2
380: ForkJoinPool.commonPool-worker-3
382: ForkJoinPool.commonPool-worker-2
383: ForkJoinPool.commonPool-worker-3
384: ForkJoinPool.commonPool-worker-2
385: ForkJoinPool.commonPool-worker-3
383: main
386: ForkJoinPool.commonPool-worker-2
387: ForkJoinPool.commonPool-worker-3
390: ForkJoinPool.commonPool-worker-2
390: ForkJoinPool.commonPool-worker-3
392: ForkJoinPool.commonPool-worker-2
392: ForkJoinPool.commonPool-worker-3
390: main
394: ForkJoinPool.commonPool-worker-2
394: ForkJoinPool.commonPool-worker-3
397: ForkJoinPool.commonPool-worker-2
397: ForkJoinPool.commonPool-worker-3
399: ForkJoinPool.commonPool-worker-2
397: main
400: ForkJoinPool.commonPool-worker-3
401: ForkJoinPool.commonPool-worker-3
401: ForkJoinPool.commonPool-worker-2
404: ForkJoinPool.commonPool-worker-3
404: ForkJoinPool.commonPool-worker-2
403: main
406: ForkJoinPool.commonPool-worker-3
406: ForkJoinPool.commonPool-worker-2
408: main
409: ForkJoinPool.commonPool-worker-3
409: ForkJoinPool.commonPool-worker-2
411: main
412: ForkJoinPool.commonPool-worker-3
412: ForkJoinPool.commonPool-worker-2
414: main
414: ForkJoinPool.commonPool-worker-3
415: ForkJoinPool.commonPool-worker-2
418: ForkJoinPool.commonPool-worker-3
418: ForkJoinPool.commonPool-worker-2
419: ForkJoinPool.commonPool-worker-3
420: ForkJoinPool.commonPool-worker-2
421: ForkJoinPool.commonPool-worker-3
422: ForkJoinPool.commonPool-worker-2
423: ForkJoinPool.commonPool-worker-3
424: ForkJoinPool.commonPool-worker-2
425: ForkJoinPool.commonPool-worker-3
426: ForkJoinPool.commonPool-worker-2
427: ForkJoinPool.commonPool-worker-3
428: ForkJoinPool.commonPool-worker-2
430: ForkJoinPool.commonPool-worker-2
430: ForkJoinPool.commonPool-worker-3
432: ForkJoinPool.commonPool-worker-3
432: ForkJoinPool.commonPool-worker-2
433: ForkJoinPool.commonPool-worker-3
434: ForkJoinPool.commonPool-worker-2
435: ForkJoinPool.commonPool-worker-3
436: ForkJoinPool.commonPool-worker-2
437: ForkJoinPool.commonPool-worker-3
438: ForkJoinPool.commonPool-worker-2
439: ForkJoinPool.commonPool-worker-3
440: ForkJoinPool.commonPool-worker-2
441: ForkJoinPool.commonPool-worker-3
442: ForkJoinPool.commonPool-worker-2
443: ForkJoinPool.commonPool-worker-3
444: ForkJoinPool.commonPool-worker-2
445: ForkJoinPool.commonPool-worker-3
446: ForkJoinPool.commonPool-worker-2
447: ForkJoinPool.commonPool-worker-3
448: ForkJoinPool.commonPool-worker-2
449: ForkJoinPool.commonPool-worker-3
450: ForkJoinPool.commonPool-worker-2
451: ForkJoinPool.commonPool-worker-3
452: ForkJoinPool.commonPool-worker-2
453: ForkJoinPool.commonPool-worker-3
454: ForkJoinPool.commonPool-worker-2
456: ForkJoinPool.commonPool-worker-2
455: ForkJoinPool.commonPool-worker-3
458: ForkJoinPool.commonPool-worker-3
457: ForkJoinPool.commonPool-worker-2
459: ForkJoinPool.commonPool-worker-3
460: ForkJoinPool.commonPool-worker-2
461: ForkJoinPool.commonPool-worker-3
462: ForkJoinPool.commonPool-worker-2
463: ForkJoinPool.commonPool-worker-3
464: ForkJoinPool.commonPool-worker-2
465: ForkJoinPool.commonPool-worker-3
466: ForkJoinPool.commonPool-worker-2
467: ForkJoinPool.commonPool-worker-3
468: ForkJoinPool.commonPool-worker-2
469: ForkJoinPool.commonPool-worker-3
470: ForkJoinPool.commonPool-worker-2
471: ForkJoinPool.commonPool-worker-3
472: ForkJoinPool.commonPool-worker-2
473: ForkJoinPool.commonPool-worker-3
474: ForkJoinPool.commonPool-worker-2
475: ForkJoinPool.commonPool-worker-3
476: ForkJoinPool.commonPool-worker-2
477: ForkJoinPool.commonPool-worker-3
478: ForkJoinPool.commonPool-worker-2
479: ForkJoinPool.commonPool-worker-3
480: ForkJoinPool.commonPool-worker-2
482: ForkJoinPool.commonPool-worker-2
481: ForkJoinPool.commonPool-worker-3
484: ForkJoinPool.commonPool-worker-3
484: ForkJoinPool.commonPool-worker-2
327: ForkJoinPool.commonPool-worker-1
485: ForkJoinPool.commonPool-worker-3
486: ForkJoinPool.commonPool-worker-2
488: ForkJoinPool.commonPool-worker-3
488: ForkJoinPool.commonPool-worker-1
489: ForkJoinPool.commonPool-worker-2
490: ForkJoinPool.commonPool-worker-3
491: ForkJoinPool.commonPool-worker-1
492: ForkJoinPool.commonPool-worker-2
495: ForkJoinPool.commonPool-worker-2
493: ForkJoinPool.commonPool-worker-3
496: ForkJoinPool.commonPool-worker-2
497: ForkJoinPool.commonPool-worker-3
499: ForkJoinPool.commonPool-worker-3
500: ForkJoinPool.commonPool-worker-3
501: ForkJoinPool.commonPool-worker-3
502: ForkJoinPool.commonPool-worker-3
503: ForkJoinPool.commonPool-worker-3
504: ForkJoinPool.commonPool-worker-3
505: ForkJoinPool.commonPool-worker-3
506: ForkJoinPool.commonPool-worker-3
507: ForkJoinPool.commonPool-worker-3
508: ForkJoinPool.commonPool-worker-3
509: ForkJoinPool.commonPool-worker-3
510: ForkJoinPool.commonPool-worker-3
511: ForkJoinPool.commonPool-worker-3
127: main

12
enums/Item.java Normal file
View File

@ -0,0 +1,12 @@
// enums/Item.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
public interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}

21
enums/Paper.java Normal file
View File

@ -0,0 +1,21 @@
// enums/Paper.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
import static enums.Outcome.*;
public class Paper implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return DRAW; }
@Override
public Outcome eval(Scissors s) { return WIN; }
@Override
public Outcome eval(Rock r) { return LOSE; }
@Override public String toString() {
return "Paper";
}
}

View File

@ -6,59 +6,6 @@
// {java enums.RoShamBo1}
package enums;
import java.util.*;
import static enums.Outcome.*;
interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}
class Paper implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return DRAW; }
@Override
public Outcome eval(Scissors s) { return WIN; }
@Override
public Outcome eval(Rock r) { return LOSE; }
@Override public String toString() {
return "Paper";
}
}
class Scissors implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return LOSE; }
@Override
public Outcome eval(Scissors s) { return DRAW; }
@Override
public Outcome eval(Rock r) { return WIN; }
@Override public String toString() {
return "Scissors";
}
}
class Rock implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return WIN; }
@Override
public Outcome eval(Scissors s) { return LOSE; }
@Override
public Outcome eval(Rock r) { return DRAW; }
@Override public String toString() {
return "Rock";
}
}
public class RoShamBo1 {
static final int SIZE = 20;

21
enums/Rock.java Normal file
View File

@ -0,0 +1,21 @@
// enums/Rock.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
import static enums.Outcome.*;
public class Rock implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return WIN; }
@Override
public Outcome eval(Scissors s) { return LOSE; }
@Override
public Outcome eval(Rock r) { return DRAW; }
@Override public String toString() {
return "Rock";
}
}

21
enums/Scissors.java Normal file
View File

@ -0,0 +1,21 @@
// enums/Scissors.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
import static enums.Outcome.*;
public class Scissors implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return LOSE; }
@Override
public Outcome eval(Scissors s) { return DRAW; }
@Override
public Outcome eval(Rock r) { return WIN; }
@Override public String toString() {
return "Scissors";
}
}

View File

@ -58,6 +58,12 @@ project(':collections') {
}
}
project(':patterns') {
dependencies {
compile project(':enums')
}
}
configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')

View File

@ -6,72 +6,15 @@
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import enums.Outcome;
import static enums.Outcome.*;
import enums.Item;
import enums.Paper;
import enums.Scissors;
import enums.Rock;
import onjava.*;
import static onjava.Tuple.*;
enum Outcome { WIN, LOSE, DRAW }
interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}
class Paper implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override public Outcome eval(Paper p) {
return Outcome.DRAW;
}
@Override public Outcome eval(Scissors s) {
return Outcome.WIN;
}
@Override public Outcome eval(Rock r) {
return Outcome.LOSE;
}
@Override public String toString() {
return "Paper";
}
}
class Scissors implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override public Outcome eval(Paper p) {
return Outcome.LOSE;
}
@Override public Outcome eval(Scissors s) {
return Outcome.DRAW;
}
@Override public Outcome eval(Rock r) {
return Outcome.WIN;
}
@Override public String toString() {
return "Scissors";
}
}
class Rock implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override public Outcome eval(Paper p) {
return Outcome.WIN;
}
@Override public Outcome eval(Scissors s) {
return Outcome.LOSE;
}
@Override public Outcome eval(Rock r) {
return Outcome.DRAW;
}
@Override public String toString() {
return "Rock";
}
}
class ItemFactory {
static List<Supplier<Item>> items =
Arrays.asList(