© People Strategists www.peoplestrategists.com Slide 1 of 42
Agile Development
and Design Patterns - II
© People Strategists www.peoplestrategists.com Slide 2 of 42
Objectives
In this session, you will learn to:
Identify the structural design pattern
Identify the behavioral design patterns
Identify the J2EE design patterns
Identify the Web application architecture
Explore the JEE architecture
© People Strategists www.peoplestrategists.com Slide 3 of 42
Identify the Structural Design Patterns
Structural design patterns:
Serve as a blueprint for how different classes and objects are combined to form larger structures.
Provide a manner to define relationships between classes or objects.
Describes how data moves through the pattern.
Some of the common structural design patterns are:
Adaptor
Bridge
Decorator
Facade
© People Strategists www.peoplestrategists.com Slide 4 of 42
Adapter Design Pattern:
Defines a manner for creating relationships between classes.
Is a design pattern that is used to allow two incompatible types to communicate.
Converts the interface of a class into another interface clients expect.
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 5 of 42
The following figure depicts the implementation of the adapter design pattern.
<<interface>>ITarget
+MethodA(): void
Adapter
+MethodA(): void
-adaptee: Adaptee
Adaptee
+MethodB(): void
Client
adaptee.MethodB
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 6 of 42
The classes/objects participating in the preceding adapter pattern are:
ITarget: Defines the domain-specific interface that Client uses.
Adapter: Adapts the interface Adaptee to the Target interface.
Adaptee: Defines an existing interface that needs adapting.
Client: Collaborates with objects conforming to the Target interface.
Implementation for the adapter design is given in the embedded code.
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 7 of 42
Bridge Design Pattern:
Defines a manner for creating relationships between classes or entities.
Is used to separate the abstract elements of a class from the implementation details.
Is used to share an implementation among multiple objects.
Hides implementation details from clients.
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 8 of 42
The following figure depicts the implementation of the bridge design pattern.
Implementation for the bridge design is given in the embedded code.
<<interface>>DrawAPI
+drawCircle(): void
RedCircle
+drawCircle(): void
GreenCircle
Shape
Uses+drawAPI(): DrawAPI
+Shape(): void+Draw(): String
BridgePatternDemo
+main(): void
Circle
+drawCircle(): void
-x, y, radius :int
+Shape(): void+Draw(): String
Uses
implementextends
Uses Uses
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 9 of 42
Decorator Design Pattern:
Defines a manner for creating relationships between classes or entities.
Creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
Is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class.
Is used to extend the functionality of individual objects, not classes. This means that the modifications are made at run-time rather than at design time.
Provides a flexible alternative to using inheritance to modify behavior.
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 10 of 42
The following figure depicts the implementation of the decorator design pattern.
Implementation for the decorator design pattern is given in the embedded code.
DecoratorPatternDemo
+main(): void
Circle
+draw(): void
Rectangle
Shape
Uses+Draw(): void
+draw(): void
implement
RedShapeDecorator
+shape: Shape
+RedShapeDecorator()+draw(): void-setRedBorder(): void
ShapeDecorator
+shape: Shape
+ShapeDecorator()+draw(): void
<<Interface>>
Ask
implement
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 11 of 42
Facade Design Pattern:
Defines a manner for creating relationships between classes or entities.
Is used to define a simplified interface to a more complex subsystem.
Is ideal when working with a large number of interdependent classes, or with classes that require the use of multiple methods.
Is useful when wrapping subsystems that are poorly designed but cannot be refactored because the source code is unavailable or the existing interface is widely used.
Involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.
Identify the Structural Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 12 of 42
The following figure depicts the implementation of the facade design pattern.
Implementation for the facade design pattern is given in the embedded code.
FacadePatternDemo
+main(): void
Circle
+draw(): void
Rectangle
Shape
<<interface>>
+Draw(): void
+draw(): void
implement
RedShapeDecorator
+circle: Shape+rectangle: Shape+square: shape
+ShapeMaker()+drawCircle(): void+drawRectangle(): void+drawSquare(): void
Square
+draw(): void
implement
Creates
Identify the Structural Design Patterns (Contd.)
Ask
© People Strategists www.peoplestrategists.com Slide 13 of 42
Identify the Behavioral Design Patterns
Behavioral Design Pattern:
Explains how objects interact.
It describes how different objects and classes send messages to each other to make things happen and how the steps of a task are divided among different objects.
In this implementation and the client should be loosely coupled in order to avoid hard-coding and dependencies.
This pattern characterizes object communication and simplifies the dynamic control flow of object behavior.
If Creational pattern is about instantiation, and structural pattern is the blueprint, then behavioral is the pattern of the relationship among objects.
Some of the common patterns come under it are:
Strategy
Command
Iterator
© People Strategists www.peoplestrategists.com Slide 14 of 42
Identify the Behavioral Design Patterns (Contd.)
Strategy Design Pattern:
Defines a manner for controlling communication between classes or entities.
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
This allows the behavior of a program to change dynamically according to configuration details or user preferences.
It also increases flexibility by allowing new algorithms to be easily incorporated in the future.
© People Strategists www.peoplestrategists.com Slide 15 of 42
The following figure depicts the implementation of the strategy design pattern.
Implementation for the strategy design pattern is given in the embedded code.
StrategyPatternDemo
+main(): void
OperationAdd
+doOperation(): int
OperationSubstract
Strategy
<<interface>>
+DoOpeation(): int
implement
Context
+strategy: Strategy
OperationMultiply
implement
Uses
Identify the Behavioral Design Patterns (Contd.)
+executeStrategy: int
+doOperation(): int+doOperation(): int
implement
Ask
© People Strategists www.peoplestrategists.com Slide 16 of 42
Identify the Behavioral Design Patterns (Contd.)
Command Design Pattern:
Defines a manner for controlling communication between classes or entities.
Is used to express a request, including the call to be made and all of its required parameters, in a command object.
Is useful when supporting activities that require the execution of a series of commands.
The command objects can be held in a queue and processed sequentially.
Use to decouple the object with its trigger.
Easily to be extensible by not touching the old structure.
© People Strategists www.peoplestrategists.com Slide 17 of 42
The following figure depicts the implementation of the command design pattern.
Implementation for the command design pattern is given in theembedded code.
Identify the Behavioral Design Patterns (Contd.)
CommandPatternDemo
+main(): void
Order
+execute(): void
<<interface>>
Broker
-orders: List
implement
Uses
+takeOrder(): void+placeOrders(): void
BuyStock
+BuyStock()+execute()
SellStock
+BuyStock()+execute()
Uses
Stock
-name: String-quantity: int
+buy(): void+sell(): void
Uses
-stock: Stock -stock: Stock
© People Strategists www.peoplestrategists.com Slide 18 of 42
Iterator Pattern Design:
Is used to provide a standard interface for traversing a collection of items in an aggregate object without understanding the underlying structure of that object.
Is commonly used design pattern in Java and .Net programming environment.
Iterator should be implemented as an interface.
Identify the Behavioral Design Patterns (Contd.)
© People Strategists www.peoplestrategists.com Slide 19 of 42
The following figure depicts the implementation of the iterator design pattern.
Implementation for the iterator design pattern is given in theembedded code.
Identify the Behavioral Design Patterns (Contd.)
Iterator
+hasNext(): boolean+next(): Object
<<interface>>
NameIteratorUsesNameRepository
+getIterator(): iterator
implement
Container
+getIterator(): iterator
Uses
-name: String []
+hasNext(): boolean+next(): Object
main(): void
IteratorPatternDemoUses
<<interface>>
implement
© People Strategists www.peoplestrategists.com Slide 20 of 42
Identifying the Web application Architecture
Web applications:
are programs that are executed on a server and accessed from a Web browser.
enable organizations to share and access information on the Internet.
are usually divided into the following logical chunks known as tiers:
Presentation:
It is the top-most tier in the Web application architecture.
It provides a UI that allows the users to interact and communicate with the
applications.
Business logic:
It is the middle tier in the Web application architecture.
It contains the logic related to the business decisions of the organization.
Data access:
It is the last tier in the Web application architecture.
It contains the logic that enables you to access the data stored in various data
sources, such as
Relational Database Management Systems (RDBMS), Extensible Markup
Language (XML), and text files.
© People Strategists www.peoplestrategists.com Slide 21 of 42
Identifying the Web application Architecture
The following figure depicts the example of Web applications.
© People Strategists www.peoplestrategists.com Slide 22 of 42
Identifying the Web application Architecture (Contd.)
Based on the complexity of the Web application, it might follow any of the following Web application architecture:
Single-tier
Two-tier
Three/n-tier
© People Strategists www.peoplestrategists.com Slide 23 of 42
Identifying the Web application Architecture (Contd.)
Single-Tier Architecture:
Is also known as the monolithic architecture.
In this architecture, the presentation logic, the business logic, and the data access logic are packaged on the same computer.
These layers are tightly coupled that means if you make changes to any one of them, you also need to modify others.
The following figure depicts the single-tier application architecture.
© People Strategists www.peoplestrategists.com Slide 24 of 42
Identifying the Web application Architecture (Contd.)
Two-tier Architecture:
In this architecture, the presentation logic resides on the client.
The business logic may reside either on the client or on the server.
The data access logic resides on the server.
Depending on the business requirements, it can be of the following types:
A fat client and thin server:
In this type, the presentation logic and the business logic reside on the client,
and the data access logic resides on the server.
This application architecture helps in reducing the load on the server.
The following figure depicts a fat client and thin server application
architecture.
© People Strategists www.peoplestrategists.com Slide 25 of 42
Identifying the Web application Architecture (Contd.)
A fat server and thin client:
In this type, the client contains only the presentation logic.
The business logic and the data access logic reside on the server.
This type of architecture is easily managed, less prone to security risks, and
offers low maintenance and licensing costs.
The following figure displays the layout of the layers in a fat server and thin
client application architecture.
© People Strategists www.peoplestrategists.com Slide 26 of 42
Identifying the Web application Architecture (Contd.)
Three-tier/N-tier Architecture:
In the three-tier architecture, the presentation logic, business logic, and data access logic are separated into different layers, as shown in the following figure.
© People Strategists www.peoplestrategists.com Slide 27 of 42
Java Enterprise Edition:
Is Oracle's enterprise Java computing platform.
Is an open and standard based platform for developing, deploying, and managing n-tier, Web-enabled, server-centric, and component-based enterprise applications.
Identifying Java Enterprise Edition (EE)
© People Strategists www.peoplestrategists.com Slide 28 of 42
Identify the J2EE Design Patterns
J2EE Design Patterns:
Identify potential problems encountered when using the various J2EE-specified technologies and help developers construct an application architecture's requirements.
Are specifically concerned with the presentation tier.
Are identified by Sun Java Center.
Some of the common J2EE patterns are :
Data Access Object (DAO) design pattern
Model-View-Design (MVC) design pattern
© People Strategists www.peoplestrategists.com Slide 29 of 42
Identify the J2EE Design Patterns (Contd.)
Data Access Object (DAO) Design Pattern:
Is used to separate low level data accessing API or operations from high level business services. Adapt a uniform interface to access multiple databases like relational, un-relational, object-oriented, etc.
Participants of the DAO pattern are.
Data Access Object Interface: This interface defines the standard operations to
be performed on a model object(s).
Data Access Object concrete class: This class implements above interface. This
class is responsible to get data from a data source which can be database / xml
or any other storage mechanism.
Model Object or Value Object: This object is simple POJO containing get/set
methods to store data retrieved using DAO class.
© People Strategists www.peoplestrategists.com Slide 30 of 42
The following figure depicts the implementation of the DAO design pattern.
Implementation for the DAO design pattern is given in theembedded code.
Identify the J2EE Design Patterns (Contd.)
StudentDao DAOPatternDemoUses
+getAllStudents(): List+updateStudent(): void+deleteStudent(): void+addStudent(): void
StudentDaoImpl
+studentDaoImpl()+getAllStudent(): List+updateStudent(): void+deleteStudent(): void+addStudent(): void
-student: List
+main(): void
+Student()+getName(): String+setName(): void+getRollNo(): int+setRollNo(): String
Student
Implement
<<interface>>
-name: String-Rollno: int
Uses
© People Strategists www.peoplestrategists.com Slide 31 of 42
Identify the J2EE Design Patterns (Contd.)
MVC Design Pattern:
Is a design pattern for the architecture of Web applications.
Is mainly is to achieve a clean separation between three components of most any Web application.
Three components of MVC pattern are:
Model:
Is Java beans with their properties and methods.
It contain the business logics and functions that manipulate the business
data.
View:
Is the Java Server Pages (JSP's) or HTML pages.
It only takes care of the presentation of the data and are known as
presentation layer.
Controller:
It accepts and intercepts user requests and controls the business objects
to fulfill these requests.
Is a JSP or servlet file.
© People Strategists www.peoplestrategists.com Slide 32 of 42
The following figure depicts the implementation of the MVC design pattern.
Implementation for the MVC design pattern is given in theembedded code.
Identify the J2EE Design Patterns (Contd.)
StudentController
-model: Student-view: StudentView
MVCPatternDemoUses
+StudentController()+setStudentName(): void+getStudentName(): String+setStudentRollNo(): void+getStudentRollNo(): String+updateView(): void
Student
+getName(): String+setName(): void+getRollNo(): String+setRollNo(): void
-rollno: String-name: String
+main(): void+retriveStudentFromDatabase(): Student
+printStudentDetails(): void
StudentView
Updates
Uses
© People Strategists www.peoplestrategists.com Slide 33 of 42
Java EE comprises of the following application architecture:
Web-centric architecture
EJB component-centric architecture
Business-to-business (B2B) application architecture
Web service application architecture
Exploring Java EE Application Architecture
© People Strategists www.peoplestrategists.com Slide 34 of 42
The following figure depicts the Web-centric configuration of theJava EE application server (Java EE server) middle tier.
In this architecture, the Web container of the Java EE technology server hosts all of the components that are required to generate the client view, process the business logic, and connect with theback-end data store.
Java EE Web-Centric Architecture
Java EE Web-Centric Architecture
© People Strategists www.peoplestrategists.com Slide 35 of 42
The following figure depicts the EJB component-based configuration of the Java EE application server (Java EE server).
This architecture uses both the Web container and EJB containers of the Java EE server.
In this model, the business logic and data access components are located in the EJB container.
Java EE EJB Component–Centric Architecture
Java EE EJB Component-Centric Architecture
© People Strategists www.peoplestrategists.com Slide 36 of 42
The following figure depicts the Java EE technology B2B application architecture.
It is an extension of the Java EE technology EJB component–based architecture and involves two EJB servers, one in each business location.
Each Java EE server hosts a web container and an EJB container.
This architecture allows for peer-to-peer communications between the corresponding containers in the two Java EE servers.
Java EE B2B Application Architecture
Java EE B2B Application Architecture
© People Strategists www.peoplestrategists.com Slide 37 of 42
The following figure depicts Java EE Web service application architecture.
It starts with the Java™ 2 Platform, Enterprise Edition (J2EE™) 1.4 platform.
Java EE component developer can expose the functionality of Java EE business components by using Web service technology.
Java EE Web Service Application Architecture
Java EE Web Service Architecture
© People Strategists www.peoplestrategists.com Slide 38 of 42
Identifying MVC Architecture
MVC is a software architectural pattern for implementing user interfaces.
It’s main aim is to separate the business logic and application data from the presentation data to the user.
The following figure displays the MVC architecture.
MVC Architecture
© People Strategists www.peoplestrategists.com Slide 39 of 42
Summary
Structural design patterns serve as a blueprint for how different classes and objects are combined to form larger structures.
Adapter design pattern defines a manner for creating relationships between classes.
Bridge design is used to separate the abstract elements of a class from the implementation details.
Decorator design pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
Facade design pattern is used to define a simplified interface to a more complex subsystem.
Behavioral design pattern Explains how objects interact.
Strategy design pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.
© People Strategists www.peoplestrategists.com Slide 40 of 42
Summary (Contd.)
Command design pattern is used to express a request, including the call to be made and all of its required parameters, in a command object.
Iterator pattern design is used to provide a standard interface for traversing a collection of items in an aggregate object without understanding the underlying structure of that object.
Web applications are programs that are executed on a server and accessed from a Web browser.
Presentation tier provides a UI that allows the users to interact and communicate with the applications.
Business logic tier contains the logic related to the business decisions of the organization.
Data access tier contains the logic that enables you to access the data stored in various data sources.
In single-tier architecture, the presentation logic, the business logic, and the data access logic are packaged on the same computer.
© People Strategists www.peoplestrategists.com Slide 41 of 42
Summary (Contd.)
In two-tier architecture, the presentation logic resides on the client, the business logic may reside either on the client or on the server, and the data access logic resides on the server.
J2EE design patterns identify potential problems encountered when using the various J2EE-specified technologies and help developers construct an application architecture's requirements.
DAO) design pattern is used to separate low level data accessing API or operations from high level business services.
MVC design pattern is a design pattern for the architecture of Web applications and mainly is to achieve a clean separation between three components of most any Web application.
In Java EE Web-centric architecture, the Web container of the Java EE technology server hosts all of the components that are required to generate the client view, process the business logic, and connect with theback-end data store.
© People Strategists www.peoplestrategists.com Slide 42 of 42
Summary (Contd.)
Java EE EJB component-centric architecture uses both the Web container and EJB containers of the Java EE server.
Java EE B2B application architecture is an extension of the Java EE technology EJB component–based architecture and involves two EJB servers, one in each business location.
Java EE Web Service Application Architecture starts with the Java™ 2 Platform, Enterprise Edition (J2EE™) 1.4 platform.