Java Design Patterns .

Java Design Patterns Template

Template (aka "Don't call us, we'll call you") Overview

An abstract class defines various methods, and has one non-overridden method which calls the various methods. - the abstract Template

public abstract class TitleInfo {  
   private String titleName;
   //the "template method" - 
   //  calls the concrete class methods, is not overridden
   public final String ProcessTitleInfo() {
       StringBuffer titleInfo = new StringBuffer();

titleInfo.append(this.getTitleBlurb()); titleInfo.append(this.getDvdEncodingRegionInfo()); return titleInfo.toString(); } //the following 2 methods are "concrete abstract class methods" public final void setTitleName(String titleNameIn) { this.titleName = titleNameIn; } public final String getTitleName() { return this.titleName; } //this is a "primitive operation", // and must be overridden in the concrete templates public abstract String getTitleBlurb(); //this is a "hook operation", which may be overridden, //hook operations usually do nothing if not overridden public String getDvdEncodingRegionInfo() { return " "; } }
To download source right-click here and "Save As...". - one of three concrete templates

public class DvdTitleInfo extends TitleInfo {  
   private String star;
   private char encodingRegion;
   public DvdTitleInfo(String titleName, 
                       String star, 
                       char encodingRegion) {
   public void setStar(String starIn) { = starIn;
   public String getStar() {
   public void setEncodingRegion(char encodingRegionIn) {
       this.encodingRegion = encodingRegionIn;
   public char getEncodingRegion() {
       return this.encodingRegion;
   public String getTitleBlurb() {
       return ("DVD: " + this.getTitleName() + 
               ", starring " + this.getStar());
   public String getDvdEncodingRegionInfo() {
       return (", encoding region: " + this.getEncodingRegion());
To download source right-click here and "Save As...". - two of three concrete templates

public class BookTitleInfo extends TitleInfo {  
   private String author;
   public BookTitleInfo(String titleName, String author) {
   public void setAuthor(String authorIn) { = authorIn;}
   public String getAuthor() {return;}   
   public String getTitleBlurb() {
       return ("Book: " + this.getTitleName() + 
               ", Author: " + this.getAuthor());
To download source right-click here and "Save As...". - three of three concrete templates

public class GameTitleInfo extends TitleInfo {  
   public GameTitleInfo(String titleName) {
   public String getTitleBlurb() {
       return ("Game: " + this.getTitleName());
To download source right-click here and "Save As...". - testing the Template

class TestTitleInfoTemplate {
   public static void main(String[] args) {
       TitleInfo bladeRunner = 
         new DvdTitleInfo("Blade Runner", 
                          "Harrison Ford", '1'); 
       TitleInfo electricSheep = 
         new BookTitleInfo("Do Androids Dream of Electric Sheep?", 
                           "Phillip K. Dick");        
       TitleInfo sheepRaider = 
         new GameTitleInfo("Sheep Raider");
       System.out.println(" ");       
       System.out.println("Testing bladeRunner   " +   
       System.out.println("Testing electricSheep " + 
       System.out.println("Testing sheepRaider   " +   
To download source right-click here and "Save As...".

Test Results

Testing bladeRunner DVD: 
Blade Runner, starring Harrison Ford, encoding region: 1
Testing electricSheep Book: 
Do Androids Dream of Electric Sheep?, Author: Phillip K. Dick 
Testing sheepRaider Game: 
Sheep Raider 


UML for Template


Portland Pattern Repository
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Sign in to be the first to comment on Java Design Patterns Template.