object-oriented analysis and design introduction tom perkins

27
Object-Oriented Analysis and Design Introduction Tom Perkins

Upload: dennis-glenn

Post on 03-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Analysis and Design Introduction Tom Perkins

Object-Oriented Analysis and Design

Introduction

Tom Perkins

Page 2: Object-Oriented Analysis and Design Introduction Tom Perkins

Pages: 634

Pub Date: November 2006

Publisher: O'Reilly

by Brett McLaughlin, Gary Pollice, David West

• Head First Object-Oriented Analysis and Design

Print ISBN-13: 978-0-59-600867-3

Print ISBN-10: 0-596-00867-8

Page 3: Object-Oriented Analysis and Design Introduction Tom Perkins

Head First Chapters

1. Great Software Begins Here2. Gathering Requirements3. Requirements Change4. Analysis5. Handling Change

1. Small Changes lead to Big Problems2. Incorporating Flexibility

6. Software Architecture7. Bringing Order to Chaos8. Design Principles9. Iterating and Testing10. An OOAD Software Process

Page 4: Object-Oriented Analysis and Design Introduction Tom Perkins

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• Major problem: Getting you involved!

Page 5: Object-Oriented Analysis and Design Introduction Tom Perkins

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

Page 6: Object-Oriented Analysis and Design Introduction Tom Perkins

Welcome to Objectville

“Foist, youse gotta speak da language!”

Page 7: Object-Oriented Analysis and Design Introduction Tom Perkins

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

Page 9: Object-Oriented Analysis and Design Introduction Tom Perkins

UML and Class Diagrams

Airplane

speed: int

getSpeed(): int

setSpeed(int)

Class Diagram

Member Variables

name:type

Name

Methods

name(parameters the method uses): return type

Page 10: Object-Oriented Analysis and Design Introduction Tom Perkins

Basic Airplane Class

VB.NETPublic Class Airplane Private mSpeed As Integer

Sub New() End Sub

Public Sub SetSpeed(ByVal value As Integer)

mSpeed = value End Sub Public Function GetSpeed() As Integer Return mSpeed End FunctionEnd Class

C#.NETusing System;using System.Collections.Generic;using System.Text;

