Vous êtes sur la page 1sur 24

Lab Exercises Online Bookstore

Application

Carol McDonald

1
Sample Application

Review process of developing application


from specification to design to
implementation.
1. Scenarios or Use Cases
2. Partitioning of functionality into
modules
3. Assignment of functionality to tiers
4. Model View Controller Design Pattern
5. Business Logic Classes

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

2
Use Case Scenarios

C reate/Update Custom e r Account

Browse Catalog

Customer

Login to Customer Account

add items to shopping cart <<extend>>

<<includes>>

check out order items Add Order to Warehouse System


(external)

<<includes>>

Send Receipt

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

3
Divide the Application Into
Modules Based on Functionality
Account Catalog Shopping Order Inventory Mailer
Cart

: Customer
login

getProducts

addItem

checkOut createOrder

updateInventory

sendOrderConfirmation

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

4
Divide the Application Into
Modules Based on Functionality

Account Customer Account Module: tracks


Customer Account information

Catalog Product Catalog Module: provides search


for products, and product details

Cart
Shopping Cart Module: allows user to save
selected items for the session.

Order
Order Processing Module: performs order
Processing when user buys the items in the
cart.
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

5
Divide the Application Into
Modules Based on Functionality

Inventory
Inventory Module: maintains information
on the number of each type of product in
stock.

Mail Messaging Module: sends confirmation


receipt messages.

Control
Control Module: control interactions
between user (browse, add items,
check out) and business objects.
(Servlets)

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

6
Interrelationship of the modules

User
interface browse Catalog

Control

new/existing account

add/remove Account

items

Cart
checkout Inventory

Mail Order

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

7
Divide Application into Objects

• Divide Application Objects into 3


categories:
– View, Boundary, or Presentation classes:
present the interface to the user
– Control classes: accept user requests and
control the business objects to fulfill the
request.
– Model classes: represent the application
business data and rules.
• Entity, Domain classes: usually abstractions
of real world entities. Long lived, nouns.
• Session classes: business process, rules,
workflow.
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

8
Model View Controller Design Pattern

Entity EJB and Session EJB

Model
State change
State
query
Notify Change
Event

View selection
View Controller

User event
JSP Servlets, or
JavaBeans Controller classes
components Session EJB
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

The model is the heart of an application. It represents the application data and
the business rules that govern access and modification of this data. The model
serves as a software approximation to a real world process. Thus we can use
simple real world modeling techniques when defining the model.
The model abstraction provides:
• The ability for the view to query the model about its state.
• Notification to the view when the model changes.
• The ability for the controller to access application functionality encapsulated
by the model.
A view renders the contents of a model. It accesses data from the model and
specifies how that data should be presented. When the model changes, it
notifies
the view. It is the view’s responsibility to maintain consistency in its
presentation
after a model change. When the view is rendered in a Web browser, it is
generated
as part of a response to a user request. In this scenario the treatment of model
changes applies to parts of the model that might be cached in the Web server
tier.

9
MVC

Presentation Application Logic


Logic Layout Business Data Access

data model
Entity Bean

Manages data

control
Process
/service
Servlet
Session Bean

Receives request Validates request


Validates input Executes process
Calls session bean HTML Enforces transactions model
Call JSP layout
Entity Bean
JSP
Formats HTML Manages data
Responds to client
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

10
Model: Business Objects:
Business Data

• Often called domain classes since they


deal with abstractions of real-world
entities. Often Nouns.
• Entity Beans: Object view of business
entity stored in persistent storage.
• Provides object wrapper for accessing
and manipulating the data.
• Persistent , long lived.

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

There is state associated both with the application user interface and with the
application or business logic. In general, an application must maintain the
follow-ing
state:
• The user identity - Typically, the user account module maintains the user
iden-tify,
which includes the user’s login ID and certain security credentials.
• The search cursor and catalog position - The catalog module maintains the
cur-sor’s
position within the current search and within the catalog hierarchy.
• The items in the shopping cart - The shopping cart module maintains the list
of
items placed in the shopping cart.
• Order information - When the user commits the order, the shopping cart
passes
this information the order information—billing address, shipping address, and
payment method—to the order management module, which eventually stores
it to a database.

