1 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method AJAX! KC Web & Java 29 november 2005.

Slides:



Advertisements
Similar presentations
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
1 juni 30, 2005 Spring JDBC. 2 juni 30, 2005 Doel Database acties Flexibel Gecontroleerd Productief.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
JDBC – Java DataBase Connectivity CSE432 Object Oriented Software Engineering.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent:
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
1 Introduction to Java Development with IDS Jean Georges Perrin IIUG GreenIvory.com JGP.net Tuesday, October 3 rd :00 – 10:00. Platform: IDS, Java.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
Introduction to Java Development with IDS Jean Georges Perrin IIUG I04 Tuesday, October 3 rd :00 – 10:00. Platform: IDS, Java.
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
Spring Data Access By, Srinivas Reddy.S
MDCFUG Is Java in Your Future? Tyler Williams Principal dataTerrace
Boiler Plating Database Resource Cleanup With Execute Around Method ACCU London February 2009 ACCU Conference April 2009 Validus 8 Thorpe Road Norwich.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Java Naming and Directory Interface Matt. 2 What is JNDI.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
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.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring JDBC Simplifying.
Spring JDBC Dima Ionut Daniel. Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Database Activity CEMC Steps 1. Verify Data Tools Platform is installed in Eclipse 2. Configure a Connection to Derby Create necessary table for.
JDBC – Java DataBase Connectivity
Java Access to RDB Relational databases (RDBs) dominate today, due to:
Lec - 14.
JDBC 15-Apr-18.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
How to be a Good Developer
Behavioral Design Patterns
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC – Java DataBase Connectivity
JDBC – Java DataBase Connectivity
JDBC 15-Nov-18.
Introduction to Behavioral Patterns (3)
Interacting with Database
Making Text for the Web part 6
Spring 프레임워크의 이해 5. Spring Abstract API.
JDBC Example.
JDBC – Java DataBase Connectivity
Presentation transcript:

1 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method AJAX! KC Web & Java 29 november 2005

2 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method 1-0

3 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method J2SE Design Patterns: Het Template Pattern Lucas Jellema KC Web & Java – 10 Minute Talks, Donderdag 29 november 2005

4 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Introducing Design Patterns Recurring algorithms, construction, designs Best practices Associated with various technologies  ERD  Database Design  J2EE Architecture MVC, Business Delegate, Façade, …  Non IT environments  J2SE – Java Programming Most famous: ….  Singleton

5 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Great Book: Head First Design Patterns Observer Decorator Factory Singleton Command Adapter and Façade Iterator and Composite State Proxy Compound

6 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method The Template Pattern aka Don’t Call us, We will call You! TeaMaker getCup boilWater steepTeaBag pourInCup optional addSugar optional addLemon CoffeeMaker getCup boilWater brewCoffee pourInCup optional addSugar optional addMilk CoffeeVerkeerdMaker getCup boilWater brewCoffee pourInCup addMilk optional addSugar getCup boilWater brew WHATEVER pourInCup addCondiments Generic pattern

7 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Template Method Pattern template method: prepareRecipe

8 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method The Template Pattern The Template (Method) Pattern  Defines the skeleton of an algorithm in a method, deferring some steps or pieces to sub-classes  Lets subclasses (re)define part of an algorithm without changing its structure (abstract) superclass algorithm final abstract hook (concrete) subclass

9 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Template Method Pattern template method: prepareRecipe

10 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method The Template Pattern The Template (Method) Pattern  Three modes Forcing subclasses to implement certain steps (abstract or interface) Allowing subclasses to complement certain steps (through overridable hooks) Allowing subclasses to redefine steps (through overridable algorithm methods)  Non-overridable steps are defined final in the superclass

11 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Template Method: BeverageMachine.prepareRecipe()

12 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method package nl.amis.shop.mypackage; public class CoffeeShop { public CoffeeShop() { } public void drinkBeverage( BeverageMachine bm) { bm.prepareRecipe(); System.out.println("Enjoy your drink\n"); } public static void main(String[] args) { CoffeeShop coffeeShop = new CoffeeShop(); coffeeShop.drinkBeverage(new CoffeeMachine()); coffeeShop.drinkBeverage(new TeaMachine()); coffeeShop.drinkBeverage(new CoffeeWrongMachine()); }//main } Using the Template Method My Little Coffeshop

13 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Examples of the Template (Method) Pattern Don’t Call Us – We Will Call You Spring JDBC Arrays.sort(Object[] a)  Not a Superclass/Subclass situation  To use sort on an array, the array elements must implement the Comparable interface by defining the compareTo() method java.io.InputStream – abstract read() method that subclasses must implement Swing  JFrame – paint() method: a hook with an empty default implementation  Applet – start(), stop(), destroy(): hooks ADF DataAction  override onCommit, add onMyEvent

14 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Examples of the Template (Method) Pattern Outside the Java Arena Oracle Forms event-triggers  Pre-Insert, Post-Insert – hooks surrounding the Insert algorithm  even On-Insert that allows overriding of the core of the the Insert algoritm but even then the other hooks will exist Database DML Event Triggers  Before insert statement, before insert row  After insert row, After insert statement

15 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Architectuur Service DAO Domain Object Domain Object Domain Object JNDI

16 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Example import java.sql.*; import javax.sql.*; public class EmpDao { public List getAllEmployees() { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; List emps = new ArrayList(); try { con = getConnection(); pstmt = con.prepareStatement ("select * from emp"); rs = pstmt.executeQuery(); while (rs.next()) { Employee e = new Employee(); e.setId (rs.getLong(1)); e.setName (rs.getString(2)); //... emps.add(e); } } catch (SQLException e) { // handle exception } finally { try { rs.close(); pstmt.close(); con.close(); } catch (SQLException e1) { // no action needed } return emps; } private Connection getConnection() throws SQLException { try { Context ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup ("java:comp/env/jdbc/myDatabase"); return ds.getConnection(); } catch (NamingException e) { // handle exception return null; } private Connection getConnection() throws SQLException { try { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); return DriverManager.getConnection "tiger"); } catch (SQLException sqle) { // handle exception return null; }

17 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Template Pattern Operation largely follows a standard algorithm At certain steps, specialization or customization is required Several implementations  Abstract ‘hook’ methods that sub-class may override  Parametrize behaviour and have invoker provide the details Such as the SQL Query Spring JDBC Templates  Implement all JDBC wiring  Parametrize the query and the result-handling

18 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Example of Spring JDBC Template public interface empDao { public List getAllEmployees (); } public class EmployeeJdbcDao extends JdbcDaoSupport implements EmpDao { public List getAllEmployees() { JdbcTemplate jt = getJdbcTemplate(); return jt.queryForList (“select * from emp”); } <bean id="dataSourceDBDirect" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">

19 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method Conclusions Template Method Pattern  Instead of overriding and reimplementing the algorithm in a subclass  The algorithms has predefined call outs To abstract method that subclasses MUST implement To concrete, overridable methods to change parts of the algorithm – but not the overall structure! To hooks (optionally implemented by subclasses to complement the steps of the algoritm Easy to apply Recognized from duplication of algorithm across classes  You frequently override a method and duplicate most of its implementation Read – and enjoy – Head First Design Patterns