Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template Behavioral Pattern By Christopher Young 04/12/10 Bowring.

Similar presentations


Presentation on theme: "Template Behavioral Pattern By Christopher Young 04/12/10 Bowring."— Presentation transcript:

1 Template Behavioral Pattern By Christopher Young 04/12/10 Bowring

2 Template Pattern Overview Definition: The Template Method Behavioral Pattern defines a program skeleton (based around a single, important algorithm) that is placed in an Abstract Class which contains a number of abstract methods that will later be overridden by subclasses (or Concrete Classes) to create it's own concrete functions while still having the same basic functioning as the Abstract class. Abstract Class: The Abstract class is a class that houses a Template Method as well as other abstract methods that can later be modified. Template Method: This template method is the overall, backbone of the entire pattern. It encapsulates an algorithm that all subclasses are expected to use and follow. Concrete Class: Any subclass that extends the abstract class. These concrete classes have to abide by the template method but have the freedom to implement (in any way of their choosing) other abstract methods that can be given in the abstract class.

3

4 Advantages/Disadvantages Advantages: - Allows concrete, subclasses to be customizable (modifiable) yet still contain the main algorithm/function that is defined in its super, abstract class - The use of 'hook operations' which are method calls located inside the template method that calls out to abstract methods that can be implemented by subclasses therefore giving the concrete classes a safe level of modifiability to the algorithm while not compromising its functionality. -Code Reuse and the localization of common behaviors Disadvantages: - Hard to maintain - Difficulty in following program flow (which in turn makes debugging more difficult) - High dependency on sibling, parent classes that could potentially cause system wide problems

5 Java Implementation /** * This class is the abstract class called Band. It represents a template that will be used to * help define any number of bands or venues that want to performShow (the algorithm) and * everything else is the 'skeleton' that will later be implemented in concrete classes */ abstract class Band { protected String bandName; protected String venueName; //This template method is the backbone, if you will, of the entire Band class. No matter how different the //subclasses become, this method will always be the same public final void performShow( String name, String venue ) { this.bandName = name; this.venueName = venue; setupEquipment(); beginPlaying(); int s = (int) numOfRemainingSongs(); while( ! EndOfShow() ) { playSong(s); s--; } packUpEquipment(); }

6 Java Implementation (cont.) //Abstract methods that subclasses will uniquely implement abstract void setupEquipment(); abstract void beginPlaying(); abstract boolean endOfShow(); abstract int numOfRemainingSongs(); abstract void playSong( int songIndex ); abstract void packUpEquipment(); }

7 Java Implementation (cont.) /** * This is the first subclass that extends the abstract Band class. It uses the skeleton of the Band class * and then create its own behavior (aside from performShow which should never be overridden) * */ public class Pink Floyd extends Band { int setList = 14; public void setupEquipment() { System.out.println(“Pink Floyd has 4 members: a drummer, guitarist, bassist and keyboardist, therefore a drumset, amps, guitars/basses, and a piano are required for setup.”); } public void beginPlaying() { System.out.println(“The band appears behind a giant wall that separates them from the audience and as they begin to play, the wall begins to fall apart, exposing the live show.”); } public boolean endOfShow() { return numOfRemainingSongs = = 0; } public int numOfRemainingSongs() { return setList; }

8 Java Implementation (cont.) public void playSong ( int index) System.out.println(“Pink Floyd performs a song. “ + index + “ songs are left in the setlist ); } public void packUpEquipment () { System.out.println(“Pink Floyd has concluded their show and begin to pack up equipment for next show.”); } //This class can also contain newly added methods that are not found in its super class, Band }


Download ppt "Template Behavioral Pattern By Christopher Young 04/12/10 Bowring."

Similar presentations


Ads by Google