0% found this document useful (0 votes)
91 views3 pages

Public Abstract Class Public Void : //default Implementation

The document shows an example of the template method design pattern. It defines an abstract base class Game that contains a template method play() which defines the generic steps to play a game (initialize, startPlay, endPlay). This method is final so it cannot be overridden. The abstract methods startPlay and endPlay are implemented by concrete subclasses like Cricket to define specific game logic. The template method ensures each game follows the same overall structure.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views3 pages

Public Abstract Class Public Void : //default Implementation

The document shows an example of the template method design pattern. It defines an abstract base class Game that contains a template method play() which defines the generic steps to play a game (initialize, startPlay, endPlay). This method is final so it cannot be overridden. The abstract methods startPlay and endPlay are implemented by concrete subclasses like Cricket to define specific game logic. The template method ensures each game follows the same overall structure.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 1:

public​ ​abstract​ ​class​ HouseTemplate {

public​ final ​void​ buildHouse(){


buildFoundation();
buildPillars();
buildWalls();
buildWindows();
}

//default implementation
void​ buildWindows() {
System.​out​.println(​"Building Glass Windows"​);
}

//methods to be implemented by subclasses


public​ ​abstract​ ​void​ buildWalls();
public​ ​abstract​ ​void​ buildPillars();

final​ ​void​ buildFoundation() {


System.​out​.println(​"Building foundation with cement,iron rods
and sand"​);
}
}

public​ ​class​ WoodenHouse ​extends​ HouseTemplate {

@Override
public​ ​void​ buildWalls​()​ {
System.out.println(​"Building Wooden Walls"​);
}

@Override
public​ ​void​ buildPillars​()​ {
System.out.println(​"Building Pillars with Wood coating"​);
}
Exercise 2:
}

public​ ​abstract​ ​class​ ​Game​ ​{


​void​ initialize​(){System.out.println(“init the game”)}
​abstract​ ​void​ startPlay​();
​abstract​ ​void​ endPlay​();

​//template method
​public​ ​final​ ​void​ play​(){

​//initialize the game


initialize​();

​//start game
startPlay​();

​//end game
endPlay​();
​}
}

public​ ​class​ ​Cricket​ ​extends​ ​Game​ {


​@Override
​void​ endPlay​()​ ​{
​System​.​out​.​println​(​"Cricket Game Finished!"​);
​ }

​@Override
​void​ startPlay​()​ ​{
​System​.​out​.​println​(​"Cricket Game Started. Enjoy the game!"​);
​ }
}
Exercise 3:

abstract public class DataParser {

//Template method
//This method defines a generic structure for parsing data
public void parseDataAndGenerateOutput()
{
readData();
processData();
writeData();
}
//This methods will be implemented by its subclass
abstract void readData();
abstract void processData();

//We have to write output in a CSV file so this step will be same for all
subclasses
public void writeData()
{
System.out.println('Output generated,writing to CSV');
}
}
public class CSVDataParser extends DataParser {

void readData() {
System.out.println('Reading data from csv file');
}
void processData() {
System.out.println('Looping through loaded csv file');
}
}

public class DatabaseDataParser extends DataParser {

void readData() {
System.out.println('Reading data from database');
}

void processData() {
System.out.println('Looping through datasets');
}
}

You might also like