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

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
The Template Method By Sinclair Schuller. What is the Template Method? “Skeleton” definition of an algorithm Allows redefinition of predetermined points.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
CSC 212 Object-Oriented Programming and Java Part 1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Classes, Interfaces and Packages
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Topic: Classes and Objects
Sections Inheritance and Abstract Classes
Template Method Pattern Iterator Pattern
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
null, true, and false are also reserved.
COMPUTER 2430 Object Oriented Programming and Data Structures I
More About Inheritance & Interfaces
Object-Oriented Programming
Building Java Programs
Building Java Programs
“Don’t call us, we’ll call you”
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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

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.

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

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(); }

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(); }

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; }

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 }