Dont call us well call you The Template

  • Slides: 14
Download presentation
“Don’t call us, we’ll call you” The Template Pattern By Konstantinos Liolios For CSPP

“Don’t call us, we’ll call you” The Template Pattern By Konstantinos Liolios For CSPP 51023 12/29/2021 Template Pattern 1

Intent n n It is a behavioral pattern Define a parent class and leave

Intent n n 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 12/29/2021 Template Pattern 2

MOTIVATION n n n Code reusability and polymorphism Factor out common elements Do not

MOTIVATION n n n 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 12/29/2021 Template Pattern 3

Implementation Issues n n n Operations which must be overridden by a subclass should

Implementation Issues n n n 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". 12/29/2021 Template Pattern 4

Criticism n n Communicates intent poorly Difficult to compose functionality Difficult to comprehend program

Criticism n n Communicates intent poorly Difficult to compose functionality Difficult to comprehend program flow Difficult to maintain 12/29/2021 Template Pattern 5

Problem n You open a database handle many times in many of your problems

Problem n 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. ) 12/29/2021 Template Pattern 6

Solution n n Factor out the common methods to a parent abstract class Create

Solution n n 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” 12/29/2021 Template Pattern 7

(4) Template Methods n Concrete Methods Complete methods that do some basic functionality and

(4) Template Methods n 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) 12/29/2021 Template Pattern 8

(4) Template Methods n Abstract Methods that need to be implemented on the derived

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

(4) Template Methods n Hook Operations Methods that “may” be overidden in the derived

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

(4) Template Methods n Finally methods that combine all the previous ones For example

(4) Template Methods n Finally methods that combine all the previous ones For example implement the algorithm 12/29/2021 Template Pattern 11

Solution Description/UML 12/29/2021 Template Pattern 12

Solution Description/UML 12/29/2021 Template Pattern 12

The Template Way import java. sql. *; public class Open. Database. Handle { abstract

The Template Way import java. sql. *; public class Open. Database. Handle { abstract class Database. Handle(String connection. String, String user. ID, String Password){ public Result. Set result; public final Result. Set Connect(connection. String, , default. User. ID, default. Password){ Open. Connection(); Execute. Query(); Return. Result. Set(); }; abstract void Open. Connection(){ String default. Connection. String= : @” + connection. String; default. Connection. String= “jdbc: oracle: thin: @” connection. String; Connection con = Driver. Manager. get. Connection(default. Connection. String , user. ID, password); } public void Execute. Query(String sql. Query) { Statement select = con. create. Statement(); result = select. execute. Query (“sql. Query "); (“sql. Query"); select. close(); con. close(); } public Result. Set Return. Result. Set() { return result; } } class Mysql. Handle extends Database. Handle { Public Mysql. Handle(String connection. String, String user. ID, String password) { super(connection. String ); public Open. Connection() { String My. SQLconnection. String=“ jdbc: msql: //” + connection. String; My. SQLconnection. String=“jdbc: msql connection. String; Connection con = Driver. Manager. get. Connection(“My. SQLconnection. String ”, user. ID, password); } } 12/29/2021 Template Pattern 13

12/29/2021 Template Pattern 14

12/29/2021 Template Pattern 14