Object-Oriented Analysis and Design: Tom Perkins
Object-Oriented Analysis and Design: Tom Perkins
Design
Introduction
Tom Perkins
• Head First Object-Oriented Analysis
and Design
Publisher: O'Reilly
Pages: 634
Head First Chapters
1. Great Software Begins Here
2. Gathering Requirements
3. Requirements Change
4. Analysis
5. Handling Change
1. Small Changes lead to Big Problems
2. Incorporating Flexibility
6. Software Architecture
7. Bringing Order to Chaos
8. Design Principles
9. Iterating and Testing
10. An OOAD Software Process
Obstacles
• Not the “perfect” Introduction to Object
Orientation book
• Examples in Java
– Rework in VB.NET or C#.NET
• Approach too “Juvenile”
– Maybe
– Incorporates many different features drawn from
learning theory and cognitive psychology
• Not a course for “object oriented gurus”
• Yet assumes some familiarity with object-
oriented coding
Approach
• Stick close to the book
• As much code as possible
• Walkthru’s
• Get your hands dirty, become involved
• Do the book exercises
• Work with someone where possible
• Participate in class discussions
• Team Learning Experiments
Welcome to Objectville
set
set
set
set
Inheritance
UML Polymorphism
Encapsulation
UML and Class Diagrams
Class Diagram
Airplane Name
speed: int Member Variables
getSpeed(): int name:type
setSpeed(int)
Methods
name(parameters the method uses): return type
Basic Airplane Class
C#.NET
VB.NET
using System;
Public Class Airplane
using System.Collections.Generic;
Private mSpeed As Integer
using System.Text;
Sub New()
namespace Objectville_Demo_CS
End Sub
{
public class Airplane
Public Sub SetSpeed(ByVal value As
{
Integer)
private int speed;
mSpeed = value
public Airplane(){ // constructor
End Sub
}
Public Function GetSpeed() As Integer
public virtual void setSpeed(int speed) {
Return mSpeed
this.speed = speed;
End Function
}
End Class
public virtual int getSpeed() {
return speed;
}
}
What doesn’t the Class Diagram
give you?
• Variable scope – public, private, friend, etc
– Full-blown UML does
– Not usually needed
• Information about the constructor method
for the class
• Details of what each method does
Inheritance
• Once class inherits behavior from another
class
• Code keywords
– VB: Inherits
– C# uses : (colon) to indicate inheritance
• “Derived” class, subclass
• “Base” class, superclass (vb – MyBase)
Inheritance
VB.NET C#.NET
Public Class Jet public class Jet:Airplane
Inherits Airplane {
private const int MULTIPLIER = 2;
Private Const MULTIPLIER = 2
Public Sub New() public Jet() : base()
MyBase.New() {
End Sub }
public override void setSpeed(int
Public Overrides Sub SetSpeed(ByVal speed)
value As Integer) {
MyBase.SetSpeed(value * base.setSpeed(speed *
MULTIPLIER) MULTIPLIER);
End Sub }
Public Sub Accelerate() public void accelerate()
MyBase.SetSpeed(GetSpeed() * 2) {
End Sub base.setSpeed(getSpeed() * 2);
End Class }
}
Base Class Modifications
Public Class Airplane
(.NET)
public class Airplane
Private mSpeed As Integer {
private int speed;
Sub New() public Airplane() // constructor
End Sub {
}
Public Overridable Sub public virtual void setSpeed(int speed)
SetSpeed( ByVal value As Integer) {
mSpeed = value this.speed = speed;
End Sub }
Public Overridable Function GetSpeed() public virtual int getSpeed()
As Integer {
Return mSpeed return speed;
End Function }
End Class }
Putting the classes to work
Start with a biplane
Module Module1 static void Main(string[] args)
{
Sub Main() FlyTest1();
FlyTest() }
End Sub public static void FlyTest1()
Public Sub FlyTest() {
Dim biplane As New Airplane Airplane biplane = new Airplane();
biplane.SetSpeed(212) biplane.setSpeed(212);
Console.WriteLine( _ Console.WriteLine(biplane.getSpeed());
biplane.GetSpeed()) Console.ReadLine();
Console.Read() }
End Sub
End Module
Module Module1 ‘Puzzle 1 (VB)
Code snippets:
Sub Main()
FlyTest()
212 Dim x = 0 boeing.GetSpeed
End Sub
Public Sub FlyTest() biplane.GetSpeed() biplane.SetSpeed
Dim biplane As New Airplane boeing.GetSpeed biplane.Accelerate()
biplane.SetSpeed(___) biplane.GetSpeed 422 x < 5
Console.WriteLine(_________________)
x < 4 boeing.SetSpeed x = 0 x < 3
Dim boeing As New Jet x = x + 1 424
boeing.SetSpeed(___) boeing.Accelerate() x-- x = 1
Console.WriteLine(_________________)
______
While _______ Desired output:
___________________
Console.WriteLine(________________) 212
If (________________ > 5000) Then
________________(________________ * 2)
844
Else
_________________
1688
End If
_________
6752
End While
13504
Console.WriteLine(biplane.GetSpeed)
Console.Read() 27008
End Sub
1696
End Module
static void Main(string[] args) // puzzle 1 C#
{
FlyTest1();
} Code Snippets:
public static void FlyTest1()
{
Airplane biplane = new Airplane();
212 boeing.getSpeed()
biplane.setSpeed(___); biplane.getSpeed() 422 x = 0
Console.WriteLine(____________); int x = 0; x < 4 boeing.accelerate()
Jet boeing = new Jet(); biplane.setSpeed x-- x = 1 x < 3
boeing.setSpeed(___);
Console.WriteLine(________________); boeing.getSpeed() boeing.accelerate()
_________; boeing.getSpeed() biplane.getSpeed()
while (________) x++ 424 x<5
{
_________________;
Console.WriteLine(_____________________);
Desired output:
if ( ______________ > 5000) 212
{
__________________(______________ * 2); 844
}
else 1688
{
______________________; 6752
}
___________; 13504
}
Console.WriteLine(biplane.getSpeed()); 27008
Console.ReadLine();
} 1696
DEMO
“Walkthru” of solution
A “toonce” of Polymorphism
Airplane Jet
speed: int MULTIPLIER: int
getSpeed(): int accelerate()
setSpeed(int)
“inherits from”