1 design patterns lecture 4. 2 three important skills understanding oo methodology mastering java...

29
1 Design patterns Lecture 4

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

1

Design patterns

Lecture 4

Page 2: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

2

Three Important skills

Understanding OO methodology Mastering Java language constructs Recognizing common problems and

applying an appropriate design pattern to solve them

Page 3: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

3

Design patterns…

A design pattern is a proposed solution to common design problems• May describe a high level architecture of

software…

• …or in detail with classes and methods.

• …or somewhere in between By becoming familiar with well-known

patterns, you may produce more flexible and robust code.

Page 4: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

4

Patterns vs frameworks

The book discriminates between “design pattern” and “framework”.• A design pattern is an abstraction you read

about

• A framework is collections of reusable classes that implement some design pattern.• Typically you extend the provided framework types,

making the subtypes context specific

Page 5: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

5

Example of pattern vs framework

The observer design pattern is in Java core API implemented by the framework of an Observer interface and the Observable class.

Page 6: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

6

View

ControllerInput and responseKnows interface of ModelKnows a little about View

ModelBusiness logicNo awareness of ViewMinimal awareness of Controller

Model-View-Controller

Page 7: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

7

View

ControllerInput and responseKnows interface of ModelKnows a little about View

ModelBusiness logicNo awareness of ViewMinimal awareness of Controller

MVC benefits Change user interface

(View) without impacting algorithms and business rules in the core (model) of the application.

Rework the model without changing anything in the user interface.

View and Model are decoupled.

Page 8: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

8

Assignment 2b

ControllerInput and responseServlets that is controller layer for the web app.

ModelBusiness logicHelper classes, e.g. Database abstractions

View

JSP based viewlayer for web app.

Page 9: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

9

Multiple views

Views

ControllerInput and responseKnows interface of ModelKnows a little about Views

ModelBusiness logicNo awareness of ViewMinimal awareness of Controller

Page 10: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

10

Other design patterns

Object creation• Singletons

• Enforces one instance but many references to the instance.

• Object factory• Decides dynamically which subclass to instantiate

Page 11: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

11

Other design patterns

Structural patterns• Adapter

• E.g. WindowAdapter class implements the exhaustive WindowListener interface with default (empty) handlers.

• Façade• When you have a class with an interface that can’t

be changed and doesn’t fit your context. Make a Façade class that perform required mappings between existing interface and desired interface.

Page 12: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

12

And numerous other patterns

We may eventually come back to this topic if we have time left in the end of June.

Page 13: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

13

Databases

Lecture 4

Page 14: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

14

“The book”

Please read chapter 9• Skim read and focus on interesting areas that may

relate to assignment 2.

• If you haven’t taken the database class (which we assume you haven’t), you should read this chapter in order to get a grip on different aspects of database design and implementation.

• You are not supposed to become an expert, but at least learn the basics, and get inspired by good practices that is mentioned in the book.

Page 15: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

15

Acronyms

RDBM – Relational database management (system)• E.g. DB2, Oracle, Sybase, MS SQL Server…

JDBC - Java database connectivity• Is part of the J2SE platform

• Provides a bridge between a Java program and an RDBM system usually running on native operating system.

Page 16: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

16

Acronyms …

SQL – Structured query language• is an ANSI (American National Standards

Institute) standard computer language for accessing and manipulating database systems.

• SQL statements are used to retrieve and update data in a database.

• May have slightly different flavors in different RDBM systems.

Page 17: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

17

PostGresQL Syntax You need to know a little PostGresQL

syntax to set up your DB and use JDBC SQL to Java type mappings: see p. 623,

figure 9-6 Documentation: http://www.postgresql.org Look at \h and \? for command syntax At the command line:

• psql• pg_dump <database_name>• man psql

Page 18: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

18

Common Table Commands CREATE TABLE table_name (

column_name1 column_type1, column_name2 column_type2, etc.); Can also specify a DEFAULT value, or other constraints like

NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, etc. \dt (shows tables) \d table_name (describes that table)

DROP TABLE table_name;

ALTER TABLE table_name RENAME TO new_table_name;

Page 19: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

19

Common Column Commands

ALTER TABLE table_name

• ADD column_name column_type [constraints];

• ALTER column_name SET DEFAULT value;

• ALTER column_name DROP DEFAULT;

• RENAME old_column_name TO new_column_name;

Page 20: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

20

Common Row Commands

INSERT INTO table_name values (42, 3, ‘foo’)

INSERT INTO table_name (col2_name, col3_name) values (3, ‘foo’);

UPDATE table_name SET col = expression [WHERE condition];

DELETE FROM table_name [WHERE condition];

Page 21: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

21

Select

SELECT LastName,FirstName FROM Persons

Page 22: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

22

Select

SELECT * FROM Persons WHERE City='Sandnes'

Page 23: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

23

Common “\” Commands

\? Shows all “\” commands

\h shows help menu

\h COMMAND_NAME shows help for a

specific command

\q quits psql program

Page 24: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

24

JDBC Driver types Type 1. JDBC-ODBC bridge (inefficient) Type 2. Mostly implemented in native code (C) with JNI

(Java Native Interfaces) to bridge between Java and the native code (C). Most efficient driver if all classes using db are on the db host

Type 3. Like type 2, but the user accesses the db over TCP/IP. Most efficient driver if not all classes using db are on the db host.

Type 4. Like type 3 but pure Java implementation, and therefore platform independent.

A type 4 driver for your first project is linked from the assignment page.

Page 25: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

25

Loading the driver and getting the db-connection Class.forName(“org.postgresql.Driver”)

• A call to forName("X") causes the class named X to be initialized. In this case the database driver will be loaded into memory.

• This will load the driver, and while loading, the driver will automatically register itself with JDBC.

Connection db = DriverManager.getConnection(url, username, password);

Page 26: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

26

Statements

java.sql.Statement is the most common way to query the db.

The PreparedStatement class may be used for precompiled SQL queries.

Page 27: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

27

Execution of SQL by Statement int Statement.executeUpdate(String sql);

• Returns number rows affected

• Good for INSERT, UPDATE, and DELETE

ResultSet Statement.executeQuery(String sql);

• Returns a ResultSet

• Good for SELECT

Page 28: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

28

ResultSet

Contains the results of a select statement. Remember

• Before reading any values, you must call next().

• You must close a ResultSet by calling close() once you have finished using it.

• Once you make another query with the Statement used to create a ResultSet, the currently open ResultSet instance is closed automatically.

Page 29: 1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and

29

Basic use of java.sql

1. Load driver class

2. Get connection

3. Get statement

4. Ask statement to execute sql string

5. Process results if needed

6. Close Statement and Connection