Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. public abstract class Game {
  2.     abstract void initialize();
  3.     abstract void startPlay();
  4.     abstract void endPlay();
  5.  
  6.     public final void play() {
  7.         initialize();
  8.         startPlay();
  9.         endPlay();
  10.     }
  11.     public void timeOut(){
  12.         System.out.println("TIME-OUT!");
  13.     }  
  14. }
  15.  
  16. public class Soccer extends Game {
  17.     public void initialize() {
  18.         System.out.println("Get soccerball and soccergoals");
  19.     }
  20.     public void startPlay() {
  21.         System.out.println("Begin first half");
  22.         timeOut();
  23.         System.out.println("Begin second half");
  24.     }
  25.     public void endPlay() {
  26.         System.out.println("Count the score,"
  27.                 + " and put the ball away");
  28.     }
  29. }
  30.  
  31. public class Tennis extends Game {
  32.     private int sets = 3;
  33.    
  34.     public void initialize() {
  35.         System.out.println("Get tennisracket and tennisball");
  36.     }  
  37.     public void startPlay() {
  38.         for(int i = 0; i<sets; i++){
  39.             System.out.println("Begin set");
  40.             timeOut();
  41.         }
  42.     }
  43.     public void endPlay() {
  44.         System.out.println("Put tennisracket and tennisball away");
  45.     }
  46. }
  47.  
  48. public class TemplatePatternDemo {
  49.     public static void main(String[] args) {
  50.         Game tennisGame = new Tennis();
  51.         tennisGame.play();
  52.        
  53.         System.out.println("");
  54.        
  55.         Game soccerGame = new Soccer();
  56.         soccerGame.play();
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement