Академический Документы
Профессиональный Документы
Культура Документы
com
www.chetanasprojects.com
www.campusmiles.com
1
Course Topics
1. Design Patterns
– What is Pattern?
– Design Pattern & Framework
– Creational Patterns
– Structural Patterns
– Behavioral Patterns
1. UML
– What is UML?
– Use Case Diagram
– Class Diagram
– Relationships Between Classes
– Sequence Diagram
– Activity Diagram
– Collaboration Diagram
1. Case Study
2
What is Pattern?
• Lets interpret following number series
• 1247234934444541
• For the time being, Don’t consider 4th digit
• 124 234 344 454
• 12 4 7 23 4 9 34 4 4 45 4 1
• First two digits are of sequential nature with 1 plus. Third digit is
fixed. Fourth digit having random value.
• First two digits are of sequential nature with 1 plus. Third digit is
fixed. Fourth digit having random value.
Keyword is: Repetition
“Each pattern describes a problem which occurs over and over
again in our environment and then describe the core solution
to that problem, in such a way that you can use this solution a
million times over, without ever doing it the same way twice”
3
Contd…
• A set of rules describing how to accomplish certain tasks in the
realm of software development.
• Identify and specify abstractions that are above the level of single
classes or instances, or of components.
• They are not usually written by single individuals: They are usually
discovered in several different programmers' works.
• Gang of Four (GoF)
– Erich Gamma
– Richard Helm
– Ralph Johnson
– John Vlissides
4
Why Design Pattern?
• Writing objects is easy.
5
Why Design Pattern?
• Designing OO software is hard.
6
Design Pattern
• Solution to Problems that arise when
developing software within a particular context
Design Pattern = Problem (Recurring) / Solution pair in a Context
As specified by GOF:
Design Pattern
7
Design Pattern & Framework
• Design Pattern is a proven design solution for a recurring design problem
which arises in specific design context.
• A framework is an integrated set of classes that make up a reusable
design for a specific class of software.
– Ex : Compiler, graphical editor.
• Together, design patterns and frameworks help to improve software
quality and reduce development time e.g., reuse, extensibility, modularity,
performance
8
Design Pattern Space
Gamma have classified each of design patterns in one of three
categories, depending upon whether it has to do with
9
Design Pattern Space
• Creational Patterns
– Deal with initializing and configuring classes and
objects.
– Make a system independent of how its objects are
created.
• Structural Patterns
– Deal with decoupling interface and implementation of
classes and objects.
• Behavioral Patterns
– Deal with dynamic interactions among societies of
classes and objects.
10
Contd…
Creational Structural Behavioral
Abstract Factory Adapter Chain of Responsibility
Builder Bridge Command
Factory Composite Interpreter
Prototype Decorator Iterator
Singleton Façade Mediator
Flyweight Memento
Proxy Observer
State
Strategy
Template
Visitor
11
Another Classification
• Class Patterns
– Relationships between classes and their sub-
classes
– Factory, Adapter, Interpreter, Template
• Object Patterns
– Object relationships which are created at runtime
and are more dynamic
12
Creational Pattern
• Creational design patterns abstract the instantiation
process.
13
Factory Pattern
• A Factory pattern is one that produces one of several possible classes
depending on data provided to it.
• Provides a simple decision making class that returns one of several
possible subclasses of an (abstract) base class depending on the data
provided.
When to Use:
• A class can’t anticipate which class of objects it must create.
Localizing the knowledge which class gets created.
14
Factory Method
Define an interface for creating an object, but let subclasses decide which class
to instantiate. Factory method lets a class defer instantiation to subclasses.
15
Structural Pattern
• Concerned with how classes and objects are
composed to form larger structures
• Structural class patterns use inheritance to compose
interfaces or implementations
• Structural object patterns describe ways to compose
objects to realize new functionality.
16
Composite
• Compose objects into tree structures to represent
part-whole hierarchies. Composite lets clients treat
individual objects and compositions of objects
uniformly.
• It defines a class hierarchy of simple objects and
composite objects to be represented in one object.
• Employee (Manager, Programmer)
• Node & Leaf.
17
Composite Class Diagram
19
Decorator
•Add additional functionality to InputStream.
Applicability
20
Behavioral Patterns
•Concerned with algorithms and assignment of
responsibilities between objects.
21
Chain of Responsibility
• A way of passing a request between a chain of
objects.
Example :
The most obvious example of the Chain of Responsibility is the class
inheritance structure itself. When a method is called, that method is
passed up the inheritance chain until the first parent class containing
that method is found.
22
Chain of Responsibility
Applicability
28
Use Case Diagram Symbols/Notations
Symbols/ Notation Description
System scope This is represented by a rectangular hollow box. This is used to define the boundary of the
system.
Use case This is represented by an ellipse shape. This is used to represent a set of transactions that
provides some tangible value to the actors.
Maintain System
Actor An actor is represented by a stick man. An actor interacts with the use case to get some tangible
value from the system.
end user
Links Actors are linked with the use cases when either they contribute to the use case or get some
benefits from the use case. Use cases are linked with other use cases via includes and extends
relationship.
Includes If some functionality is common to multiple use cases then it can be separated as a use case and
denoted by <<includes>> stereotype.
Extends If there are two use cases in which one use case has a special type is functionality extending the
other use case then it is represented by using extension point and denoted by <<extends>>
stereotype.
Packages Packages are used to group the use cases. Package is represented as Insurance
29
Use Case Diagram
Salesman
Browse Catalog
<<include>>
<<extend>>
Update Sales Data
30
Sequence Diagram Symbols/Notations
Class roles The class role is a view of the class that helps achieve a particular
functionality.
Actor The notation and use of actor is same as that described in the use
case diagram. The actors start a sequence by acting upon the system.
Represented the same way as the actors of the use case diagram.
31
Sequence Diagram
reOrder()
if reorder level is
reached
32
Relationships Between Classes
33
Relationships Between Classes
Note:
Note: For
For details,
details, please
please refer
refer http://en.wikipedia.org/wiki/Class_diagram#Association
34
Relationships Between Classes
• Association
• Dependency
• Aggregation
• Generalization
When two classes are connected to each other in any way, an association
relation is established. For example: A "student studies in a college"
association can be shown as:
Student College
Person House
41
Activity Diagram Symbols/Notations
Activity This is represented by a rectangle whose vertical sides are represented as
semi-circles.
This is used to represent activity as the name suggests.
Receiv
eorder
Action flow Represented by lines with arrowheads. These are used to show how the
actions change the system behavior.
Initial state Notation and usage are same as that of the Statechart diagram.
End state Notation and usage are same as that of the Statechart diagram.
Swim lanes Swim lanes are represented by vertical bars with titles that divide the activity
diagrams into various sections. These are used to denote the processes used
by various groups in an organization
Synchronization This is represented by a horizontal or vertical bar. This is used to denote
concurrent activities.
States Notation and usage are same as that of the Statechart diagram. Denoted the
same way as the states in the state chart diagram.
42
Activity Diagram
Start
Receive Order
Fork
Activity
Branch
Join
Join
Close orders
End
43
Case Study
Soccer Game engine is to model and simulate the soccer game in a controlled
environment with the help of various parameter. The idea is to identify, reconsider and
analyze the situation of a game based on input fed by the user and decision taken by
the game engine based on analysis done by game engine algorithm in real time.
• It also address the problem of scalability and how future pattern can be
defined.
44
Case Study
Soccer Game Engine
• How you identify the entities in your game system
Identifying Entities :
First of all, you need to identify the objects you use in your game engine. For this,
you should visualize how the end user is going to use the system. Let us assume
that the end user is going to operate the game in the following sequence (let us
keep things simple).
Logical objects :
47
Contd…
To be decided
• How these objects are structured.
• How they are created.
• Their behavior when they interact each other, to formulate the design
specifications.
• Ball
When the position of a ball changes, all the players and the referee should be
notified straight away.
• Team and TeamStrategy
When the game is in progress, the end user can change the strategy of his team
(E.g., From Attack to Defend)
• Player
A player in a team should have additional responsibilities, like Forward, Defender
etc, that can be assigned during the runtime.
• PlayGround
Each ground constitutes of gallery, ground surface, audience, etc - and each
ground has a different appearance. 48
Contd…
Identifying Patterns To Use
1: Addressing the design problems related with the 'Ball‘
First of all, take the specifications related to the ball. You need to design a
framework such that when the state (position) of the ball is changed, all the
players and the referee are notified regarding the new state (position) of the ball.
Now, let us generalize the problem
Specific Design Problem: "When the position of a ball changes, all the players
and the referee should be notified straight away."
Problem Generalized: "When a subject (in this case, the ball) changes, all its
dependents (in this case, the players) are notified and updated automatically."
Once you have such a design problem, you refer the GOF patterns - and
suddenly you may find out that you can apply the 'Observer' pattern to solve the
problem.
Next, we have to address the specifications related to the team and team
strategy. When the game is in progress, the end user can change the strategy of
his team (E.g., From Attack to Defend). This clearly means that we need to
separate the Team's Strategy from the Team that uses it.
Specific Design Problem: "When the game is in progress, the end user can
change the strategy of his team (E.g., From Attack to Defend)"
Then, you can chose the 'Strategy' pattern to address the above design problem.
Strategy Pattern: Define a family of algorithms, encapsulate each one, and make
them interchangeable. Strategy lets the algorithm vary independently from
clients that use it.
50
Contd…
3: Addressing the design problems related with 'Player'
We need to assign responsibilities (like forward, defender etc) to each player
during run time. At this point, you can think about sub classing (i.e. inheritance) -
by creating a player class, and then inheriting classes like Forward, Defender etc
from the base class. But the disadvantage is that, when you do sub classing, you
cannot separate the responsibility of an object from its implementation.
I.e, In our case, sub classing is not the suitable method, because we need to
separate the responsibilities like 'Forward', 'Midfielder', 'Defender' etc from the
Player implementation. Because, a player can be a 'Forward' one time, and some
other time, the same player can be a 'Midfielder'.
52
Contd…
Understanding the Observer Pattern
53
Contd…
Now we will go ahead with first case (Addressing the design problems related with the 'Ball‘ , slide no. 47 )
Adapting the Observer Pattern
Observer Pattern 54
Contd…
Converting into Code
55
Contd…
56
Contd…
57
Contd…
58
Contd…
59
Contd…
Final Output
60