Design Class Diagrams http flic krp87 i HCA
























































- Slides: 56
Design Class Diagrams http: //flic. kr/p/87 i. HCA
What are you going to learn about today? • Design class diagram notation http: //flic. kr/p/8 Jpk. Tg
Object-Oriented Design “Define software objects and how they collaborate to satisfy their requirements” You need to be able to reason about a design • To understand designer’s intent • To critique/improve the design
Source code not best medium for reasoning • Lots of redundancy and detail irrelevant for some program understanding tasks • Especially poor at depicting relationships among classes in OO programs Solution: Use abstract, visual representations— for example, Design Class Diagrams (DCDs)
Let’s learn UML class diagrams by example
Consider this Java class Java public class Airplane { private int speed; public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } UML
Based on this UML, how do you model… a class? Java UML public class Airplane { private int speed; Airplane public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } Box with class name—simple!
Based on this UML, how do you model… instance variables and methods? Java UML Attribute compartment public class Airplane { private int speed; Airplane public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } speed set. Speed get. Speed Operation compartment
Tip: Sketch classes starting from the upper left Sale Payment Store address
Based on this UML, how do you model… constructors? Java UML Constructor stereotype public class Airplane { private int speed; Airplane public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } speed <<constructor>> Airplane set. Speed get. Speed
Based on this UML, how do you model… attribute types? Java UML Attribute type public class Airplane { private int speed; Airplane public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } speed : int <<constructor>> Airplane set. Speed get. Speed
Based on this UML, how do you model… method signatures? Java UML public class Airplane { private int speed; public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } Airplane speed : int <<constructor>> Airplane() set. Speed(speed : int) get. Speed() : int Return type (omit void) Parameter list
Based on this UML, how do you model… visibility? Java public class Airplane { private int speed; public Airplane() { } public void set. Speed(int speed) { this. speed = speed; } public int get. Speed() { return speed; } } UML Airplane - speed : int + <<constructor>> Airplane() + set. Speed(speed : int) + get. Speed() : int - means private + means public # means protected
How to model a reference to a class instance? Java public class Airport {. . . } public class Airplane { private Airport home; private int speed; . . . } UML Airplane - speed : int + <<constructor>> Airplane() + set. Speed(speed : int) + get. Speed() : int
Like this? Java public class Airport {. . . } public class Airplane { private Airport home; private int speed; UML Airplane - home : Airport✖ - speed : int + <<constructor>> Airplane() + set. Speed(speed : int) + get. Speed() : int . . . } Not like this!
Like this—with an association (arrow)! Java public class Airport {. . . } public class Airplane { private Airport home; private int speed; . . . } UML Airplane - speed : int + <<constructor>> Airplane() + set. Speed(speed : int) + get. Speed() : int * 1 - home Airport
Distinguish between data types and non-data types! • Instances of data types are plain old values – Examples: integers, strings, dates, dollar amounts – No need to distinguish between two instances of the same dollar amount • Instances of non-data types have identity – Examples: Sale, Register, Airport, City, Employee – Even if two employees had the same name and salary, they would still be distinct employee instances • In Design Class Diagrams, model – attributes as data types – associations among non-data types
But what are these things? Airplane - speed : int + <<constructor>> Airplane() + set. Speed(speed : int) + get. Speed() : int * 1 - home Airport
Examples of multiplicities Also common to see 0. . * 8
How many Airplanes per Airport? How many Airports per Airplane? Airplane - speed : int + <<constructor>> Airplane() + set. Speed(speed : int) + get. Speed() : int 0 or more Airplanes per Airport 1 Airport per Airplane * 1 - home Airport
Based on this UML, how do you model… inheritance? Java public class Airplane {. . . } public class Jet extends Airplane {. . . } UML Airplane … … With a triangle arrow Jet … …
Based on this UML, how do you model… interfaces? Java UML public interface Flyable { public void fly(); } <<interface>> Flyable + fly() public class Airplane implements Flyable { public void fly() {. . . } Public operations only Interface stereotype With a dashed triangle arrow Airplane … + fly()
With these notations, you be able to express the structure of your object-oriented designs
Refinement process: Unfolding the design Design Classes refine Implementation public class Register { private int id; private Sale current. Sale; . . . } public class Sale { private Date. Time time; . . . }
Refinement is not mechanical One possible refinement class City {. . . protected: string city. Name; unsigned population; vector<Airport*> serves; }; class Airport {. . . protected: string airport. Name; CODE airport. Code; ZONE time. Zone; vector<City*> serves; }; Another possible refinement class City {. . . protected: string city. Name; unsigned population; }; class Airport {. . . protected: string airport. Name; CODE airport. Code; ZONE time. Zone; }; multimap<City*, Airport*> city. Serves; multimap<Airport*, City*> airport. Serves;
A word about agile modeling (quoting Larman) Experienced analysts and modelers know the secret of modeling: The purpose of modeling (sketching UML, …) is primarily to understand, not to document. Thus, we favor hand-drawn diagrams over typeset ones http: //flic. kr/p/7 SFKjj
Benefits of refinement process • Postpones design decisions until ready to make them • Manages complexity – Less refined designs more abstract http: //flic. kr/p/7 WYW 68
Summary • Design as refinement process • Lot o’ class diagram notations and conventions http: //flic. kr/p/YSY 3 X
Now, I’m going to introduce a bunch of new class diagram notations and conventions to help you make your DCDs more expressive (All notations from last time still apply)
Full format of attribute notation Class attr op • visibility name : type multiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibility name : type multiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibility name : type multiplicity = default {property} • Examples:
Visibility • + Public – Any class that can see the containing class can see • - Private – Only containing class can see • # Protected – Only containing class and its descendants can see • ~ Package – Only classes within containing package can see
Full format of attribute notation Class attr op • visibility name : type multiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibility name : type multiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibility name : type multiplicity = default {property} • Examples:
Full format of operation notation Class attr op • visibility name (parameters) : return-type {property} • Examples:
Full format of operation notation Class attr op • visibility name (parameters) : return-type {property} • Examples:
Full format of operation notation Class attr op • visibility name (parameters) : return-type {property} Same as with attributes • Examples:
Full format of operation notation Class attr op • visibility name (parameters) : return-type {property} • Examples:
Full format of operation notation Class attr op • visibility name (parameters) : return-type {property} • Examples:
Modeling attributes and associations public class Register { private int id; private Sale current. Sale; . . . } public class Sale { private Date. Time time; . . . }
Distinguish between data types and non-data types! • Instances of data types are plain old values – Examples: integers, strings, dates, dollar amounts – No need to distinguish between two instances of the same dollar amount • Instances of non-data types have identity – Examples: Sale, Register, Airport, City, Employee – Even if two employees had the same name and salary, they would still be distinct instances • In Design Class Diagrams, model – attributes as data types – associations among non-data types
Modeling an ordered list {list} Multiplicity Properties: set bag ordered set list (or sequence) unordered, unique elements (default) unordered, non-unique elements
Modeling a method
Omit setter and getter operations from design classes • Added noise outweighs value • Assume you have them if you need them
Modeling constructors
Generalization implies inheritance Objects of subclass have all attributes and methods of superclass
Modeling dependencies public class Foo { public void do. X() { System. run. Finalization(); . . . } Model like this Foo depends on System
Modeling interfaces An interface is essentially an abstract class with only abstract, public operations (no attributes and no methods)
Three ways to model constraints 1 3 2
Modeling hashes with qualified associations Multiplicities are different than usual: • (catalog, ID) 1 description • description 1 (catalog, ID) Implies catalog 0. . * description Key type If numbers, could be item. ID : Integer
Modeling relationships with association classes Each link between a Company object and a Person object is an instance of Employment
Modeling templates Binding notations:
User-defined compartments
Modeling active objects Each instance of Clock has its own thread of control that executes within run() Note the bars