Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Good software.
CS 151: Object-Oriented Design August 22 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.
CS 211 Inheritance AAA.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Software Engineering and Design Principles Chapter 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Computer Science 240 Principles of Software Design.
Object Oriented Software Development
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Chapter 8 More Object Concepts
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Computer Science 240 © Ken Rodham 2006 Principles of Software Design.
Object Oriented Analysis and Design Chapter 4: Analysis.
Design Patterns. Patterns “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution.
Refactoring Improving the structure of existing code Refactoring1.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
2. GATHERING REQUIREMENTS Object-Oriented Analysis and Design NTPCUG.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Object-Oriented Analysis and Design Introduction Tom Perkins.
Refactoring1 Improving the structure of existing code.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Requirements Change! Chapter 3 Object-Oriented Analysis and Design Tom Perkins.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 151: Object-Oriented Design August 27 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Nothing Stays the Same Chapter 5 HeadFirst OOAD Tom Perkins - NTPCUG.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
12 OBJECT-ORIENTED DESIGN CHAPTER
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Refactoring1 Improving the structure of existing code.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Written by: Dr. JJ Shepherd
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Object- oriented Design Principles
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
MPCS – Advanced java Programming
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
About the Presentations
Un</br>able’s MySecretSecrets
Strategy Design Pattern
Objects First with Java
CMPE 135: Object-Oriented Analysis and Design August 23 Class Meeting
Improving the structure of existing code
CMPE 135: Object-Oriented Analysis and Design August 30 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Introduction to Design Patterns
CMPE 135: Object-Oriented Analysis and Design February 5 Class Meeting
Software Design Lecture : 39.
HFOOAD Chapter 5 Interlude
CMPE 135 Object-Oriented Analysis and Design August 22 Class Meeting
Presentation transcript:

Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series

Welcome! 10 (+) week book study Tom Perkins Books available at Nerdbooks.com

Session Objectives Awareness of functional OO app Identify 3 steps to great software design Use enums vs string comparisons Use encapsulation to protect program parts Use delegation to achieve loose coupling Be aware of what makes a program functional, maintainable, reusable, and flexible

