Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and."— Presentation transcript:

1 1 Design patterns Lecture 4

2 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and applying an appropriate design pattern to solve them

3 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.

4 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

5 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.

6 6 Model-View-Controller

7 7 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.

8 8 Assignment 2b

9 9 Multiple views

10 10 Other design patterns Object creation Singletons Enforces one instance but many references to the instance. Object factory Decides dynamically which subclass to instantiate

11 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.

12 12 And numerous other patterns We may eventually come back to this topic if we have time left in the end of June.

13 13 Databases Lecture 4

14 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.

15 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.

16 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.

17 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.orghttp://www.postgresql.org Look at \h and \? for command syntax At the command line: psql pg_dump man psql

18 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;

19 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;

20 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];

21 21 Select SELECT LastName,FirstName FROM Persons

22 22 Select SELECT * FROM Persons WHERE City='Sandnes'

23 23 Common “\” Commands \? Shows all “\” commands \h shows help menu \h COMMAND_NAME shows help for a specific command \q quits psql program

24 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.

25 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);

26 26 Statements java.sql.Statement is the most common way to query the db. The PreparedStatement class may be used for precompiled SQL queries.

27 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

28 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.

29 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


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

Similar presentations


Ads by Google