GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — OnJava8-Examples/interfaces/Games.java at master · lliittllee/OnJava8-Examples Expand file tree/ Copy pathGames.javaMore file actions 59 lines (53 loc) · 1.3 KB / Copy pathGames.javaFile metadata and controls59 lines (53 loc) · 1.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 // interfaces/Games.java // (c)2017 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. // A Game framework using Factory Methods interface Game { boolean move(); } interface GameFactory { Game getGame(); } class Checkers implements Game { private int moves = 0; private static final int MOVES = 3; @Override public boolean move() { System.out.println("Checkers move " + moves); return ++moves != MOVES; } } class CheckersFactory implements GameFactory { @Override public Game getGame() { return new Checkers(); } } class Chess implements Game { private int moves = 0; private static final int MOVES = 4; @Override public boolean move() { System.out.println("Chess move " + moves); return ++moves != MOVES; } } class ChessFactory implements GameFactory { @Override public Game getGame() { return new Chess(); } } public class Games { public static void playGame(GameFactory factory) { Game s = factory.getGame(); while(s.move()) ; } public static void main(String[] args) { playGame(new CheckersFactory()); playGame(new ChessFactory()); } } /* Output: Checkers move 0 Checkers move 1 Checkers move 2 Chess move 0 Chess move 1 Chess move 2 Chess move 3 */ |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |