Design patterns
Download
Report
Transcript Design patterns
Design patterns
Lecture 4
1
Three Important skills
Understanding OO methodology
Mastering Java language constructs
Recognizing common problems and
applying an appropriate design pattern to
solve them
2
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.
3
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
4
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.
5
Model-View-Controller
View
Controller
Input and response
Knows interface of Model
Knows a little about View
Model
Business logic
No awareness of View
Minimal awareness of Controller
6
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.
View
Controller
Input and response
Knows interface of Model
Knows a little about View
Model
Business logic
No awareness of View
Minimal awareness of Controller
7
Assignment 2b
View
JSP based view
layer for web app.
Controller
Input and response
Servlets that is controller
layer for the web app.
Model
Business logic
Helper classes,
e.g. Database abstractions
8
Multiple views
Views
Controller
Input and response
Knows interface of Model
Knows a little about Views
Model
Business logic
No awareness of View
Minimal awareness of Controller
9
Other design patterns
Object creation
• Singletons
• Enforces one instance but many references to the
instance.
• Object factory
• Decides dynamically which subclass to instantiate
10
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.
11
And numerous other patterns
We may eventually come back to this
topic if we have time left in the end of
June.
12
Databases
Lecture 4
13
“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.
14
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.
15
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.
16
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
17
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;
18
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;
19
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];
20
Select
SELECT LastName,FirstName FROM Persons
21
Select
SELECT * FROM Persons WHERE City='Sandnes'
22
Common “\” Commands
\? Shows all “\” commands
\h shows help menu
\h COMMAND_NAME shows help for a
specific command
\q quits psql program
23
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.
24
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);
25
Statements
java.sql.Statement is the most
common way to query the db.
The PreparedStatement class may
be used for precompiled SQL queries.
26
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
27
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.
28
Basic use of java.sql
1.
2.
3.
4.
5.
6.
Load driver class
Get connection
Get statement
Ask statement to execute sql string
Process results if needed
Close Statement and Connection
29