Class Diagram & GRASP Patterns
Class Diagram & GRASP Patterns
Majid Hussain
Class Diagram and Grasp pattern
Revision Up till Now
High- System
Set of Use
Level Domain Model Sequenc
Requireme Case e
and
nts Diagram Diagram
Expanded
Use cases
Use case
Identification Main
Non- Functional • Boss test Success
Functional Requiremen • Elementary Business Scenarios
Requiremen ts Process
ts • Size Test
Represent feature/functions/user goals
Attribute
type Name of the class
Attribute
name Student
Operation
name Return value
Parameters
Navigability
Order
+ dateReceived: Date [0..1]
+ isPrepaid: Boolean
+ lineItems: OrderLine [*]
Bidirectional Association
Person Car
+ carsOwned: Car [*] + Owner: Person [0..1]
Generalization/Specialization
• Generalization hierarchies may be created by generalizing
from specific things or by specializing from general things.
Inheritance
• Class inheritance is implicit in a generalization relationship
between classes.
• Subclasses inherit attributes, associations, & operations
from the superclass
What is the
inheritance
mechanism in Java?
Inheritance
Java Inheritence
• A subclass may override an inherited aspect
• e.g. AdminStaff & CreativeStaff have different
• methods for calculating bonuses
• A Subclass may add new features
• qualification is a new attribute in CreativeStaff
• Superclasses may be declared {abstract}, meaning
they have no instances
• Implies that the subclasses cover all possibilities
• e.g. there are no other staff than AdminStaff and
CreativeStaff
Aggregation
• This is the “Has-A” relationship
aggregation
Club Member
* *
:Car :Train
0..1 0..1
:Person 0..*
driver 1 passengers
aggregation
Aggregation and Composition
• Composition is strong form of aggregation that
implies ownership:
• if the whole is removed from the model, so is the part.
• the whole is responsible for the disposition of its parts
• Note: Parts can be removed from the composite (where allowed) before
the composite is deleted
1 :Engine
composition
:Locomotive1..*
1 0..1
:Car :Train
0..1 0..1
:Person 0..*
driver 1 passengers
aggregation
UML CLASS DIAGRAM ACTIVITY
A hockey league is made up of at least four hockey teams. Each hockey team is
composed of six to twelve players, and one player captains the team. A team has
a name and a record. Players have a number and a position. Hockey teams play
games against each other. Each game has a score and a location. Teams are
sometimes lead by a coach. A coach has a level of accreditation and a number of
years of experience, and can coach multiple teams. Coaches and players are
people, and people have names and addresses.
Draw a class diagram for this information, and be sure to label all associations
with appropriate multiplicities that are required to develop the above system.
13
14
Object-Oriented Design
• “After identifying your requirements and creating a
domain model, then add methods to the software
classes, and define the messaging between the
objects to fulfill the requirements.”
• But how?
• How should concepts be implemented by classes?
• What method belongs where?
• How should the objects interact?
• This is a critical, important, and non-trivial task
Why Patterns?
• A Design Model may have hundreds or even thousands of software classes and
hundreds or thousands of responsibilities to be assigned.
• During object design, when interactions between objects are defined; we make
choices about the assignment of responsibilities to software classes..
16
17
Design Patterns - GRASP A design pattern is a general
repeatable solution to a
commonly occurring problem
in software design
• Information Expert
• Creator
• High Cohesion
• Low Coupling
• Controller
Controller
• A simple layered architecture has a user interface layer (UI) and a
business logic layer.
• Actors, such as the human user, generate UI events (such as clicking a
button).
• The UI software objects (such as a JFrame window and a JButton) must
process the event.
• When objects in the UI layer pick up an event, they must delegate the
request to an object in the domain layer.
• Problem: What first object in the business logic layer should receive the
message from the UI layer?
OR in other words
• Who should be responsible for handling system events ?
Controller
• The Controller is also an important idiom in modern web
development frameworks, in forming a pillar of the Model-
View-Controller architectural pattern. Controllers are used
in AngularJS, Ruby on Rails, Sails and more.
20
Problem: What first object in the
business logic layer should
receive the message from the UI
layer?
Solution:
Façade Controller to hide the
complexity
• Works as a wrapper
• Should not contain
business logic
Controller
• Which class will receive the first system call?
• Deals with how to delegate the request from the UI layer objects to
domain layer objects.
• It delegates the work to other class and coordinates the overall activity.
Façade Controller
Sequence Diagram
26
Controller Demo
27
28
Information Expert
• Problem : What is general principle of assigning
responsibilities to Objects?
• classes that know too much about other classes make changes hard to
coordinate and make code brittle and difficult to reuse.
• If class A knows too much about class B, changes in class B may break
the functionality in class A.
Low Coupling
• Problem: How to achieve low dependency, low change
impact, increase re-use ?
Low
coupling
Coupling
39
Example (High Coupling)
Example (Low Coupling)
Example
Domain Model System Sequence Diagram
Sequence Diagram
Design Alternative
o r
o
P si g n
D e
High Cohesion
• Cohesion refers to how the functions of a class belong
together. Related code should be close to each other to
make it highly cohesive.
44
High Cohesion
• How are the operations of any element
functionally related?
• Benefits
• Easily understandable and maintainable.
• Code reuse
• Low coupling
Example of Low Cohesion
Example of High Cohesion
Pure Fabrication
• If domain model provides no reasonable concept to
assign responsibility without violating
cohesion/coupling -> create a new abstraction (e.g.,
PersistantStorage).
Sale
date
time
Contains
:Sale
*
Product date
SalesLineItem Specification t := getTotal()
: Sale time
* 1
Described by description getTotal()
quantity
price
itemID
Domain Model
49
System Sequence Diagram
50
makeNewSale()
Controller ?
Information Expert ??
Creator ??
51
Explanation
Register may be thought of as recording a Sale
• Register is a reasonable candidate for creating a Sale.
• By having the Register create the Sale, the Register can easily be
associated with it over time,
• During future operations within the session, the Register will have a
reference to the current Sale instance.
Therefore:
• the Register creates the Sale
• the Sale creates an empty collection, represented by a multiobject in the
interaction diagram.
52
enterNewItem(itemID, quantity)
53
54
Code level
view
55
Class Diagram, So far
56
endSale – Design Decisions
• Sale is to be completed
• Total with tax calculated is presented
57
endSale()
58
59
getTotal()
60
makePayment(cashTendered)
61
62
63