11
Online BookStore Database Schema

1 0-n
Orders
Customer
orderid
email
email
password date
name ShipAddress
address Total price
1

0..n

OrderLineItem
Product
orderId
ISBN
linenumber
title ISBN
author quantity
description Unit price

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

The application maintains accounts and tracks orders for products. Thus, there
are three areas for which data must be maintained: product, account, and order
information. The product, category, and item tables represent the business’s
product catalog. Each item has an associated entry in the inventory table that
represents the inventory for that product. The account table maintains account
information, one record per customer, with information such as customer
name,
password, and customer address. Finally, there is an orders table with one
record
per order, which keeps information about the order, including ship-to address,
bill-to address, total price of the order, and payment (credit card name,
expiration
date, type) information. The orders table is linked to lineitem and orderstatus
tables. Each item in an order is stored in a separate lineitem record, which con-
tains
the quantity ordered and price and a separate orderstatus record, which
contains a reference to the item and the status of the order.

12
UML for Entity Objects

Customer Order
1 0..n
orderId
email email

0..n

Product 1 0..n OrderLineItem


orderId
ISBN
lineNumber
ISBN

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

13
Customer Entity EJB
C u s t o m e rE J B
e m a il
a d d res s
Customer nam e
p a s s w o rd
email c r e d i t C a rd N u m b e r

password s e t C u s to m e r D e ta i l s ( )
g e t C u s to m e r D e ta i l s ( )
name lo g in ( )
e j b C r e a te ( )
address e j b P o s tC r e a t e ( )
e jb F in d B yN a m e ()
e jb R e m o ve ( )
s e tE n tityC o n te xt()
u n s e tE n tityC o n te x t ( )
e jb A c tiva te ( )
e jb P a s s iva te ( )
e jb L o a d ()
e j b S to r e ( )

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

14
Order Entity EJB

orders OrderEJB
orderid orde rId
orderLineItem s
emailId
email
date
s ta tu s
1 ShipAddress to talPrice
Total price orde rDate

getOrderDeta i l s ()
ejbCreate()
ejb R e m ove()
lineitem s e tEntityContext()
n orderId ejb L o a d ()
linenumber ejb S tore()
itemId ejbFind ByPrim a ryK e y()
ejb Activate()
quantity
ejb P a s s ivate()
Unit price
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

15
Value Objects
CustomerEJB
email
address
name CustomerDetails
password email
creditCardNumber address
name
setCustomerDetails() password
getCustomerDetails() cred itCardNum ber
login()
ejbCreate() getEmail()
ejbPostCreate() g e tA d d r e s s ( )
ejbFindByName() getName()
ejbRemove() getPassword()
setEntityContext() g e tC reditCardNumber()
unsetEntityContext()
ejbActivate()
ejbPassivate()
ejbLoad()
ejbStore()

Value objects are used to encapsulate a serializable read only version of an entire
remote object. This allows to retrieve the value of all the details of a remote object
This App has details objects named XXXDetails (where XXX takes the values Book,
Customer, and Order) for each enterprise bean.
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Value Objects
A value object is a business object that can be passed by value. The pass-by-
value semantics can be achieved by implementing the object as a serializable
Java object. A business concept should be implemented as a value object when
it is fine-grained, dependent, and immutable. There are two types of value
objects: dependent objects and details objects.
An object is a dependent object of another object if its life cycle is completely
managed by that object and if it can only be accessed indirectly through that
object. Dependent objects have no set methods. Therefore, dependent objects
can only be modified by creating a new containing object. Examples of
dependent objects in the sample application are Address and CreditCard.
A value object can also be used to encapsulate a serializable version of an
entire remote object. Such objects are used to allow a client to retrieve the
value of all the details of a remote object in one call. The sample application
contains a details object named XXXModel (where XXX takes the values
Catalog, Inventory, Account, Cart, and Order) for each enterprise bean.

16
Model: Business Objects:
Business Rules, Process, Workflow

• Stateless Session Beans:


– Reusable service objects
– May provide high performance
– Need to operate on multiple rows at
a time
– Provides procedural view of data

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

