IT 240 Programming Paradigms - PowerPoint PPT Presentation

About This Presentation
Title:

IT 240 Programming Paradigms

Description:

... and the Structure Chart. main() compute() scanf ... Superset of ASCII. Internationalization. Still compatible with integer types. Sizes and Ranges ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 140
Provided by: johnpaul5
Category:

less

Transcript and Presenter's Notes

Title: IT 240 Programming Paradigms


1
IT 240Programming Paradigms
  • MS Information Technology Offshore Program
  • Ateneo de Davao
  • Session 3

2
Course Outline Revisited
  • Programming Language Concepts
  • Survey of Languages and Paradigms
  • Imperative, Functional, Object-Oriented
  • Focus OO Programming
  • OO Languages (Java and C)
  • OO Design (UML)
  • Advanced Topics (Design Patterns)

3
Schedule This Weekend
  • Friday Evenin
  • OOP Concepts
  • Saturday Morning
  • OOP Languages More Java
  • Saturday Afternoon
  • OOP Languages C, contrast with Java
  • Intro to OO Design the Unified Modeling
    Language (UML)

4
Key OOP Concepts
  • Object, Class
  • Instantiation, Constructors
  • Encapsulation
  • Inheritance and Subclasses
  • Abstraction
  • Reuse
  • Polymorphism, Dynamic Binding

5
Object
  • Definition a thing that has identity, state,
    and behavior
  • identity a distinguished instance of a class
  • state collection of values for its variables
  • behavior capability to execute methods
  • variables and methods are defined in a class

6
Class
  • Definition a collection of data (fields/
    variales) and methods that operate on that data
  • data/methods define the contents/capabilities of
    the instances (objects) of the class
  • a class can be viewed as a factory for objects
  • a class defines a recipe for its objects

7
Instantiation
  • Object creation
  • Memory is allocated for the objects fields as
    defined in the class
  • Initialization is specified through a constructor
  • a special method invoked when objects are created

8
Encapsulation
  • A key OO concept Information Hiding
  • Key points
  • The user of an object should have access only to
    those methods (or data) that are essential
  • Unnecessary implementation details should be
    hidden from the user
  • In Java/C, use classes and access modifiers
    (public, private, protected)

9
Inheritance
  • Inheritance
  • programming language feature that allows for the
    implicit definition of variables/methods for a
    class through an existing class
  • Subclass relationship
  • B is a subclass of A
  • B inherits all definitions (variables/methods) in
    A

10
Abstraction
  • OOP is about abstraction
  • Encapsulation and Inheritance are examples of
    abstraction
  • What does the verb abstract mean?

11
Reuse
  • Inheritance encourages software reuse
  • Existing code need not be rewritten
  • Successful reuse occurs only through careful
    planning and design
  • when defining classes, anticipate future
    modifications and extensions

12
Polymorphism
  • Many forms
  • allow several definitions under a single method
    name
  • Example
  • move means something for a person object but
    means something else for a car object

13
Dynamic Binding
  • The capability of an implementation to
    distinguish between the different forms during
    run-time

14
Visual and Event-driven Programming
  • Fits very well with the OO Paradigm
  • Visual Programming and GUIs
  • windows, icons, buttons, etc. are objects created
    from ready-made classes
  • Event-driven Programming
  • execution associated with user interaction with
    visual objects that causes the invocation of
    other objects methods

15
Whats Next?
  • OO Languages
  • how are these concepts implemented in language
    platforms such as Java and C
  • implementation tradeoffs
  • OO Design
  • the success of the paradigm lies on how well
    classes are designed
  • need models and techniques (the UML) that allow
    for good planning and design

16
Report Topic
  • Part of the requirement for this course is a
    report to be presented on my next visit
  • Topic on OO Paradigm
  • propose a topic by tomorrow
  • report on a language, an environment, or an
    application of OOP (e.g., files or networking)
  • Requirements
  • 15-20 minute presentation, 2-page report
  • hands-on demo (unless excusable)

17
Java
18
Computing in the 1990s
  • The Internet and the WWW
  • from retrieving documents to executing remote
    programs
  • Graphical User Interfaces (GUIs)
  • visual and event-driven programming
  • Object-oriented Programming (OOP)

