Presentation is loading. Please wait.

Presentation is loading. Please wait.

“Don’t call us, we’ll call you”

Similar presentations


Presentation on theme: "“Don’t call us, we’ll call you”"— Presentation transcript:

1 “Don’t call us, we’ll call you”
The Template Pattern By Konstantinos Liolios For CSPP51023 5/7/2019 Template Pattern

2 Intent It is a behavioral pattern
Define a parent class and leave the implementation of some methods to subclasses but guide this process unlike the strategy pattern 5/7/2019 Template Pattern

3 MOTIVATION Code reusability and polymorphism
Factor out common elements Do not reinvent the wheel Most likely you have used it already w/o knowing Every time you implement methods in subclasses you are using the Template Pattern 5/7/2019 Template Pattern

4 Implementation Issues
Operations which must be overridden by a subclass should be made abstract If the template method itself should not be overridden by a subclass, it should be made final To allow a subclass to insert code at a specific spot in the operation of the algorithm, insert “hook” operations into the template method. These hook operations may do nothing by default. Try to minimize the number of operations that a subclass must override, otherwise using the template method becomes tedious for the developer In a template method, the parent class calls the operations of a subclass and not the other way around. This is an inverted control structure that's sometimes referred to as "the Hollywood principle," as in, "Don't call us, we'll call you". 5/7/2019 Template Pattern

5 Criticism Communicates intent poorly
Difficult to compose functionality Difficult to comprehend program flow Difficult to maintain 5/7/2019 Template Pattern

6 Problem You open a database handle many times in many of your problems and you slightly change the same code every time you connect to a different RDBMS (Oracle, Postgress etc.) 5/7/2019 Template Pattern

7 Solution Factor out the common methods to a parent abstract class
Create a template class implementing only the use-case specific methods and inherit the “concrete ones” 5/7/2019 Template Pattern

8 (4) Template Methods Concrete Methods
Complete methods that do some basic functionality and will be inherited by all derived classes (ec. do a handshake with a server app or release a filehandle) 5/7/2019 Template Pattern

9 (4) Template Methods Abstract Methods
Methods that need to be implemented on the derived classes For example: Draw an isosceles circle in a jpanel, or draw a right triangle 5/7/2019 Template Pattern

10 (4) Template Methods Hook Operations
Methods that “may” be overidden in the derived classes For example: Write a default abstract method. 5/7/2019 Template Pattern

11 (4) Template Methods Finally methods that combine all the previous ones For example implement the algorithm 5/7/2019 Template Pattern

12 Solution Description/UML
5/7/2019 Template Pattern

13 The Template Way 5/7/2019 Template Pattern import java.sql.*;
public class OpenDatabaseHandle { abstract class DatabaseHandle(String connectionString, String userID, String Password){ public ResultSet result; public final ResultSet Connect(connectionString, defaultUserID, defaultPassword){ OpenConnection(); ExecuteQuery(); ReturnResultSet(); }; abstract void OpenConnection(){ String defaultConnectionString= + connectionString; Connection con = DriverManager.getConnection(defaultConnectionString, userID, password); } public void ExecuteQuery(String sqlQuery) { Statement select = con.createStatement(); result = select.executeQuery (“sqlQuery"); select.close(); con.close(); public ResultSet ReturnResultSet() return result; class MysqlHandle extends DatabaseHandle { Public MysqlHandle(String connectionString, String userID, String password) { super(connectionString); public OpenConnection() String MySQLconnectionString=“jdbc:msql://” + connectionString; Connection con = DriverManager.getConnection(“MySQLconnectionString”, userID, password); 5/7/2019 Template Pattern

14 5/7/2019 Template Pattern


Download ppt "“Don’t call us, we’ll call you”"

Similar presentations


Ads by Google