ObjectOriented Analysis and Design Head First ObjectOriented Analysis
Object-Oriented Analysis and Design
• Head First Object-Oriented Analysis and Design by Brett Mc. Laughlin, Gary Pollice, David West Publisher: O'Reilly Pub Date: November 2006 Print ISBN-10: 0 -596 -00867 -8 Print ISBN-13: 978 -0 -59 -600867 -3 Pages: 634
Head First Chapters 1. 2. 3. 4. 5. Great Software Begins Here Gathering Requirements Change Analysis Handling Change 1. 2. 6. 7. 8. 9. 10. Small Changes lead to Big Problems Incorporating Flexibility 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 • 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 objectoriented 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
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 set set Inheritance UML Polymorphism Encapsulation
UML and Class Diagrams Class Diagram Airplane speed: int get. Speed(): int set. Speed(int) Methods name(parameters the method uses): return type Name Member Variables name: type
Basic Airplane Class VB. NET Public Class Airplane Private m. Speed As Integer Sub New() End Sub C#. NET using System; using System. Collections. Generic; using System. Text; namespace Objectville_Demo_CS { public class Airplane Public Sub Set. Speed(By. Val value As { Integer) private int speed; m. Speed = value public Airplane(){ // constructor End Sub } Public Function Get. Speed() As Integer public virtual void set. Speed(int speed) { Return m. Speed this. speed = speed; End Function } End Class public virtual int get. Speed() { 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 – My. Base)
Inheritance VB. NET Public Class Jet Inherits Airplane Private Const MULTIPLIER = 2 Public Sub New() My. Base. New() End Sub C#. NET public class Jet: Airplane { private const int MULTIPLIER = 2; public Jet() : base() { } public override void set. Speed(int Public Overrides Sub Set. Speed(By. Val speed) value As Integer) { My. Base. Set. Speed(value * base. set. Speed(speed * MULTIPLIER); End Sub } Public Sub Accelerate() public void accelerate() My. Base. Set. Speed(Get. Speed() * 2) { End Sub base. set. Speed(get. Speed() * 2); End Class } }
Base Class Modifications (. NET) Public Class Airplane Private m. Speed As Integer public class Airplane { private int speed; Sub New() public Airplane() // constructor End Sub { } Public Overridable Sub Set. Speed( public virtual void set. Speed(int speed) By. Val value As Integer) { m. Speed = value this. speed = speed; End Sub } Public Overridable Function Get. Speed() public virtual int get. Speed() As Integer { Return m. Speed return speed; End Function } End Class }
Putting the classes to work Start with a biplane Module 1 Sub Main() Fly. Test() End Sub Public Sub Fly. Test() Dim biplane As New Airplane biplane. Set. Speed(212) Console. Write. Line( _ biplane. Get. Speed()) Console. Read() End Sub End Module static void Main(string[] args) { Fly. Test 1(); } public static void Fly. Test 1() { Airplane biplane = new Airplane(); biplane. set. Speed(212); Console. Write. Line(biplane. get. Speed()); Console. Read. Line(); }
Module 1 ‘Puzzle 1 (VB) Sub Main() Fly. Test() End Sub Public Sub Fly. Test() Dim biplane As New Airplane biplane. Set. Speed(___) Console. Write. Line(_________) Code snippets: 212 Dim x = 0 boeing. Get. Speed biplane. Get. Speed() biplane. Set. Speed boeing. Get. Speed biplane. Accelerate() biplane. Get. Speed 422 x < 5 x < 4 boeing. Set. Speed x = 0 x < 3 x = x + 1 424 boeing. Accelerate() x-- x = 1 Dim boeing As New Jet boeing. Set. Speed(___) Console. Write. Line(_________) ______ While _____________ Console. Write. Line(________) If (________ > 5000) Then ________(________ * 2) Else _________ End If _____ End While Console. Write. Line(biplane. Get. Speed) Console. Read() End Sub End Module Desired output: 212 844 1688 6752 13504 27008 1696
static void Main(string[] args) // puzzle 1 C# { Fly. Test 1(); } public static void Fly. Test 1() { Airplane biplane = new Airplane(); biplane. set. Speed(___); Console. Write. Line(______); Jet boeing = new Jet(); boeing. set. Speed(___); Console. Write. Line(________); _____; while (____) { _________; Console. Write. Line(___________); if ( _______ > 5000) { _________(_______ * 2); } else { ___________; } Console. Write. Line(biplane. get. Speed()); Console. Read. Line(); } Code Snippets: 212 boeing. get. Speed() biplane. get. Speed() 422 x = 0 int x = 0; x < 4 boeing. accelerate() biplane. set. Speed x-- x = 1 x < 3 boeing. get. Speed() boeing. accelerate() boeing. get. Speed() biplane. get. Speed() x++ 424 x<5 Desired output: 212 844 1688 6752 13504 27008 1696
DEMO “Walkthru” of solution
A “toonce” of Polymorphism Airplane Jet speed: int MULTIPLIER: int get. Speed(): int accelerate() set. Speed(int) “inherits from” Airplane = new Airplane(); Airplane = new Jet(); Airplane = new Rocket();
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 Fly. Test 2 and Fly. Test 3
Rick’s Guitar Shop Application UML Class Diagrams Guitar serial. Number: string price: double builder: string type: string back. Wood: string top. Wood: string get. Serial. Number(): string get. Price(): double set. Price(): double get. Builder(): double get. Model(): string get. Type(): double get. Back. Wood(): string get. Top. Wood(): string Inventory Guitars: List add. Guitar(string, double, string, string) get. Guitar(string): Guitar search(Guitar): Guitar initialize. Inventory: print. Inventory()
Main Application Inv: Inventory g: Guitar what. Erin. Likes: Guitar initialize. Inventory(Inventory)
Demo Walkthru Rick’s 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 • What would you do first with Rick’s app?
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
- Slides: 25