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 60 61 62 63
| abstract class Game { abstract void initialize();
abstract void startPlay();
abstract void endPlay();
public final void play() { initialize(); startPlay(); endPlay(); } }
class Cricket extends Game {
@Override void endPlay() { System.out.println("Cricket Game Finished!"); }
@Override void initialize() { System.out.println("Cricket Game Initialized! Start playing."); }
@Override void startPlay() { System.out.println("Cricket Game Started. Enjoy the game!"); } }
class Football extends Game {
@Override void endPlay() { System.out.println("Football Game Finished!"); }
@Override void initialize() { System.out.println("Football Game Initialized! Start playing."); }
@Override void startPlay() { System.out.println("Football Game Started. Enjoy the game!"); } }
public class TemplatePatternDemo { public static void main(String[] args) { Game game = new Cricket(); game.play(); System.out.println(); game = new Football(); game.play(); } }
|