Class Diagrams: Object-Oriented Design
Class Diagrams: Object-Oriented Design
Class Diagrams: Object-Oriented Design
Class Diagrams
John Leaney, Lian Loke, Lizveth Robles University of Technology, Sydney
Contributors
John Reekie, University of Technology, Sydney
Overview
So far:
Object-oriented design what it is Expressing requirements what system are we building
This week:
MyClass
Class B Class C
Class diagrams
CRC cards
Class Diagram
UML structural diagram for presenting the static model of a system
Use cases express a dynamic relationship (this, then that ..)
The static model is first constructed in the requirements analysis phase, ( for example with CRC cards) and greater detail is added in design, and as the design progresses. Typically shows:
Classes with attributes, operations, invariants. Relationships between classes
Example Class
+ + +
Class name
UML
BankAccount accountNumber: Integer Attributes balance: Money = 0 open(accountName: String): Boolean close( ): Boolean Operations withdraw (amount: Money): Boolean
Java
public class BankAccount { private int accountNumber; private Money balance; public boolean open(String accountName){ } }
Discuss!
More info
Class names are capitalized Attributes and operations have visibility, type, name Operations represent:
common behaviours of all instances of a class actions that can be carried out by, or on, an object services offered by a class specification for some aspect of behaviour of a class
For example:
change an instances data (i.e. attributes) respond to simple queries perform calculations send messages or events to other objects
Visibility
Public (+)
member of class is directly accessible to other objects; part of public interface
Private (-)
member is hidden from public use; only accessible to other members within the class
Protected (#)
special visibility scope applicable to inheritance only
My advice: Unless you have a good reason to do otherwise, make all operations public, and all attributes private
Java vs UML
You will note that the syntax is different
UML is a design language. It does not depend on any particular implementation language Java is an implementation language. It has nothing whatsoever to do with UML You have to know both!
Java code naturally grows data members and operations that arent in the UML
This is the old problem of keeping design specifications in sync with implementations
Responsibility
Some behaviour for which an object is held accountable
Collaboration
Process whereby several objects cooperate to provide some higherlevel behaviour
CRC Cards
Class Name: Responsibilities Responsibilities of a class are listed in this section. Collaborations Collaborations with other classes are listed here, together with a brief description of the purpose of the collaboration.
Class Name Responsibilities Provide client information. Provide list of campaigns. Class Name Responsibilities
Client Collaborations
Provide campaign information. Provide list of adverts. Add a new advert. Class Name Responsibilities Provide advert details. Construct adverts. Advert
Collaborations
Types of Responsibilities
Knowing
knowing about private encapsulated data knowing about related objects knowing about things it can derive or calculate
Doing
doing something itself, such as creating an object or doing a calculation initiating action in other objects controlling and coordinating activities in other objects
Examples
a Sale is responsible for knowing its total (POS example) a Sale is responsible for creating SalesLineItems
A CRC Session
The participants are users (ideally) and designers. A designer runs the session, records the data and resolves any disputes. The users contribute most of the ideas, as they are the people who know the current system. As classes, responsibilities and collaborators are discovered, they are written down, one class per card.
Other techniques
Users, experts, commonsense, imagination Noun phrase identification
Extract the nouns from a written description of the vision for the system (or from similar kinds of texts, eg. brief, system narrative, etc.) Classes are named by nouns
Stereotype classes
Classes can have stereotypical, or often found, roles. 3 main stereotypes in UML
Entity
representative of key entities or things in the application domain
Boundary
user or external interfaces
Control
coordinate or delegate to other objects
Other:
Collection classes
provide storage and management of a set of objects of the one type. e.g. array, linked list, vector
Patterns
A pattern is a common, recurring set of objects and relationships that exhibits characteristic behaviours.
http://java.sun.com/blueprints/patterns/MVC-detailed.html
If any answer is Yes, consider modelling the potential class in some other way (or do not model it at all)
Cherry
Fruit
Specialisation
Inheritance cont.
In UML
Advert
NewspaperAdvert
Polymorphism (1)
Dance time dress
numberDancers
Ball Room
Ballet
Break Dance
bow() perform()
Flamenco
Line Dance
Polymorphism (2)
Each child class has the operation perform. Perform has a different meaning for each class.
The dance is performed in a completely different way.
Each subclass must define its own perform operation. It should be possible to use any of the subclasses where the base class can be used (the Liskov substitution principle)
Cons
Excessively deep hierarchies become brittle and hard to understand There is on-going controversy on whether implementation inheritance is good (or whether we should just have interface inheritance)
Regardless, inheritance is firmly established in objectoriented analysis, design, and implementation. You must understand and be able to use it well.
Association
This relationship specifies the structural relationship between two classes (it is a needs-a relationship). Both classes are conceptually at the same level (i.e., none is more important than the other). An association can be named, for clarity.
throws
Dart needs the person, in order to be thrown
Dart
Person
Multiplicity
Multiplicity of an association shows how many of one class is related to how many of the other class.
Typical numbers are 0, 1, .. * (many) or 1..* (1 to many),
Person
Company
Role
A role is the face that a class at one end of an association presents to the class at the other end of the association.
If you use roles with an association, then you dont need to name the association.
Person
employee
employer
Company
role name may be implied as employer-employee relation
Navigation
In which direction(s) are messages sent along an association?
This is referred to as the navigability of the association.
Customer Person
1 places 1 throws
* *
Order Dart
Aggregation : whole-part
Sometimes we need to show the importance, the cumulative role, the bringing together relationship of one class. Thence we show the association as a whole-part (i.e., a has-a) relationship.
1
City
Skyscraper
Aggregation cont.
In UML
Campaign
0..*
Advert
In Java public class Campaign{ private Advert adverts[]; public Advert getAdvert(int id){ } }
Interfaces
A class realizes an interface So: read (at least) the references here:
http://www.softwarepractice.org/courseware/index.php/design/class_diagrams