19
Java Intro Summary(Last Session)
  • Simple Java Application
  • HelloWorld example
  • Standalone Java program
  • public static void main( )
  • Simple Java Applet
  • HelloAgain example
  • Executed through browser or appletviewer
  • Requires .html file

20
Lab Exercise 1TextCopy Applet
  • Filename TextCopy.java
  • Variables
  • two TextField variables
  • one Button variable
  • Methods
  • init() creates and displays the UI objects
  • action() processes UI events

21
Invoking Methods on Objects
  • Syntax for method invocation
  • object.methodName(arguments)
  • Example message.setText(Hello)
  • calls setText method on a TextField object
  • To find out what methods are available for a
    given class
  • javap package.name.NameOfClass
  • ex. javap java.awt.TextField

22
Java Program Structure
  • Java Class
  • (optional) import declarations
  • class declaration
  • Class
  • class name should match its file name
  • extends for inheritance
  • contains method/function definitions

23
The Paradigm Change
  • From a structured collection of functions
  • procedural programming
  • To a collection of interacting objects
  • object-oriented programming

24
Procedural Programming and the Structure Chart
main()
compute()
scanf()
print_results()
printf()
25
Procedural Programming and DFDs
Inventory Management
Accept and Post Delivery
Delivery info
Transaction
Item Master
26
OO CounterpartObject Interaction
new (delivery info)
Encoder
Transaction
post (item count)
Item Master
27
OOP and Object Interaction
  • Objects pass messages to each other
  • An object responds to a message by executing an
    associated method defined in its class
  • Causes a chain reaction
  • The user could be viewed as an object
  • Depicted in an Object Interaction Diagram

28
Hello.java Application
println()
System.out object
29
HelloAgain Applet Creation
USER
BROWSER
1 Open HA.html
2 new 3 paint()
HelloAgain Applet
g Graphics object
4 drawString()
Note new means the object of class HelloAgain
is created
30
HelloAgain AppletAnother Scenario
USER
BROWSER
1 Move the browser window
2 paint()
HelloAgain Applet
g Graphics object
3 drawString()
31
TextCopy AppletCreation
USER
BROWSER
1 Open the .html file
2 new 3 init()
4 new 5 setText()
TextCopy Applet
message TextField object
7 new
6 new
copy Button object
destination TextField object
32
TextCopy AppletClicking on the Button
USER
BROWSER
1 Click on button
2 action()
3 getText()
HelloAgain Applet
message TextField object
copy Button object
4 setText()
destination Button object
33
Java Versus C
34
Language Differences
  • Compilation and Execution
  • Data Types and Operators
  • Variables
  • Others

35
C ProgramCompilation and Execution
  • prog.c is compiled to prog.exe
  • for multiple modules, produce prog.obj first and
    then link with other .obj files
  • prog.exe is a readily executable binary-coded
    program
  • Execution begins with the function main() in
    the C program

36
Java ProgramCompilation and Execution
  • Prog.java is compiled to Prog.class
  • Execution
  • for applets, the browser loads Prog.class and UI
    events can then be processed
  • for applications, a Java interpreter loads
    Prog.class and causes program execution to begin
    in the main() method of this class

37
The Java Virtual Machine
  • Browsers and the Java interpreters have to
    simulate this standard machine
  • Compile once, run anywhere
  • Class Loader
  • The JVM facilitates the loading and execution of
    classes
  • Several classes, not just one class, are loaded
  • Java class library

38
Data Types
  • Most C types apply to Java
  • int, char, float, double
  • Other primitive types
  • short, long (also available in C)
  • byte, boolean
  • Main difference
  • In Java, the size of a data type type is strictly
    specified (in C, size depends on the machine)

39
Value Ranges for theJava Data Types
  • boolean true, false
  • size 1 bit
  • Not compatible with integer types as in C
  • char Unicode character set
  • size 2 bytes
  • Superset of ASCII
  • Internationalization
  • Still compatible with integer types

