Title: Review ObjectOriented Programming
1Review - Object-Oriented Programming
- We describe and understand the world around us
by - Identifying objects in our world
- Understanding what the objects can do
- Interacting with objects to accomplish a task
- Encapsulation
- Information confined or hidden inside an object
- Interact with objects via external properties
- Objects with common attributes grouped together
in classes
2Review - Object-Oriented Programming
- An object is an entity that has state, behavior,
and identity - State information the object needs to remember
- Property
- Current values of properties
- Behavior objects reaction to state changes,
interactions with other objects - Methods model object behavior
- Methods are invoked
3A Western Town the base concept
The first thing that Eunice needs is a setting.
So she pulls down a binder she has marked
"Western Towns", opens to the first page, and
reads the first few lines
Every Western town has a few key ingredients
stables, saloons, sheriffs, and a couple
troublemakers. A standard town would have three
stables and be located West of the Mississippi
sometime around 1850.
- This description
- Establishes the key ingredients of a Western
town. - Gives default values that may occur in a typical
town.
4A Western Town - pseudocode
WesternTown has a certain number of stables
has a certain number of saloons has a certain
number of sheriffs has a certain number of
troublemakers is located somewhere exists at
a certain time  a typical WesternTown would
have number of stables 3 location
Western America time period 1850
- First line Eunice is defining a Western town.
- Then she declares what sorts of variables (those
things which define how the town looks) that a
class of Western towns might have. - Then she constructs a sample town, with default
values for her variables.
5Eunices New Book - The Main Plot
The Gunfight at the Old West Saloon This story
takes place in a Western town called Sweaty Post.
Sweaty Post has one sheriff, two saloons, and
five troublemakers.
Main Gunfight Western Town sweatyPost is a new
Western Town the number of saloons in
sweatyPost is two the number of sheriffs is
one the number of troublemakers is five
6Humans the base concept
Now for the villain Eunice picks up another
binder. This is marked Humans. This is what she
finds in the introduction
"All humans start out with two legs, two arms,
two eyes, a nose, a mouth and a hair color. They
are either male or female, have a name, have a
hair color, and have different preferences in
whiskey. If someone asks, humans can respond with
their name or their hair color."
- Notice Eunice likes to make all body parts
variables ! - The sex, name, hair color and whiskey preference
will be defined later in the plot.
7Humans - pseudocode
Human has a certain number of legs has a
certain number of arms has a certain number of
eyes has a certain number of noses has a
certain number of mouths has a name has a
certain sex has a hair color has a strong
preference in whiskey A standard human would
start with two legs two arms two eyes
one nose one mouth When someone asks for
your name Tell them your name When someone
asks for your hairColor Tell them your
hairColor
- Eunice's human has some interactivity. She can
ask its name or its hair color, and it will
respond. This is called a method and is your key
to a good time. We'll get to it later.
8The Villain! (a review)
- Villains are based on the idea of humans. They
are identical, except that they have some
additional qualities, namely a moustache, a hat,
a certain "look", some level of drunkenness, and
a certain quantity of damsels in their
possession. Your standard villain will look mean,
start the day out sober, and not yet have
captured any damsels. - Whenever the main plot says that a villain drinks
whiskey, his level of drunkedness will go up by
one. - If someone asks a villain how drunk he is, the
villain will always respond with his level of
drunkenness. - If the villain is supposed to tie up a damsel,
tie up the specified damsel, then add one to the
number of damsels he has tied up. Then print out
" Oh my gosh! (our villain) with (moustache
kind), has tied up (the specified damsel), the
pretty damsel with (her hair color) hair!.
9The Villain psuedocode part 2
Villain extends the idea of Humans. Every
villain has a moustache Every villain has a hat
Every villain has a "look" Every villain will
have some level of drunkenness Every villain
will tie up a certain number of damsels For a
given Villain, He is a male He will look
mean He will start out sober He will start
the day without having any damsels tied up
drinkWhiskey drunkedness increases by
one howDrunkAmI tell them how drunk I
am tieUpDamsel (a damsel) add one to the
number of damsels this villain has tied
up print "Oh my gosh! (our villain) with
(moustache kind), has tied up (the specified
damsel), the pretty damsel with (her hair
color) hair! "
10The Main Plot
All this work with drinking and tieing up damsels
is making Eunice feel a bit run down, so she
decides to stop working on her characters for a
little while, and to work a bit on the main plot.
Accordingly, she sets her pen to the sheet of
paper labeled main routine
Here is the main plot of the Gunfight at the Old
West Saloon There is a Western Town called
Sweaty Post. Sweaty Post has two saloons, one
sheriff, and five troublemakers. There is a male
villain named Maurice. Maurice has a black hat
and a red moustache. Maurice prefers Jack Daniels
whiskey. Mary is a female human. She is blonde.
She prefers her whiskey straight. In our story,
Maurice starts out by drinking whiskey. He then
lets everyone know how drunk he is, and then ties
up a woman named Mary.
11The Main Plot pseudocode
Here is the main plot of Gunfight at the Old West
Saloon In the novel Gunfight There is a town
called sweatyPost sweatyPost has two saloons
sweatyPost has one sheriff sweatyPost has
five troublemakers  There is a new villain
identified as villainOne villainOne name is
Maurice villainOne has a black hat
villainOne has a red moustache villainOne
prefers Jack Daniels whiskey  There is a new
Human identified as damselOne damselOne is
Mary damselOne is female damselOne is blonde
damselOne prefers her whiskey straight
 villainOne drinks some whiskey villainOne
