Chapter 6-Distributed Programming
Chapter 6-Distributed Programming
Distributed Programming
Tier Architecture
Overview
In the beginning, life was simple. Computers were separate, individual
devices.
Programs had access to all the computer's input and output through
computer- connected devices. With the invention of networks, life became
more complicated.
Now we have to write programs that depend on other programs
running on faraway computers. Often, we have to write all those
faraway programs as well! This is what's called distributed
programming.
Distributed Application
A distributed application is a system comprised of programs running on
multiple host computers. The architecture of this distributed application is
a sketch of the different programs, describing which programs are running
on which hosts, what their responsibilities are, and what protocols
determine the ways in which different parts of the system talk to one
another.
Tier Architecture
The concept of tiers provides a convenient way to group different
classes of architecture.
One Tier
it has a one-tier architecture. If your application is running on two
computers --
for instance, a typical Web CGI application that runs on a Web browser
(client) and a Web server
. In a two-tier system
, you have a client program and a serverprogram. The main difference
between the two is that the server responds to requests from many
different clients, while the clients usually initiate the requests for
information from a single server
Three tier
A three-tier application adds a third program to the mix, usually a
database, in which the server stores its data. The three-tier application
is an incremental improvement to the two-tier architecture.
N-tier
An n-tier architecture, on the other hand, allows an unlimited number of
programs
to run simultaneously, send information to one another, use different
protocols to communicate, and interact concurrently. This allows for a
much more powerful application, providing many different services to
many different clients.
JDBC Overview
1. Get a Connection to the database.
2. Create a Statement using the Connection.
3. Execute the Statement with SQL string.
4. Use the results.
JDBC Overview
selects a specific Connection type
and instantiates it
creates Statements
for database actions
JDBC Code
/** BAD CODE. We'll fix this later. */
static final String URL = "jdbc:mysql://dbserver/world";
static final String USER = "student";
static final String PASSWORD = "secret";
// 1. Get a Connection to the database.
Connection connection =
DriverManager.getConnection( URL, USER, PASSWORD );
// 2. Create a Statement
Statement statement = connection.createStatement();
DriverManager <<interface>>
creates Connection
getConnection( url, user,
passwd) : Connection createStatement(): Statement
close( )
isClosed( ): boolean
getCatalog( ): String
HSQLConnection MySqlConnection
Patterns Question
What design pattern is used by DriverManager?
DriverManager <<interface>>
creates Connection
getConnection( url, user,
passwd) : Connection createStatement(): Statement
close( )
isClosed( ): boolean
getCatalog( ): String
HSQLConnection MySqlConnection
Where is the Database Driver?
Driver is in a JAR file.
JAR file must be on the
CLASSPATH. Use one of these:
1. add as an external jar file to your IDE project
2. add the JAR to your CLASSPATH
CLASSPATH = /my/path/mysql-connector.jar;.
use http://se.cpe.ku.ac.th/download/mysql
alternate: http://www.mysql.com
Install it in your software "library" directory,
e.g. C:/lib/mysql
</tr>
</c:forEach>
3. Performing Create (Insert Records)
<c:set var="id_n" value=“67"/>
<c:set var="pname_n" value=“Printer"/>
<c:set var="quantity_n" value=“3"/>
<sql:update dataSource="${snapshot}"
var="count"> UPDATE Product SET pname =
'colgate' WHERE id = ?
<sql:param value="${id_n}" />
</sql:update>
4. Performing Delete (Delete Records)
<sql:update dataSource="${snapshot}"
var="count"> DELETE FROM Product WHERE
id = ?
<sql:param value="${empId}" />
</sql:update>
Three-tier Architecture
Three-tier Architecture
Three-tier Architecture
The 3-Tier Architecture for Web Apps
Data Layer - A database, comprising both data sets and the database
management system or RDBMS software that manages and provides
access to the data (back- end)
Origin of Design Patterns
9-Nov-16
The MVC pattern
MVC stands for Model-View-Controller
The Model is the actual internal representation
The View (or a View) is a way of looking at or displaying the
model
The Controller provides for user input and modification
These three components are usually implemented as separate
classes
MVC
One of the most common Design Patterns is Model-View-Controller
(MVC)
The model does all the computational work
It is input/output free
All communication with the model is via methods
The controller tells the model what to do
User input goes to the controller
The view shows results; it is a “window” into the model
The view can get results from the controller, or
The view can get results directly from the model
36
Advantages of MVC
One advantage is separation of concerns
Computation is not intermixed with I/O
Consequently, code is cleaner and easier to understand
Another advantage is flexibility
The GUI (if one is used) can be completely revamped without touching the
model in any way
Another big advantage is reusability
The same model used for a servlet can equally well be used for an
application or an applet (or by another process)
37
MVC Design Pattern Example in Java console
Model - Model represents an object or JAVA POJO carrying data. It can
also have logic to update controller if its data changes.
View - View represents the visualization of the data that model contains.
Controller - Controller acts on both model and view.
Implementation