40
Sizes and Rangesfor Other Java Types
  • int 4 bytes
  • -2,147,483,648 to 2,147,483,647
  • float 4 bytes
  • 1.01e-45 to 3.40e38
  • double 8 bytes
  • 4.94e-324 to 1.80e308

41
Operators
  • All operators in C apply
  • , , and, ! now apply to boolean operands only
  • and as boolean operators do not perform
    short-cutting
  • can be distinguished from integral bitwise
    operations
  • for String concatenation

42
Two Kinds of Variables
  • Variables of a primitive type
  • e.g., int x char c
  • Variables of a reference type (class)
  • e.g., Button b String s
  • Conventions
  • Primitive types are reserved words in Java and
    are indicated in all-lower-case letters
  • Class names first letter usually capitalized

43
Variables and Values
  • Primitive type variables
  • int x
  • x 5

X
X
5
44
Variables and References
  • Reference type variables
  • Button x
  • x new Button(copy)

X
Button Object
copy
X
45
The new Keyword
  • new Button(copy) creates a Button object and
    returns a reference (an address) to that object
    that a Button variable could hold

Button Object
copy
X
1023
1023
1023 is some address in memory
46
The null Keyword
  • Use null to indicate (or test) that the variable
    does not currently refer to an object
  • x null
  • if (x null) ...

X
null
47
Global Variables
  • Java has no global variables
  • Scope of variables analogous to a single C source
    program containing several functions
  • Within a class, variables declared outside the
    methods are available to every method
  • Variables declared within a method (including
    parameters) are local to that method

48
Prototypes in C
  • In C, prototypes are sometimes required
  • double squareroot(int) / prototype /
  • d squareroot(5) / used before definition /
  • double squareroot(int num) / definition /
  • In Java, the compiler reads the program at least
    twice to resolve usage

49
include vs import
  • In C,
  • include ltsomefile.hgt contains the macros and
    prototypes that enable a program to call
    functions defined in the standard library
  • In Java,
  • import some.package. enables a program to refer
    to existing classes (particularly those from the
    class library) by its simple name (Button versus
    java.awt.Button)

50
Statements
  • All control structures follow the same syntax
    (if, switch, while, do-while, for)
  • In a compound statement or block, variable
    declarations and statements may intersperse
  • New statement for exception handling try-catch

51
Arrays
  • Declaration double nums
  • Creation nums new double8
  • Use nums3 6.6
  • Note starting index is 0 (0 to 7, above)

52
Visualizing an Array
nums
nums new double8
6.6
nums3 6.6
double nums
53
Array of Objects
TextField object
slots
TextField object
TextField object
TextField object
TextField object
54
Classes and Objectsin Java
55
Variables and Objects
  • Let Circle be a class with
  • variable r that indicates its radius
  • method area() that computes its area
  • Declaration Circle c
  • Instantiation c new Circle()
  • Usage c.r 5.5
  • System.out.println(c.area())

56
The complete Circle class
  • public class Circle
  • public double x,y // center coordinates
  • public double r // radius
  • // the methods
  • public double circumference()
  • return 23.14r
  • public double area() return 3.14rr

57
Using the Circle class
  • public class TestCircle
  • public static void main(String args)
  • Circle c
  • c new Circle()
  • c.x 2.0 c.y 2.0 c.r 5.5
  • System.out.println(c.area())

58
The this keyword
  • this refers to the current object
  • In the Circle class, the following definitions
    for area() are equivalent
  • public double area() return 3.14 r r
  • public double area() return 3.14 this.r
    this.r
  • Using the keyword clarifies that you are
    referring to a variable inside an object
  • dot operator used consistently

59
Constructors
  • A constructor is a special type of method
  • has the same name as the class
  • It is called when an object is created
  • new Circle() // calls the Circle() method
  • If no constructor is defined, a default
    constructor that does nothing is implemented

60
A constructor for the Circle class
  • public class Circle
  • public double x,y // center coordinates
  • public double r // radius
  • public Circle()
  • // sets default values for x, y, and r
  • this.x 0.0 this.y 0.0 this.r 1.0
  • ...

