Top Banner
CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park
37

CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

CMSC 132: Object-Oriented Programming II

Nelson Padua-Perez

William Pugh

Department of Computer Science

University of Maryland, College Park

Page 2: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Class Diagrams

Represent the (static) structure of the system

General In JavaName Name

State Variables

Behavior Methods

Page 3: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Relationships Between Classes

AssociationPermanent, structural, “has a”

Solid line (arrowhead optional)

DependencyTemporary, “uses a”

Dotted line with arrowhead

GeneralizationInheritance, “is a”

Solid line with open (triangular) arrowhead

ImplementationDotted line with open (triangular) arrowhead

OR

Page 4: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Association

Denotes permanent, structural relationship

State of class A contains class B

Represented by solid line (arrowhead optional)

Car and Engine classes know about each other

Page 5: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Associations w/ Navigation Information

Can indicate direction of relationship

Represented by solid line with arrowhead

Gas Pedal class knows about Engine class Engine class doesn’t know about Gas Pedal class

Page 6: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Associations w/ Navigation Information

Denotes “has-a” relationship between classes

“Gas Pedal” has a “Engine”

State of Gas Pedal class contains instance of Engine class can invoke its methods

Page 7: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Multiplicity of Associations

Some relationships may be quantified

Multiplicity denotes how many objects the source object can legitimately reference

Notation* 0, 1, or more

5 5 exactly

5..8 between 5 and 8, inclusive

5..* 5 or more

Page 8: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Multiplicity of AssociationsMany-to-one

Bank has many ATMs, ATM knows only 1 bank

One-to-manyInventory has many items, items know 1 inventory

Page 9: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Dependency

Denotes dependence between classes

Always directed (Class A depends on B)

Represented by dotted line with arrowhead

A depends on B

A B

Page 10: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Dependency

Caused by class methods

Method in Class A temporarily “uses a” object of type Class B

Change in Class B may affect class A

A uses object of class B

A B

Page 11: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Dependency

Dependence may be caused byLocal variable

Parameter

Return value

Example

Class A { Class B {

B Foo(B x) { …

B y = new B(); …

return y; …

} } }

Page 12: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Dependency Example

Class Driver depends on Class Car

Page 13: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Generalization

Denotes inheritance between classes

Can view as “is-a” relationship

Represented by line ending in (open) triangle

Laptop, Desktop, PDA inherit state & behavior from Computers

Page 14: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Implementation

Denotes class implements Java interface

Represented by dotted line ending in (open) triangle

A implements interface B

A «B»

Page 15: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Examples

Read UML class diagram

Try to understand relationships

ExamplesPets & owners

Computer disk organization

Banking system

Home heating system

Printing system

Page 16: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Veterinary System

Try to read & understand UML diagram

Page 17: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Veterinary System

Try to read & understand UML diagram

• 1 or more Pets associated with 1 PetOwner

Page 18: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Computer System

Try to read & understand UML diagram

Page 19: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Computer System

Try to read & understand UML diagram

• 1 CPU associated with 0 or more Controllers

• 1-4 DiskDrives associated with 1 SCSIController

• SCSIController is a (specialized) Controller

Page 20: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Banking System

Try to read & understand UML diagram

Page 21: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Banking System

• 1 Bank associated with 0 or more Accounts

• Checking, Savings, MoneyMarket are Accounts

Page 22: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Example – Home Heating System

Try to read & understand UML diagram

Page 23: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML 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 Thermostat

Page 24: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Class Diagrams Java

Different representation of same informationName, state, behavior of class

Relationship(s) between classes

Practice deriving one from the other Accurately depicting relationship between classes

Page 25: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Java : Veterinary System

UML

Java

Page 26: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Java : Veterinary System

UML

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

Page 27: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Java UML : Veterinary System

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

UML

Page 28: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Java UML : Veterinary System

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

UML

Page 29: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Class Diagrams Java

UML

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

Page 30: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Java : Computer System

UML

Java

Page 31: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Java : Computer System

UML

Javaclass Controller {}class SCSIController extends Controller {}

Page 32: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Java : Computer System

UML

JavaDesign code using all available information in UML…

Page 33: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Java : Computer System

Javaclass CPU {

Controller [ ] myCtlrs;}class Controller {

CPU myCPU;}class SCSIController extends Controller {

DiskDrive [ ] myDrives = new DiskDrive[4];}Class DiskDrive {

SCSIController mySCSI;}

Page 34: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Java UML : Printing System

Javaclass Registry {

PrintQueue findQueue();}class PrintQueue {

List printJobs;Printer myPrinter;Registry myRegistry;void newJob();int length();Resources getResource();

}

Page 35: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Java UML : Printing System

JavaClass Printer {

Resources myResources;Job curJob;void print();boolean busy();boolean on();

}class Job {

Job(Registry r) {…

}}

Page 36: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Java UML : Printing System

Page 37: CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

UML Summary

Graphics modeling language

Visually represents design of software system

We focused on class diagramsContents of a class

Relationship between classes

You should be able toDraw UML class diagram given Java code

Write Java code given UML class diagram