17
Stateless Services:
Catalog Session EJB
CatalogEJB
product
ISBN
getBooks()
title
getBookDetails()
author findBooksBySubject()
description ejbCreate()
s e tSessionContext()
e j b R e m o ve()
ejbActivate()
ejbPassivate()

A Catalog object represents different products and provides


browsing and searching services to its clients. Both of the primary functions
of the catalog, browsing and searching, are generic services which are not tied
to any particular client. Also, the catalog object reads multiple rows in
the database at the same time and provides a shared view of the data.
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

A Catalog object represents different categories and products and provides


browsing and searching services to its clients. Both of the primary functions of
the catalog, browsing and searching, are generic services which are not tied to
any particular client. Also, the catalog object operates on multiple rows in the
database at the same time and provides a shared view of the data.

The sample application uses stateless session beans for objects containing
more than one database row. In particular, because stateless session beans
provide high performance, stateless session beans are a good choice to provide
a high-performance cache of data for operations that access multiple rows. In
the sample application, the Catalog stateless session bean functions as a cache
that is built up over time.

18
Model: Business Objects:
Business Rules, Process, Workflow

• Stateful Session Beans


– Maintain client specific state
– Non-persistent
– Workflow, process management

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

19
Stateful Session EJB

ShoppingCartEJB
cart
numberOfItems

addItem ()
deleteItem ()
clear()
getItem s ()
getItem ()
getNumberOfItems()
getTotal()
ejbCreate()
s e tSessionContext()
ejbRemove()
ejbActivate()
ejbPassivate()

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

20
Controller Objects
Responsible for coordinating the Model
and View.
1. Receives user requests, and translate
them into application business events.
2. Invokes methods on the model to
cause desired state changes.
3. Selects the screen shown in response
to the request.
request event
controller model
Customer

display

view
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

he Controller
The sample application must reflect the state of a user’s interaction with the
applica-tion
and the current values of persistent data in the user interface. According to the
MVC pattern, this functionality is assumed by the controller. In the sample
applica-tion,
the controller is split between the Web tier and the EJB tier. In this section we
will discuss the implementation of the controller for the shopping interaction
in the
sample application.
The controller is responsible for coordinating the model and view. The view
depends on the controller for view selection. The model depends on the
controller
for making state changes to the model. The controller must accept user
gestures
from the view, translate them into a business events based on the behavior of
the
application, and process these events. The processing of an event involves
invok-ing
methods of the model to cause the desired state changes. Finally, the controller
selects the screen shown in response to the request that was processed.
21
Since the controller must coordinate both the view and the data, it straddles
Example Solution: EJBs

Clients Web Server Application Server DBMS

EJB Container

Catalog Customer Account

Servlets

Servlets
Order Entry
Shopping
Cart

EJB Server

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

When using a distributed component model platform for providing a software


solution, the goal of the application designer is to identify various components
comprising a system. Our recommendation is to partition the system based on
functionality, isolate the functional units of work, and identify the
components that make up the function. For example, in this application we
divided the application into following functional units: Customer Account
Management, Order Management, Product Inventory Management,
Catalog Management. Each functional unit can then be broken down in to
various sub-components.
For example, Customer Account management has a Web component to
display and gather account information from the client, a business logic
component to process the account information in accordance with some
business rules, and a database component to store the persistent account
information. The different components of an application need to follow certain
design rules when communicating with each other.

22
Example Scenario continued

Servlets
add to cart
register show
or login products
Shopping
cart
purchase
catalog

customer Order

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

23
Example Scenario continued

Servlet Enterprise JavaBean J


D
Login/Register Customer B
C
H
T
T Browse Catalog
P
(S)
Shopping
Purchase Order
Cart

Enterprise Application Server

Server Configuration

JNDI

Directory Server
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

The Client interface is provided by servlets which generate HTML pages


displayed in the browser. The servlet calls methods in the appropriate
enterprise beans. The session bean ShoppingCart also acts as the client for the
entity bean Order. The entity beans are stored in database tables - Order,
Customer, (Product) .

24

Vous aimerez peut-être aussi