Design Patterns For Enterprise Application

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Design Patterns for Enterprise

Application

1
Model View Controller (MVC)

2
MVC

3
History
A framework pattern for reusable applications.
Depends on the Observer pattern.

First developed by Xerox PARC for Smalltalk-80.

Used by the Cocoa APIs for Apple’s OS X.


Recommended structural framework pattern in J2EE.
MVC

The intent of MVC is to keep neatly separate objects


into one of tree categories
– Model
• The data, the business logic, rules, strategies, and so on
– View
• Displays the model and usually has components that allows user to
edit change the model
– Controller
• Allows data to flow between the view and the model
• The controller mediates between the view and model

5
Model

The Model's responsibilities


– Provide access to the state of the system
– Provide access to the system's functionality
– Can notify the view(s) that its state has changed

6
View

The view's responsibilities


– Display the state of the model to the user
At some point, the model (a.k.a. the observable)
must registers the views (a.k.a. observers) so
the model can notify the observers that its state
has changed

7
Controller

The controller's responsibilities


– Accept user input
• Button clicks, key presses, mouse movements, slider bar
changes
– Send messages to the model, which may in turn
notify it observers
– Send appropriate messages to the view
In Java, listeners are controllers
8
MVC - Web

9
Web Application

The Web-tier controller receives each incoming HTTP request and


invokes the requested business logic operation in the application
model
Based on the results of the operation and state of the model, the
controller then selects the next view to display
Finally, the controller generates the selected view and transmits it to
the client for presentation
Model 2
Model 2 Architecture to serve dynamic content
– Model: Enterprise Beans with data in the DBMS
• JavaBean: a class that encapsulates objects and can be displayed graphically
– Controller: Servlets create beans, decide which JSP to return, do the bulk
of the processing
– View: The JSPs generated in the presentation layer (the browser)

11
MVC Benefits

Clarity of design
– easier to implement and maintain
Modularity
– changes to one don't affect the others
– can develop in parallel once you have the interfaces
Multiple views
– games, spreadsheets, powerpoint, Eclipse, UML
reverse engineering, ….

12
Front Controller
The Front Controller

View 1

View 2
Client Front
Controller
View 3

View 4
Helper Helper Helper
class class class
Problem

There are common processes required by all


requests made to an application.
– Security
– Content retrieval
There is no centralized handler for these
requests.
This makes each component perform these
tasks in its own way which can lead to redundant
code.
One Solution

Use the Front Controller pattern to provide a


single point of contact for all client requests.
The Front Controller integrates with all other
application services such as security,
persistence, etc.
This allows common code to be moved into the
Front Controller.
16-17
Participants
FrontController
– translates user requests and dispatches them as application events.
– selects Views for the user based on application state.
– applies templates and enforces security across all Views.
Views
– each View sends requests to the FrontController, whenever it would have
otherwise communicated with another View.
The process
– A client sends a request (through a view) to the FrontController.
– The FrontController dispatches the request as an application event, and selects
an appropriate view to be displayed on client.
– The FrontController may also apply a transformation or enforce a policy on the
selected view.
Sequence
Benefits of Front Controller
Navigation is easier to understand and configure.
– Because view selection is centralized in the front controller, you only have to look
at the controller to understand the site navigation. Also, you only need to modify
the controller in order to change the navigation.
Views are handled consistently.
– Since the front controller handles view selection, it can consistently apply
templating and security policies across all views. Also, it is easier to configure the
behaviors of these functions, since only the controller needs to be modified.
Views can be easily changed and reused.
– Since views communicate only with the front controller, there are no
dependencies between views. This allows views, and even the front controller, to
be varied and reused independently.
Data Access Object
DAO

16-22
Problem

Applications require persistence.


Access mechanisms and APIs vary across
different persistence technology.
Components should be independent of the
actual persistence mechanism.
One Solution

Use the DAO pattern to isolate persistence


semantics from business semantics.
Business components use the DAO to access
the persistence layer.
Participants

Business Object: Requires access to the Data


Store.
Data Access Object: Translates requests from
the Business Object into requests against the
Data Store.
Data Store: Performs actual storage of the data.
Sequence
Benefits

Data access is transparent since the client code


doesn't need to know the implementation details.
Centralizes all data access into its own layer
thus providing better application partitioning
reduced client complexity.

You might also like