namespace Objectville_Demo_CS{ public class Airplane { private int speed; public Airplane(){ // constructor } public virtual void setSpeed(int speed) { this.speed = speed; } public virtual int getSpeed() { return speed; } }}

Page 11: Object-Oriented Analysis and Design Introduction Tom Perkins

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

Page 12: Object-Oriented Analysis and Design Introduction Tom Perkins

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)

Page 13: Object-Oriented Analysis and Design Introduction Tom Perkins

InheritanceVB.NETPublic Class Jet Inherits Airplane

Private Const MULTIPLIER = 2 Public Sub New() MyBase.New() End Sub

Public Overrides Sub SetSpeed(ByVal value As Integer) MyBase.SetSpeed(value * MULTIPLIER) End Sub Public Sub Accelerate() MyBase.SetSpeed(GetSpeed() * 2) End SubEnd Class

C#.NET public class Jet:Airplane { private const int MULTIPLIER = 2;

public Jet() : base() { } public override void setSpeed(int speed) { base.setSpeed(speed * MULTIPLIER); } public void accelerate() { base.setSpeed(getSpeed() * 2); } }

Page 14: Object-Oriented Analysis and Design Introduction Tom Perkins

Base Class Modifications (.NET)

Public Class Airplane Private mSpeed As Integer

Sub New() End Sub

Public Overridable Sub SetSpeed( ByVal value As Integer) mSpeed = value End Sub Public Overridable Function GetSpeed() As Integer Return mSpeed End FunctionEnd Class

public class Airplane { private int speed; public Airplane() // constructor { } public virtual void setSpeed(int speed) { this.speed = speed; } public virtual int getSpeed() { return speed; } }

Page 15: Object-Oriented Analysis and Design Introduction Tom Perkins

Putting the classes to workStart with a biplane

Module Module1

Sub Main() FlyTest() End Sub Public Sub FlyTest() Dim biplane As New Airplane biplane.SetSpeed(212) Console.WriteLine( _biplane.GetSpeed()) Console.Read() End Sub

End Module

static void Main(string[] args) { FlyTest1(); } public static void FlyTest1() { Airplane biplane = new Airplane(); biplane.setSpeed(212); Console.WriteLine(biplane.getSpeed());

Console.ReadLine(); }

Page 16: Object-Oriented Analysis and Design Introduction Tom Perkins

Module Module1 ‘Puzzle 1 (VB)

Sub Main() FlyTest() End Sub Public Sub FlyTest() Dim biplane As New Airplane biplane.SetSpeed(___) Console.WriteLine(_________________)

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

212 Dim x = 0 boeing.GetSpeedbiplane.GetSpeed() biplane.SetSpeed boeing.GetSpeed biplane.Accelerate()biplane.GetSpeed 422 x < 5x < 4 boeing.SetSpeed x = 0 x < 3x = x + 1 424 boeing.Accelerate() x-- x = 1

212

844

1688

6752

13504

27008

1696

Code snippets:

Desired output:

Page 17: Object-Oriented Analysis and Design Introduction Tom Perkins

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(); }

212 boeing.getSpeed()biplane.getSpeed() 422 x = 0int x = 0; x < 4 boeing.accelerate() biplane.setSpeed x-- x = 1 x < 3boeing.getSpeed() boeing.accelerate() boeing.getSpeed() biplane.getSpeed() x++ 424 x < 5

Code Snippets:

212

844

1688

6752

13504

27008

1696

Desired output:

Page 19: Object-Oriented Analysis and Design Introduction Tom Perkins

DEMO

“Walkthru” of solution

Page 20: Object-Oriented Analysis and Design Introduction Tom Perkins

A “toonce” of Polymorphism

Airplane

speed: int

getSpeed(): int

setSpeed(int)

Jet

MULTIPLIER: int

accelerate()

“inherits from”

Airplane plane = new Airplane(); Airplane plane = new Airplane();

Airplane plane = new Jet();

Airplane plane = new Rocket();

Page 21: Object-Oriented Analysis and Design Introduction Tom Perkins

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

Page 22: Object-Oriented Analysis and Design Introduction Tom Perkins

Rick’s Guitar Shop ApplicationUML Class Diagrams

Guitar

serialNumber:string price: double builder:string type:string backWood:string topWood:string

getSerialNumber():stringgetPrice():doublesetPrice():doublegetBuilder():doublegetModel():stringgetType():doublegetBackWood():stringgetTopWood():string

Inventory

Guitars: List

addGuitar(string,double,string,string,string,string,string)

getGuitar(string):Guitar search(Guitar):GuitarinitializeInventory:printInventory()

Page 23: Object-Oriented Analysis and Design Introduction Tom Perkins

Main Application

Inv:Inventoryg:GuitarwhatErinLikes:Guitar

initializeInventory(Inventory)

Page 24: Object-Oriented Analysis and Design Introduction Tom Perkins

Demo

Walkthru Rick’s Guitar Shop Application

Page 25: Object-Oriented Analysis and Design Introduction Tom Perkins

Problems

• Search routine does not work

• Should use constants or enums instead of strings

• Search should return multiple guitars

• Architecture could use some restructuring

• What would you do first with Rick’s app?

Page 26: Object-Oriented Analysis and Design Introduction Tom Perkins

Assignment

• If your’re new to object oriented programming, read Appendix ii: Welcome to Objectville

• Work through exercises in Chapter 1

• Download Rick’s App from class site

• Modify it to:– Fix the search routine– Use enums instead of strings

Page 27: Object-Oriented Analysis and Design Introduction Tom Perkins

FINIS