Вы находитесь на странице: 1из 54

On the Stop Assignment

Our Company maintains a group of race cars. Our


cars use some of our new 8-cylinder engines and
new transmissions. Once the engines are
assembled, the pistons, carburetor, and plugs
cannot be swapped between engines due to changes
caused by the high temperatures.
We want to keep records of the performance
achieved by each engine in each car and each
transmission in combination with each engine.
Our drivers evaluate each car to give us their
assessment of the handling. We need a system to
track the configurations and the drivers
assessments.

Visibility Control

To restrict the access to certain


variables and methods outside the class
Visibility modifiers/access modifiers to
provide three types of visibility controls
1. Public
2. Private
3. Protected

Public Access
Access modifier = public
To make variables and methods visible to
all the classes outside
Has the widest possible visibility and
accessible everywhere.
In fact, this is what some times needed to
be prevented in many programs
Visible to all classes regardless of their
packages
E.g.
public int age;
public void CalAge() {}

Friendly Access

No access modifier (default)


Limited version of public accessibility
friendly level of access
Visible only in the same package
Not accessible to other packages
Package = group of related classes stored
separately

Protected Access
Access modifier = protected
Visibility level lies between public
access and friendly access
Visible to all classes and subclasses
in the same package
Visible to subclasses in other
packages
Non-subclasses in other packages
cannot access the protected
members

Private Access

Access modifier = private


Highest degree of protection
Accessible only within their own class
Cannot be inherited by subclasses
Therefore not accessible in subclasses
A method declared as private behaves like
a method declared as final
Prevent the method from being subclassed
Cannot override non-private method in
subclass to make it private

Private Protected Access


Declared with two keywords
Access modifier = private protected
E.g.
private protected int StudentNumber;

Visibility level lies between protected access


and private access
Visible in all subclasses regardless of what
package they are in
Cannot accessible by other classes in the same
packages

Visibility Levels
Access Public
Modifier

Protected

Friendly
(Default)

Private
Protected

Private

Access
Location
Same Class

Yes

Yes

Yes

Yes

Yes

Subclasses in same
package

Yes

Yes

Yes

Yes

No

Other Classes in same


package

Yes

Yes

Yes

No

No

Subclasses in other
packages

Yes

Yes

No

Yes

No

Non-subclasses in other
packages

Yes

No

No

No

No

Rules of Thumb
1. To be visible everywhere use public
2. To be visible everywhere in the current
package and also subclasses in other
packages use protected
3. To be visible everywhere in the current
package use friendly (default)
4. To be visible only in subclasses
regardless of packages use private
protected
5. To be visible only in own class use
private

Representing objects
squares with object type, optionally
preceded by object name and colon
write object's name if it clarifies the diagram
object's "life line" represented by dashed vert.
line

Messages between objects


message (method call) indicated by horizontal arrow
to other object
write message name and arguments above arrow

dashed arrow back indicates return


different arrowheads for normal / concurrent (asynchronous)
methods

Lifetime of objects
creation: arrow with 'new'
written above it
notice that an object
created after the start of the
scenario appears lower
than the others

deletion: an X at bottom of
object's lifeline
Java doesn't explicitly
delete objects; they fall out
of scope and are garbagecollected

Indicating method calls


activation: thick box over object's life line;
drawn when object's method is on the stack
either that object is running its code, or it is on the
stack waiting for another object's method to finish
nest to indicate recursion
Activation

Nesting

Indicating selection and loops


frame: box around part of a sequence diagram to
indicate selection or loop
if -> (opt) [condition]
if/else -> (alt) [condition], separated by horiz. dashed line
loop -> (loop) [condition or items to loop over]
opt

[balance
<>0]

alt

[balance
< 100.00]

[balance>= 100.00]

loop

[balance
< 0]

linking sequence diagrams


if one sequence diagram is too large or refers to
another diagram, indicate it with either:
an unfinished arrow and comment
a "ref" frame that names the other diagram
when would this occur in our system?

Customer Info

ref

Verify customer credit


Approved?

Example sequence diagram

Object
Collaboration

Business
Process

(Activity
Diagrams)
(Interaction
Diagrams)

Objects Behavior
(State Diagrams)
System Functionality

Use-case Diagrams