560 likes | 574 Views
Design Class Diagrams. http://flic.kr/p/87iHCA. What are you going to learn about today?. Design class diagram notation. http://flic.kr/p/8JpkTg. Object-Oriented Design. “ Define software objects and how they collaborate to satisfy their requirements ”.
E N D
Design Class Diagrams http://flic.kr/p/87iHCA
What are you goingto learn about today? • Design class diagram notation http://flic.kr/p/8JpkTg
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)
Consider this Java class Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } }
Based on this UML, how do you model…a class? Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } } Airplane Box with class name—simple!
Based on this UML, how do you model…instance variables and methods? Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } } Airplane speed setSpeed getSpeed Operation compartment Attribute compartment
Tip: Sketch classes starting from the upper left Sale Payment Store address
Based on this UML, how do you model…constructors? Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } } Airplane speed <<constructor>> Airplane setSpeed getSpeed Constructor stereotype
Based on this UML, how do you model…attribute types? Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } } Airplane speed : int <<constructor>> Airplane setSpeed getSpeed Attribute type
Based on this UML, how do you model…method signatures? Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } } Airplane speed : int <<constructor>> Airplane() setSpeed(speed : int) getSpeed() : int Parameterlist Return type(omit void)
Based on this UML, how do you model…visibility? Java UML public class Airplane { private int speed; public Airplane() { } public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } } Airplane - speed : int + <<constructor>> Airplane() + setSpeed(speed : int) + getSpeed() : int - means private + means public # means protected
How to model a reference to a class instance? Java UML public class Airport { ... } public class Airplane { private Airport home; private int speed; ... } Airplane - speed : int + <<constructor>> Airplane() + setSpeed(speed : int) + getSpeed() : int
Like this? Java UML public class Airport { ... } public class Airplane { private Airport home; private int speed; ... } Airplane - home : Airport - speed : int ✖ + <<constructor>> Airplane() + setSpeed(speed : int) + getSpeed() : int Not like this!
Like this—with an association (arrow)! Java UML public class Airport { ... } public class Airplane { private Airport home; private int speed; ... } Airplane - speed : int + <<constructor>> Airplane() + setSpeed(speed : int) + getSpeed() : int * - home 1 Airport
Distinguish between data typesand 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() + setSpeed(speed : int) + getSpeed() : int * - home 1 Airport
Examples of multiplicities Also commonto see 0..* 8
How many Airplanes per Airport?How many Airports per Airplane? Airplane - speed : int + <<constructor>> Airplane() + setSpeed(speed : int) + getSpeed() : int * 0 or more Airplanesper Airport - home 1 1 Airport perAirplane Airport
Based on this UML, how do you model…inheritance? Java UML public class Airplane { ... } public class Jet extends Airplane { ... } Airplane … … With a triangle arrow Jet … …
Based on this UML, how do you model…interfaces? Interface stereotype Java UML public interface Flyable { public void fly(); } public class Airplane implements Flyable { public void fly() { ... } ... } <<interface>> Flyable + fly() With a dashed triangle arrow Airplane Public operations only … + 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 currentSale; ... } public class Sale { private DateTime time; ... }
Refinement isnot mechanical Another possiblerefinement One possiblerefinement class City { ... protected: string cityName; unsigned population; }; class Airport { ... protected: string airportName; CODE airportCode; ZONE timeZone; }; multimap<City*, Airport*> cityServes; multimap<Airport*, City*> airportServes; class City { ... protected: string cityName; unsigned population; vector<Airport*> serves; }; class Airport { ... protected: string airportName; CODE airportCode; ZONE timeZone; vector<City*> serves; };
A word about agile modeling(quoting Larman) Experienced analysts and modelers know thesecret of modeling: Thus, we favor hand-drawn diagrams over typeset ones The purpose of modeling (sketching UML, …) is primarily to understand, not to document. http://flic.kr/p/7SFKjj
Benefits of refinement process • Postpones design decisions until ready to make them • Manages complexity • Less refined designs more abstract http://flic.kr/p/7WYW68
Summary • Design as refinement process • Lot o’ class diagram notations and conventions http://flic.kr/p/YSY3X
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 • visibilityname : typemultiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibilityname : typemultiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibilityname : typemultiplicity = 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 • visibilityname : typemultiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibilityname : typemultiplicity = default {property} • Examples:
Full format of attribute notation Class attr op • visibilityname : typemultiplicity = default {property} • Examples:
Full format of operation notation Class attr op • visibilityname (parameters) : return-type {property} • Examples:
Full format of operation notation Class attr op • visibilityname (parameters) : return-type {property} • Examples:
Full format of operation notation Class attr op • visibilityname (parameters) : return-type {property} • Examples: Same as with attributes
Full format of operation notation Class attr op • visibilityname (parameters) : return-type {property} • Examples:
Full format of operation notation Class attr op • visibilityname (parameters) : return-type {property} • Examples:
Modeling attributes and associations public class Register { private int id; private Sale currentSale; ... } public class Sale { private DateTime time; ... }
Distinguish between data typesand 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:
Omit setter and getter operationsfrom design classes • Added noise outweighs value • Assume you have them if you need them
Generalization implies inheritance Objects of subclass have all attributes and methods of superclass
Modeling dependencies public class Foo { public void doX() { System.runFinalization(); ... } ... } Foo depends on System Model like this
Modeling interfaces An interface is essentially an abstract class with only abstract, public operations (no attributes and no methods)