61
A constructor with parameters
  • public class Circle
  • public Circle(double x, double y, double z)
  • this.x x this.y y this.r z
  • // using this is now a necessity
  • ...

62
Using the different constructors
  • Circle c, d
  • c new Circle()
  • // radius of circle has been set to 1.0
  • System.out.println(c.area())
  • d new Circle(1.0,1.0,5.0)
  • // radius of circle has been set to 5.0
  • System.out.println(d.area())

63
Method Overloading
  • In Java, it is possible to have several method
    definitions under the same name
  • but the signatures should be different
  • Signature
  • the name of the method
  • the number of parameters
  • the types of the parameters

64
Encapsulation in Java
  • Access modifiers
  • public
  • a public variable/method is available for use
    outside the class it is defined in
  • private
  • a private variable/method may be used only within
    the class it is defined in

65
The Circle class Revisited
  • public class Circle
  • private double x,y // center coordinates
  • private double r // radius
  • // ...
  • // when using the Circle class ...
  • Circle c
  • c.r 1.0 // this statement is not allowed

66
Outside accessto private data
  • No direct access
  • Define (public) set and get methods instead or
    initialize the data through constructors
  • Why?
  • If you change your mind about the names and even
    the types of these private data, the code using
    the class need not be changed

67
Set and Get Methods
  • Variables/attributes in a class are often not
    declared public
  • Instead
  • define use a (public) set method to assign a
    value to a variable
  • define a get method to retrieve that value
  • Consistent with encapsulation

68
Set and Get Methods for Radius
  • public class Circle
  • // ...
  • private double r // radius
  • //
  • public void setRadius(double r) this.r r
  • public double getRadius() return this.r
  • // ...

69
Inheritance in Java
70
The extends Keyword
  • In Java,
  • public class B extends A
  • means B is a subclass of A
  • objects of class B now have access to variables
    and methods defined in A

71
The EnhancedCircle class
  • public class EnhancedCircle extends Circle
  • // as if area(), circumference(), setRadius()
    and getRadius()
  • // automatically defined x,y,r are also
    present (but are private
  • // to the the Circle class)
  • private int color
  • public void setColor(int c) this.color c
  • public void draw()
  • public double diameter()
  • return this.getRadius()2

72
Using a Subclass
  • EnhancedCircle c
  • c new EnhancedCircle() // Circle()
    constructor
  • //
    implicitly invoked
  • c.setColor(5)
  • c.setRadius(6.6)
  • System.out.println(c.area())
  • System.out.println(c.diameter())
  • c.draw()

73
Applets and Inheritance
  • Java Applets that we write extend the Applet
    class (defined in package java.applet)
  • Methods such as add() (for adding visual
    components) are actually methods available in the
    Applet class
  • init(), action(), and paint() are also available
    but can be overridden

74
Class Hierarchy
  • Subclass relationship forms a hierarchy
  • Example TextField class
  • TextField extends TextComponent which extends
    Component which extends Object
  • Object is the topmost class in Java
  • Exercise (use javap)
  • determine where the methods setText() and
    getText() are defined

75
Superclass Variables,Subclass Objects
  • Let B be a subclass of A
  • It is legal to have variables of class A to refer
    to objects of class B
  • Example
  • Circle c
  • c new EnhancedCircle()

76
Method Overriding
  • A method (with a given signature) may be
    overridden in a subclass
  • Suppose class B extends A
  • let void operate() be a method defined in A
  • void operate() may be defined in B
  • objects of class A use As operate()
  • objects of class B use Bs operate()

77
Dynamic Binding
  • Let A be a superclass of subclasses B and C
  • A variable of class A may refer to instances of
    A, B, and C
  • Java facilitates the calling of the appropriate
    method at run time
  • Example
  • A v v.operate()

78
Constructors and Superclasses
  • Suppose B extends A
  • new B() calls Bs constructor
  • how about As constructor ?
  • Rule
  • the constructor of a superclass is always invoked
    before the statements in the subclass
    constructor are executed

79
super()
  • Used to call a superclass constructor
  • Implicitly included when not indicated
  • If B extends A, the following are equivalent
  • public B() public B()
  • // body of constructor super()
  • // body of constructor

80
Calling a particular Constructor
  • Use super with parameters if a particular
    constructor should be called
  • Example
  • public class BlueButton extends Button
  • public BlueButton(String s)
  • super(s) // without this, super() is called
    (label-less)
  • setBackground(Color.blue)

81
More Uses for super
  • When overriding a method, one can merely extend
    the method definition
  • public class Manager extends Employee
  • public void increase()
  • super.increase() // call Employees
    increase
  • // do more stuff

82
Visual and Event-Driven Programming in Java
83
Lab Exercise II
  • Create a BankAccount class
  • Methods deposit(), withdraw(), getBalance(),
    addInterest()
  • Create a separate Bank class (Java application)
    that tests BankAccount
  • Create BankAccount object(s) and invoke methods
  • Create a CheckingAccount class
  • Methods same as BankAccount, but add a drawCheck
    method
  • Add code in Bank to test CheckingAccount

84
The Java AWT
  • Components
  • Button, TextField, Label, Panel
  • Others
  • Layout Managers
  • FlowLayout, GridLayout, BorderLayout
  • CardLayout, GridBagLayout
  • add() in applet

85
Components
  • Button clickable visual object
  • Label text
  • TextField
  • contains editable text
  • setText() and getText()
  • Panel
  • contains other visual components
  • setLayout() and add()

86
Layout Managers
  • FlowLayout
  • objects are placed row by row, left to right
  • GridLayout
  • divides container into an m by n grid
  • BorderLayout
  • divides container into 5 parts
  • Center, North, South, East, West

87
Event Processing in the (old) JDK 1.0.2
  • What needs to be done
  • init() method create visual components and add
    them to the applet
  • action() determine which button was clicked and
    indicate associated action
  • Problems
  • nested if-statement in action() inefficient
  • code associated with visual object far from its
    definition

88
Event Processing inJDK 1.1 and higher
  • Listener objects
  • responsible for processing UI events
  • specifies actions that corresponds to an event
  • JDK 1.1
  • need to associate a listener for every visual
    object that the user interacts with
  • listener should implement method(s) that specify
    corresponding actions

89
Graphics in Java
  • The paint() method
  • Takes a Graphics object as a parameter
  • Graphics methods
  • drawString(s, x, y)
  • drawOval(x, y, r1, r2)
  • drawRect(x1, y1, x2, y2)
  • javap java.awt.Graphics

90
paint()
  • Describes current picture of the applet
  • repaint()
  • method of Applet
  • call this method when your drawing should be
    updated
  • automatically called but system not always aware
    of updates

91
Example MovingCircle
  • Extends Applet
  • Variables
  • x, y, r position and size of circle
  • move Button variable
  • Methods
  • init() initialize variables
  • action() when button pressed, update x
  • paint() g.drawOval(x,y,r,r)

92
Lab Exercise III
  • Implement MovingCircle
  • Arrange it so that a Circle class is used
  • define and initialize a Circle object in the
    applet
  • instead of x, y, and r
  • Maintain several Circle objects
  • have an add button that adds Circles

93
Java vs C
94
Outline
  • Program Structure and Execution
  • Encapsulation and Inheritance
  • Objects and Variables
  • Constructors
  • Methods and Operators
  • Containers and Reuse
  • GUI Programming

95
Program Structure
  • Class definition similar in Java and C
  • Java two types of programs
  • application (with main() function)
  • applet (typically embedded in a web page)
  • C
  • a program is still a collection of functions that
    may use objects and classes
  • main() function serves as driver

96
Program Execution
  • Java Virtual Machine (VM)
  • programs both compiled and interpreted
  • compiler produces .class from .java
  • VM loads .class file(s) as needed
  • C compiled, linked, and loaded
  • modules separately compiled
  • linked to produce executable
  • static vs dynamic libraries

97
Encapsulation
  • Enforced through access keywords
  • public for interface
  • private to make implementation inaccessible
  • protected access for subclasses only
  • In Java
  • each member is prefixed with a keyword
  • In C
  • public, private, and protected sections

98
Breaking Encapsulation
  • Possible in C through the friend keyword
  • A method or class may be declared as a friend of
    an existing class
  • Allows access to private members
  • A friend is someone who has access to your
    private parts.

99
Inheritance
  • Feature that allows a class to be defined based
    on another class
  • methods and attributes are inherited
  • Java and C difference
  • Java public class A extends B
  • C class A B
  • Multiple inheritance possible in C, not in Java

100
Objects and Identity
  • Questions
  • How/when are objects created?
  • What is the relationship between a variable and
    an object?
  • Difference between Java and C
  • distinction between primitive (built-in) type
    variables and variables for objects
  • reference relationship between variable and
    actual object

101
Variables for Built-in Types
  • Variables for built-in types (C and Java)
  • int x
  • x 5

X
X
5
102
Reference Variables(in Java)
  • Reference type variables
  • Button x
  • x new Button(click)

X
Button Object
click
X
103
Variables That hold Objects (in C)
  • Declaration of an object variable allocates space
    for the object
  • Button x(Click)

X
click
104
Pointers (in C)
  • Variables can be explicitly declared as pointers
    to objects
  • Button x
  • x new Button(click)

X
Button Object
click
X
105
Object Construction
  • Constructor
  • place where you include code that initializes the
    object
  • Default Constructor
  • no additional info required
  • User-defined Constructor
  • with parameters that specify values or sizes

106
Constructors in Java and C
  • In Java,
  • a constructor is invoked only through the new
    keyword
  • recall that all object variables are references
  • In C,
  • a constructor is called upon variable
    declaration, or explicitly through new with
    pointers, or in other situations
  • other types of constructors

107
C Control Over Copy and Assignment
  • In C, the semantics of a b (assignment)
    can be specified
  • by defining the copy-assignment operator
  • In C, there is a copy constructor
  • specifies what happens during object copying,
    e.g., when function parameters are passed
  • There is more low-level control
  • shallow copy vs deep copy

108
Methods
  • Defines object behavior
  • Static methods vs instance methods
  • Method overloading
  • within class, two methods with the same name but
    different signatures
  • Method overriding
  • same signatures across different classes
    (subclass and superclass)

109
Operators
  • In C, operators like , , , , etc. can be
    defined, just like methods
  • Example
  • class Matrix // ... Matrix
    operator(Matrix m) //
  • c a b // equiv to c a.operator(b)

110
Containers
  • Examples Lists, Stacks, Files, etc.
  • Structures that contain elements
  • Often, the elements type has little or nothing
    to do with the containers operations
  • Possible room for re-use
  • unified container code for a stack of integers, a
    stack of webpages, a stack of strings, ...

111
Java and the Object Hierarchy
  • All classes extend the Object class
  • A variable of class Object can refer to any Java
    object
  • Example
  • public class Stack Object A int top //
    void push(Object elt) // ...

112
C and Templates
  • Templates allow for a generic definition
  • parameterized definition, where the element type
    is the parameter
  • Example
  • templateltclass Tgtpublic class StackltTgt T
    AMAX int top void push(T element) //

113
GUI Programming
  • In Java, GUI is part of its development kit
  • java.awt. is a collection of classes that
    support visual programming and graphics
  • visual objects (buttons, textfields, etc), layout
    managers, events, etc.
  • In C
  • not part of the language
  • libraries dependent on platform (MFCs and Motif)

114
Object-OrientedDesign and the UML
115
Object-Oriented Modeling
  • UML Unified Modeling Language
  • Emerging OO Modeling Standard
  • What is depicted?
  • System functionality
  • Class details and static relationships
  • Object interaction
  • State transition within an object

116
Modeling Techniques
  • Use Cases/Use Case Diagrams
  • Class Diagrams
  • CRC Cards
  • Interaction Diagrams
  • State Diagrams

117
ExampleUse Case Diagram
LIBRARY SYSTEM
Facilitate Borrow
Search for Book
Borrower
Librarian
Facilitate Return
118
Class Diagramsand CRC Cards
  • Class Diagrams similar to ER Diagrams but in
    addition, it incorporates
  • methods, not just attributes for each entity
  • inheritance
  • Class-Responsibility-Collaboration Cards
  • technique that depicts responsibilities of
    classes with respect to other classes (hints on
    both data and behavior details)

119
ExampleInteraction Diagram
2 checkIfAvailable()
Borrow Screen
Book
1 checkIfDelinquent() 3 borrowBook()
4 setBorrower()
Borrower
120
ExampleState Diagram (Book)
start
Reserved
Borrowed
New
Librarian activates book as available
Borrower returns book
Available
121
Object-Oriented Design Models
  • Static Model
  • Class Diagrams
  • Dynamic Model
  • Use Cases, Interaction Diagrams, State Diagrams,
    others

122
OO Static Model
  • Class Diagrams
  • Relationships
  • Association
  • Aggregation/Composition
  • Inheritance
  • Attribute and Method names

123
Classes in a Class Diagram
  • Class name only Example
  • With Details Example

Bank Account
Class Name
Bank Acct double balance deposit() withdraw()
Class Name attributes methods
124
Relationships
  • Inheritance (arrow)
  • example between Secretary and Employee
  • Composition/Aggregation (diamond)
  • example between Car and Wheel
  • Association (line)
  • example between Borrower and Book

125
Inheritance
Employee
public class Secretary extends Employee
Secretary
126
Composition
Car
Wheel
4
w
public class Car Wheel w ...
public Wheel() w new Wheel4
...
127
Association
Borrower
Book
3
1
currBorr
bk
public class Borrower Book bk
public Borrower() bk new Book3
public class Book Borrower currBorr
128
OO Dynamic Model
  • Goal Represent
  • Object behavior
  • Object interaction
  • Traditional (relational) Dynamic Modeling
  • Data Flow Diagrams (DFDs)
  • Problem Processes separate from data
  • Need modeling notation that highlight tight
    relationship between data processes

129
DFD Example(Inventory Management)
Accept and Post Delivery
Delivery info
Transaction
Item Master
130
OO CounterpartObject Interaction
new (delivery info)
Encoder
Transaction
post (item count)
Item Master
131
Building anOO Dynamic Model
  • Identify use cases
  • Describe each use cases through an Interaction
    Diagram
  • Derive implied methods (and attributes)

132
Use Cases
  • What is a Use Case (Scenario) ?
  • Typical interaction between user and the system
  • Set of use cases lt-gt systems functions
  • Examples
  • word processor increase font size of text
    portion
  • ATM withdraw cash from savings account

133
Depicting Use Cases
  • Set of Use Cases for a system
  • Use Case Diagram
  • Describing a single Use Case
  • Text (narrative)
  • Describing object interaction for a single Use
    Case
  • Interaction Diagram

134
ExampleUse Case Diagram
LIBRARY SYSTEM
Facilitate Borrow
Search for Book
Borrower
Librarian
Facilitate Return
135
Example Use Case
  • Facilitate Borrow
  • Given a borrowers ID Card and the book to be
    borrowed, the librarian enters the borrowers ID
    number and the books catalogue number. If the
    borrower is allowed to borrow the book, the
    system displays that the book has been recorded
    as borrowed
  • Objects/Classes involved
  • Book, Borrower, Librarians Borrow Screen

136
Use Case Diagram Notation
  • Stick Figures - Actors
  • Ellipses - Use Cases
  • Links - association between actors and use cases
  • ltltusesgtgt and ltltextendsgtgt
  • between use cases

137
Interaction Diagram Example
2 checkIfAvailable()
Borrow Screen
Book
1 checkIfDelinquent() 3 borrowBook()
4 setBorrower()
Borrower
138
Interaction Diagrams
  • Rectangles Classes/Objects
  • Arrows Messages/Method Calls
  • Labels on Arrows
  • sequence number
  • method name
  • more details, when necessary (conditions,
    parameters, types, return types)

139
Methods
  • Interaction Diagrams suggest methods for classes
  • consequence on detailed class diagram
  • The label(s) of an arrow should be a method of
    the class the arrow points to
  • Library System
  • Borrower class should have at least two methods
    (checkIfDelinquent and borrowBook)
Write a Comment
User Comments (0)
About PowerShow.com