Created
May 21, 2017 09:08
-
-
Save fermopili/ceaf37d23b9f8e677e58f9ba4175ce2f to your computer and use it in GitHub Desktop.
com.javarush.task.task37.task3712 Шаблонный метод
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3712; | |
public class Basketball extends Game | |
{ | |
public void prepareForTheGame() | |
{ | |
System.out.println ( "Preparing for the Basketball game..." ); | |
} | |
public void playGame() | |
{ | |
System.out.println ( "Playing basketball!" ); | |
} | |
public void congratulateWinner() | |
{ | |
System.out.println ( "Give it up to the NBA finals champions!!!" ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3712; | |
public class Football extends Game{ | |
public void prepareForTheGame() { | |
System.out.println("Preparing for the Football game..."); | |
} | |
public void playGame() { | |
System.out.println("Kickoff!!! Playing football!"); | |
} | |
public void congratulateWinner() { | |
System.out.println("Here is the new football CHAMPION!!!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3712; | |
/** | |
* Created by Administrator on 21.05.2017. | |
*/ | |
public abstract class Game | |
{ | |
abstract public void prepareForTheGame(); | |
abstract public void playGame(); | |
abstract public void congratulateWinner(); | |
public void run() | |
{ | |
prepareForTheGame ( ); | |
playGame ( ); | |
congratulateWinner ( ); | |
} | |
; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3712; | |
import java.util.ArrayList; | |
import java.util.List; | |
/* | |
Шаблонный метод | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
List<Game> games = new ArrayList<>(); | |
games.add(new Football()); | |
games.add(new Basketball()); | |
games.add(new Tennis()); | |
for(Game game : games) { | |
game.run(); | |
System.out.println("---------------------------------------------"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3712; | |
public class Tennis extends Game{ | |
public void prepareForTheGame() { | |
System.out.println("Preparing for the Tennis game..."); | |
} | |
public void playGame() { | |
System.out.println("Playing tennis!"); | |
} | |
public void congratulateWinner() { | |
System.out.println("Rafael Nadal wins once again!!!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task37.task3712" | |
Шаблонный метод | |
Классы Football, Basketball и Tennis описывают три популярные игры. | |
Спроси у своего любимого поисковика о паттерне Template method (Шаблонный метод) и сделай так, | |
чтобы код написанный в методе main класса Solution имел смысл. | |
P.S. Класс Game должен быть абстрактным. | |
Требования: | |
1. Классы Football, Basketball и Tennis должны быть потомками класса Game. | |
2. В классе Game должны быть определены три абстрактных метода prepareForTheGame, playGame и congratulateWinner. | |
3. В классе Game должен быть реализован метод run, вызывающий методы prepareForTheGame, playGame и congratulateWinner в правильном порядке. | |
4. Класс Game должен быть абстрактным. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment