Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Design and Architecture

Similar presentations


Presentation on theme: "Software Design and Architecture"— Presentation transcript:

1 Software Design and Architecture
Behavioral Design Patterns Muhammad Nasir

2 Template Method Intent
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

3 Template Method - Motivation
Consider an application framework that provides Application and Document classes. The Application class is responsible for opening existing documents such as a file. A Document object represents the information in a document once it's read from the file. Applications built with this framework can subclass Application and Document to suit specific needs.

4 Template Method For example, a drawing application defines Draw-Application and Draw-Document subclasses. A spread sheet application defines Spread-sheet-Application and Spread-sheet-Document subclasses. We call OpenDocument a template method. A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior.

5 Template Method - Motivation

6 Template - Applicability
To implement the invariant parts of an algorithm once and leave it upto subclasses to implement the behavior that can vary When common behavior among subclasses should be factored and localized in a common class to avoid code duplication To control subclasses extensions using the template method. Template method calls Hook operations there by permitting extensions at those points The Hollywood Principle –”Don’t call us, we will call you”

7 Template – Hollywood Principle
The Hollywood Principle allows low-level components to hook themselves into a system, but the high-level components determine when they are needed, and how. In other words, the high-level components tell the low-level components “don’t call us, we’ll call you”.

8 Template Method - Structure

9 Template Method - Participants
AbstractClass (Application) defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm. Implements a template method defining the skeleton of an algorithm. ConcreteClass (MyApplication) Implements the primitive operations to carry out subclass-specific steps of the algorithm.

10 Template Method - Collaboration
ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm.

11 Template Method – Sample Code

12 Template Method – Sample Code

13 Template Method – Sample Code

14 Template Method - Consequences
Template methods are a fundamental technique for code reuse. Avoid code duplication Control Sub-Classing

15 Template Method – Related Patterns
Factory Methods are often called by template methods. In the Motivation example, the factory method CreateDocument is called by the template method OpenDocument. Strategy: Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

16 The End Thanks for listening Questions would be appreciated…


Download ppt "Software Design and Architecture"

Similar presentations


Ads by Google