Object-Oriented Analysis and Design: Tom Perkins
Object-Oriented Analysis and Design: Tom Perkins
Object-Oriented Analysis and Design: Tom Perkins
by Brett McLaughlin, Gary Pollice, David West Publisher: O'Reilly Pub Date: November 2006 Print ISBN-10: 0-596-00867-8
Software Architecture Bringing Order to Chaos Design Principles Iterating and Testing An OOAD Software Process
Obstacles
Not the perfect Introduction to Object Orientation book Examples in Java
Rework in VB.NET or C#.NET
Not a course for object oriented gurus Yet assumes some familiarity with objectoriented coding
Approach
Stick close to the book As much code as possible Walkthrus 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
Foist, youse gotta speak da language!
Learning Objectives
Be able to read and interpret a UML class diagram Be able to write a simple class in VB.NET or C#.Net Be able to write a class that inherits from another class, changes some behavior and adds some behavior to the base class Be able to define polymorphism Be able to identify what encapsulation is and why it is important
Points of Interest
set
Encapsulation
namespace Objectville_Demo_CS { 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; } } }
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
Inheritance
VB.NET Public Class Jet Inherits Airplane Private Const MULTIPLIER = 2 Public Sub New() MyBase.New() End Sub C#.NET public class Jet:Airplane { private const int MULTIPLIER = 2;
public Jet() : base() { } 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 } }
Module Module1 Puzzle 1 (VB) Sub Main() FlyTest() End Sub Public Sub FlyTest() Dim biplane As New Airplane biplane.SetSpeed(___) Console.WriteLine(_________________)
Code snippets: 212 Dim x = 0 boeing.GetSpeed biplane.GetSpeed() biplane.SetSpeed boeing.GetSpeed biplane.Accelerate() biplane.GetSpeed 422 x < 5 x < 4 boeing.SetSpeed x = 0 x < 3 x = x + 1 424 boeing.Accelerate() x-- x = 1 Desired output: 212 844 1688 6752 13504 27008 1696
Dim boeing As New Jet boeing.SetSpeed(___) Console.WriteLine(_________________) ______ While _______ ___________________ Console.WriteLine(________________) If (________________ > 5000) Then ________________(________________ * 2) Else _________________ End If _________ End While Console.WriteLine(biplane.GetSpeed) Console.Read() End Sub End Module
static void Main(string[] args) // puzzle 1 C# { FlyTest1(); } public static void FlyTest1() { Airplane biplane = new Airplane(); biplane.setSpeed(___); Console.WriteLine(____________); Jet boeing = new Jet(); boeing.setSpeed(___); Console.WriteLine(________________); _________; while (________) { _________________; Console.WriteLine(_____________________); if ( ______________ > 5000) { __________________(______________ * 2); } else { ______________________; } ___________; } Console.WriteLine(biplane.getSpeed()); Console.ReadLine(); }
Code Snippets: 212 boeing.getSpeed() biplane.getSpeed() 422 x = 0 int x = 0; x < 4 boeing.accelerate() biplane.setSpeed x-- x = 1 x < 3 boeing.getSpeed() boeing.accelerate() boeing.getSpeed() biplane.getSpeed() x++ 424 x<5
6752
13504 27008 1696
DEMO
Walkthru of solution
A toonce of Polymorphism
Airplane speed: int getSpeed(): int setSpeed(int) inherits from Airplane plane = new Airplane(); Airplane plane = new Airplane(); Airplane plane = new Jet(); Airplane plane = new Rocket(); Jet MULTIPLIER: int accelerate()
Encapsulation Escapades
Hiding information from the rest of your application Hide the details within the object; use Public sparingly Demo change the internal speed variable from private to public
Run FlyTest2 and FlyTest3
Inventory
Demo
Walkthru Ricks Guitar Shop Application
Problems
Search routine does not work Should use constants or enums instead of strings Search should return multiple guitars Architecture could use some restructuring
Assignment
If yourre new to object oriented programming, read Appendix ii: Welcome to Objectville Work through exercises in Chapter 1 Download Ricks App from class site Modify it to:
Fix the search routine Use enums instead of strings