CMSC 132: Object-Oriented Programming IINelson Padua-PerezWilliam PughDepartment of Computer ScienceUniversity of Maryland, College ParkUML Class DiagramsRepresent the (static) structure of the systemGeneral In JavaName NameState VariablesBehavior MethodsRelationships Between ClassesAssociationPermanent, structural, “has a”Solid line (arrowhead optional)DependencyTemporary, “uses a”Dotted line with arrowheadGeneralizationInheritance, “is a”Solid line with open (triangular) arrowheadImplementationDotted line with open (triangular) arrowheadORAssociationDenotes permanent, structural relationship State of class A contains class BRepresented by solid line (arrowhead optional)Car and Engine classes know about each otherAssociations w/ Navigation InformationCan indicate direction of relationshipRepresented by solid line with arrowheadGas Pedal class knows about Engine class Engine class doesn’t know about Gas Pedal classAssociations w/ Navigation InformationDenotes “has-a” relationship between classes“Gas Pedal” has a “Engine”State of Gas Pedal class contains instance of Engine class ⇒⇒⇒⇒can invoke its methodsMultiplicity of AssociationsSome relationships may be quantifiedMultiplicity denotes how many objects the source object can legitimately referenceNotation*⇒⇒⇒⇒0, 1, or more5⇒⇒⇒⇒5 exactly5..8⇒⇒⇒⇒between 5 and 8, inclusive5..*⇒⇒⇒⇒5 or moreMultiplicity of AssociationsMany-to-oneBank has many ATMs, ATM knows only 1 bankOne-to-manyInventory has many items, items know 1 inventoryDependencyDenotes dependence between classesAlways directed (Class A depends on B)Represented by dotted line with arrowheadA depends on B A BDependencyCaused by class methodsMethod in Class A temporarily “uses a” object of type Class BChange in Class B may affect class AA uses object of class B A BDependencyDependence may be caused byLocal variableParameterReturn valueExampleClass A { Class B {B Foo(B x) { …B y = new B(); …return y; …} } }Dependency ExampleClass Driver depends on Class CarGeneralizationDenotes inheritance between classesCan view as “is-a” relationshipRepresented by line ending in (open) triangleLaptop, Desktop, PDA inherit state & behavior from ComputersImplementationDenotes class implements Java interface Represented by dotted line ending in (open) triangleA implements interface B A «B»UML ExamplesRead UML class diagramTry to understand relationshipsExamplesPets & ownersComputer disk organizationBanking systemHome heating systemPrinting systemUML Example – Veterinary SystemTry to read & understand UML diagramUML Example – Veterinary SystemTry to read & understand UML diagram• 1 or more Pets associated with 1 PetOwnerUML Example – Computer SystemTry to read & understand UML diagramUML Example – Computer SystemTry to read & understand UML diagram• 1 CPU associated with 0 or more Controllers• 1-4 DiskDrives associated with 1 SCSIController• SCSIController is a (specialized) ControllerUML Example – Banking SystemTry to read & understand UML diagramUML Example – Banking System• 1 Bank associated with 0 or more Accounts• Checking, Savings, MoneyMarket are AccountsUML Example – Home Heating SystemTry to read & understand UML diagramUML Example – Home Heating System• Each Thermostat has 1 Room • Each Thermostat associated with 0 or more Heaters• ElectricHeater is a specialized Heater• AubeTH101D is a specialized ThermostatUML Class Diagrams ↔↔↔↔ JavaDifferent representation of same informationName, state, behavior of classRelationship(s) between classesPractice deriving one from the otherAccurately depicting relationship between classesUML →→→→ Java : Veterinary SystemUMLJavaUML →→→→ Java : Veterinary SystemUMLJavaclass Pet {PetOwner myOwner; // 1 owner for each pet}class PetOwner {Pet [ ] myPets; // multiple pets for each owner}Java →→→→ UML : Veterinary SystemJavaclass Pet {PetOwner myOwner; // 1 owner for each pet}class PetOwner {Pet [ ] myPets; // multiple pets for each owner}UMLJava →→→→ UML : Veterinary SystemJavaclass Pet {PetOwner myOwner; // 1 owner for each pet}class PetOwner {Pet [ ] myPets; // multiple pets for each owner}UMLUML Class Diagrams ↔↔↔↔ JavaUMLJavaclass Pet {PetOwner myOwner; // 1 owner for each pet}class PetOwner {Pet [ ] myPets; // multiple pets for each owner}UML →→→→ Java : Computer SystemUMLJavaUML →→→→ Java : Computer SystemUMLJavaclass Controller {}class SCSIController extends Controller {}UML →→→→ Java : Computer SystemUMLJavaDesign code using all available information in UML…UML →→→→ Java : Computer SystemJavaclass CPU {Controller [ ] myCtlrs;}class Controller {CPU myCPU;}class SCSIController extends Controller {DiskDrive [ ] myDrives = new DiskDrive[4];}Class DiskDrive {SCSIController mySCSI;}Java →→→→ UML : Printing SystemJavaclass Registry {PrintQueue findQueue();}class PrintQueue {List printJobs;Printer myPrinter;Registry myRegistry;void newJob();int length();Resources getResource();}Java →→→→ UML : Printing SystemJavaClass Printer {Resources myResources;Job curJob;void print();boolean busy();boolean on();}class Job {Job(Registry r) {…}}Java →→→→ UML : Printing SystemUML SummaryGraphics modeling languageVisually represents design of software systemWe focused on class diagramsContents of a classRelationship between classesYou should be able toDraw UML class diagram given Java codeWrite Java code given UML class
View Full Document