Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template Method Pattern Iterator Pattern

Similar presentations


Presentation on theme: "Template Method Pattern Iterator Pattern"— Presentation transcript:

1 Template Method Pattern Iterator Pattern
CSE-110 (Fall 2016) Based Upon Wikipedia’s Coverage of the Patterns

2 Template Method Pattern
In the template method of this design pattern, one or more algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed

3 The Pattern

4 Usage The template method is used in frameworks, where each implements the invariant parts of a domain's architecture, leaving "placeholders" for customisation options. This is an example of inversion of control. The template method is used for the following reasons:[5] Let subclasses implement (through method overriding) behavior that can vary. Avoid duplication in the code: the general workflow structure is implemented once in the abstract class's algorithm, and necessary variations are implemented in each of the subclasses. Control at what point(s) subclassing is allowed. As opposed to a simple polymorphic override, where the base method would be entirely rewritten allowing radical change to the workflow, only the specific details of the workflow are allowed to change.

5 Java Example abstract class Game { /* Hook methods. Concrete implementation may differ in each subclass*/ protected int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner(); /* A template method : */ public final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; } printWinner(); }}

6 Java Example, continued
void makePlay(int player) { // Process one turn of player } boolean endOfGame() { // Return true if game is over // according to Monopoly rules void printWinner() { // Display who won /* Specific declarations for the Monopoly game. */ // ... //Now we can extend this class in order //to implement actual games: class Monopoly extends Game { /* Implementation of necessary concrete methods */ void initializeGame() { // Initialize players // Initialize money }

7 Java Example, continued
class Chess extends Game { /* Implementation of necessary concrete methods */ void initializeGame() { // Initialize players // Put the pieces on the board } void makePlay(int player) { // Process a turn for the player boolean endOfGame() { // Return true if in Checkmate or // Stalemate has been reached } void printWinner() { // Display the winning player /* Specific declarations for the chess game. */ // ...

8 Iterator Pattern In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. For example, the hypothetical algorithm SearchForElement can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows SearchForElement to be used on any container that supports the required type of iterator.

9 Definition The essence of the Iterator Factory method Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation."

10 Java Iterator Interface Iterable Interface hasNext() or next()
“enhanced for loop”, e.g. “for each loop” for item: items Iterable Interface iterator() returns an Iterator


Download ppt "Template Method Pattern Iterator Pattern"

Similar presentations


Ads by Google