tells us how drunk he is villainOne ties up a
damsel named Mary
12Object-Oriented Design
- Requirements phase of software life cycle
- User needs identified and documented
- Requirements document describes behavior of
proposed software from users perspective - Focus on what to do, not how to do it
- (The green in our prior slides)
- Specification phase
- Define explicit behavior of program
- Specification document identifies specific
outputs for all possible inputs - Specific system requirements documented
- (Yellow in prior slides approximates this, but it
is not really represented in prior slides)
13Object-Oriented Design (continued)
- Design phase
- Uses requirements and specification documents
- Identifies and defines classes, behaviors of each
class, and interactions - Implementation phase
- Code written
- Design document is a guideline
- Design phase is one of the most difficult
- Design allows programmers to develop software
that is efficient, easy to build and maintain
14Finding Classes
- Primary goals of design phase
- Identify classes
- Determine relationships between classes
- Decisions require talent and experience
- Majority of classes identified in requirements
and specification documents - Nouns in requirements document provide list of
potential classes - List must be refined
15Unified Modeling Language (UML)
- Software design must be clearly, concisely,
correctly described to programmers - Unified Modeling Language (UML) a graphical
language - Visually expresses software system design
- Represents a design in standard format
- UML provides five views, nine diagram types
- View diagrams that highlight a system aspect
- UML has no rigid rules for what is included
16Unified Modeling Language (UML) (continued)
17Class Diagrams
- Describe static structure of a system in terms of
classes and their relationships - Class drawn as a rectangle divided into three
compartments - Class names appear in bold text, centered at the
top of the rectangle - Compartments describing state and behavior are
optional - Method type information also optional
- Names of routines that take parameters must be
followed by parentheses
18Class Diagrams (continued)
19Class Diagrams (continued)
20Class Diagrams (continued)
- Specify parameter and return types with colon
notation - Include only as much information as necessary
- Most class diagrams do not include accessors or
mutators - Also omit void return values
- Relationship between classes defined
- One instance invokes a method or accesses the
state of an instance of another
21Your Turn
- Recall our story about Eunice from last time?
- What would the class diagram for WesternTown look
like?
22Class Diagrams (continued)
- UML defines relationships that describe how
classes are related in a class diagram - Association
- State of one class contains a reference to an
instance of another class - Permanent structural relationship
- Solid line drawn between two classes to represent
association - Not always bidirectional
- Navigability arrow indicates direction of a
relationship
23Class Diagrams (continued)
- Multiplicity several instances of a class
- Denoted with an asterisk and a number
- Number indicates the number of class instances
- Dependency change in one class affects another
class that uses it - Inherently one-way relationship
- Association usually implemented as part of the
class state - Dependency usually implemented as local variable,
parameter, return value - Called automatic variables
24Class Diagrams (continued)
25Class Diagrams (continued)
- Dependency
- Represented as a dashed line
- Arrow points to independent element
- Generalization
- Superclass is a generalization of its subclass
- Denoted by a triangle connecting a subclass to
its parent class - Associations and generalizations represented in a
single diagram
26Class Diagrams (continued)
27Sequence Diagrams
- Sequence diagrams describe how groups of objects
dynamically collaborate - Single sequence diagram describes single behavior
- Time flows from bottom to top
- Vertical lines denote lifetime of an object
- Each line labeled with object name followed by
name of class from which object instantiated - Colon separates object name from class name
- Class name always given, object name optional
- Class name preceded by colon
28Sequence Diagrams (continued)
29Your Turn
- Recall our story about Eunice from last time?
- What would the class diagram for WesternTown look
like? - What would the dependency/sequence diagram for
the whole project look like?
30Sequence Diagrams (continued)
31Sequence Diagrams (continued)
- Illustrates sequencing of events
- Does not show algorithms
- Shows order in which messages are sent
- Each class described in a separate class diagram
- If sequence diagram shows message passing,
corresponding class diagram must agree - Separate sequence diagram for each major behavior
in system being modeled - Includes behaviors central to system understanding
32Summary
- Object-oriented design think in terms of
encapsulated entities, external behaviors - Internal behavior may change
- Does not affect users program
- Encapsulation and inheritance important
characteristics - UML is an industrial standard to express system
design