Rick’s Guitar Store App Developed by “Down and Dirty Coding, Inc” Inventory management system Search tool – match a customer with their dream instrument Handout (C# and VB) Available on class website

Rick’s Guitar Shop Application UML Class Diagrams Guitar serialNumber:string price: double builder:string type:string backWood:string topWood:string getSerialNumber():string getPrice():double setPrice():double getBuilder():double getModel():string getType():double getBackWood():string getTopWood():string Inventory Guitars: List addGuitar(string,double,string,string, string,string,string) getGuitar(string):Guitar search(Guitar):Guitar initializeInventory: printInventory()

Main Application Inv:Inventory g:Guitar whatErinLikes:Guitar initializeInventory(Inventory)

DEMO ricksGuitar_start

Nice app, but it has a few problems … Doesn’t work (functionality) Doesn’t give client choices Heavy use of string comparisons Dependencies –Inventory class must know internals of Guitar class –Maybe needs some architecture re-do WHERE TO START????

3 Steps to Great Software 1.Solve the customer’s problem (do what it is supposed to do) 2.Apply OO principles (remove duplicate code, OO techniques, etc) 3.Strive for maintainable, reusable design (patterns, refactoring)

Step 1 – get it to do what it should be doing Problem: search doesn’t return a hit (never, ever) What’s the problem? Possible solutions: –Use lowercase comparisons –Use enums instead of strings –(some problem fix, some improved design – using enums improves design and fixes problem)

Enums Enumerations are user-defined sets of integral constants that correspond to a set of friendly names. Using enumerations makes your code easier to read, easier to debug, and less prone to errors caused by typographical errors. –MS eLearning series – Windows-based Applications with C# and VB, chapter 3, lesson 2

Enums enum Wood { Indian_Rosewood, Brazilian_Rosewood, Mahogany, Maple, Cocobolo, Cedar, Adirondack, Alder, Sitka }

Enums – code GuitarEnums class InitalizeInventory method Note Intellisense -> fewer errors

Enums - Code To describe Erin’s dream guitar: Guitar whatErinLikes = new Guitar("", 0, Builder.Fender, "Stratocastor", GuitarType.Electric, Wood.Alder, Wood.Alder);

Step 1.b – Give customers choices Search should return a list of guitars instead of a single guitar LinkedList matchingGuitars = inv.search(whatErinLikes); Old: New: Guitar guitar = inv.search(whatErinLikes); See full code in ricksGuitars_choices

Step 2 – Apply some OO Principles Encapsulation –Break your apps/objects into logical parts –Keep those parts separate –Apples in apple boxes, oranges in orange boxes –Code Cohesion – group like things together Groups (blocks of code, objects) should have a single purpose

Good Object Design Well-designed objects  only one purpose Name of object should tell what it does –Jet object takeOff(), land() methods OK takeTicket() method not OK Object should represent only one concept –Duck object – should not represent quacker, rubber duckie, and getting out of the way of a foul ball Unused values (null properties) are a dead giveaway

Encapsulation Candidate – Guitar object as used in Search Method Guitar whatErinLikes = new Guitar("", 0, Builder.Fender,"Stratocastor", GuitarType.Electric, Wood.Alder, Wood.Alder); Unused variables Clients never enter a serial number or price Only guitar’s general properties used –Builder, topWood, etc

Solution: Break object into separate parts and keep those parts separate Encapsulation Create a separate GuitarSpec object

Guitar  GuitarSpec Guitar serialNumber:string price: double builder:string model:string type:string backWood:string topWood:string spec:GuitarSpec getSerialNumber():string getPrice():double setPrice():double getBuilder():double getModel():string getType():double getBackWood():string getTopWood():string getSpec:GuitarSpec GuitarSpec builder:string model:string type:string backWood:string topWood:string getBuilder():double getModel():string getType():double getBackWood():string getTopWood():string

Encapsulation Code Changes Guitar whatErinLikes = new Guitar("", 0, Builder.Fender, "Stratocastor", GuitarType.Electric,Wood.Alder, Wood.Alder); LinkedList matchingGuitars = inv.search(whatErinLikes); Search now using a GuitarSpec object: Old way New way GuitarSpec whatErinLikes = new GuitarSpec( Builder.Fender, "Stratocastor", GuitarType.Electric, Wood.Alder, Wood.Alder); LinkedList matchingGuitars = inv.search(whatErinLikes);

Encapsulation Code Changes Guitar class contains a GuitarSpec object – composition Design principle – Favor composition over inheritance Use “has-a” instead of “is-a” class Guitar { private string serialNumber; private double price; private GuitarSpec spec; public Guitar(string serialNumber, double price, Builder builder, string model, GuitarType type, Wood backWood, Wood topWood) { this.serialNumber = serialNumber; this.price = price; this.spec = new GuitarSpec(builder, model, type, backWood, topWood); … private variables constructor

Encapsulation Code Changes foreach (Guitar g in guitars) { GuitarSpec guitarSpec = g.getSpec(); if (!searchSpec.getBuilder().Equals( guitarSpec.getBuilder()) ) continue; … Inventory search routine now uses GuitarSpec for comparisons:

Additional benefit of Encapsulation Suppose Rick wants to start carrying both 6-string and 12-string guitars –Add a numStrings field to GuitarSpec only –Won’t need to change Guitar class Encapsulation – change one part of your app without changing other parts What to encapsulate – Isolate the parts of your app that might vary from the parts that will remain the same

What we’ve done OO Principles applied: –Functionality – does what it is supposed to do –Encapsulation – separated parts that might vary from parts that remain the same –Flexibility – made code easier to change, change is inevitable Yet to come (tune in next week …) –Polymorphism and Inheritance See: ricksGuitars_encapsulation

Step 3 – Make code reusable and extendable Rick wants to offer both 6-string and 12- string guitars Plan: 1.Add numStrings property and getNumStrings() method to GuitarSpec 2.Encapsulate GuitarSpec in Guitar class constructor 3.Delegate comparison of search object to GuitarSpec class – no longer Inventory’s job 4.Update Tester class and test everything

2. Encapsulate GuitarSpec parameter in Guitar constructor public Guitar(string serialNumber, double price, Builder builder, string model, GuitarType type, Wood backWood, Wood topWood) { this.serialNumber = serialNumber; this.price = price; this.spec = new GuitarSpec(builder, model, type, backWood, topWood); } Old: New: public Guitar(string serialNumber, double price, GuitarSpec spec) { this.serialNumber = serialNumber; this.price = price; this.spec = spec; }

New constructor with encapsulated GuitarSpec Create Guitar objects for Inventory static void InitializeInventory(Inventory inv) { inv.addGuitar("11277", , new GuitarSpec(Builder.Collings, "CJ", GuitarType.Acoustic,6, Wood.Indian_Rosewood, Wood.Sitka)); inv.addGuitar("V95693", , new GuitarSpec(Builder.Fender, "Stratocastor", GuitarType.Electric,6, Wood.Alder, Wood.Alder));

3. Delegation Delegation – let someone else do it Inventory search method – now has to know the insides of the GuitarSpec object to do its comparison Delegate this comparison to the GuitarSpec object –“Does this other GuitarSpec object match you?”

Modified GuitarSpec object – match method public Boolean matches(GuitarSpec otherSpec) { if (builder != otherSpec.builder) return false; if ((model != null) && (!model.Equals("")) && (!model.Equals(otherSpec.model))) return false; if (type != otherSpec.type) return false; if (numStrings != otherSpec.numStrings) return false; if (backWood != otherSpec.backWood) return false; if (topWood != otherSpec.topWood) return false; return true; }

Using the match method foreach (Guitar g in guitars) { if (g.getSpec().matches(searchSpec)) matchingGuitars.AddLast(g); } return matchingGuitars; Search method in Inventory class: See: ricksGuitars_final for code

Review (and that rhymes with Whew!) We’ve looked at –Enums –Encapsulation –Delegation –Code Reuse To write good software: –Do what the customer wants –Apply OO principles –Design and code for flexibility, maintainability, and reuse

Assignment – Chapter 2 Get the book! Slow down! Make yourself comfortable! (Take your pants off!) Do the exercises Read “No Dumb Questions” Drink lots of water Talk about what you’ve learned Get involved with the process Use what you’ve learned at least one time this week