View
213
Download
0
Tags:
Embed Size (px)
Citation preview
Why design?Why design?
• Every program is designedEvery program is designed– Either by intent or by defaultEither by intent or by default
• Two programs that do the same thingTwo programs that do the same thing– May have big differences inMay have big differences in
• ComplexityComplexity
• SpeedSpeed
• EfficiencyEfficiency
Why design?Why design?
• Every time you write a program, big Every time you write a program, big or small, you are a designer.or small, you are a designer.– You select variables, names, scope, You select variables, names, scope,
choose to use functions, objects, choose to use functions, objects, interface, etc.interface, etc.
• There are good and bad ways to There are good and bad ways to design programsdesign programs
Software design goalsSoftware design goals• UsabilityUsability
– it solves problems people want to solveit solves problems people want to solve
• ReliabilityReliability– It solves them correctly, without crashingIt solves them correctly, without crashing
• MaintainabilityMaintainability– Problems can be easily correctedProblems can be easily corrected
• ReusabilityReusability– Reduces cost, increases reliabilityReduces cost, increases reliability
Main approaches to software Main approaches to software designdesign
• Top-down design (TDD)Top-down design (TDD)– Breaks problems down into smaller Breaks problems down into smaller
onesones
• Object-oriented design (OOD)Object-oriented design (OOD)– Breaks problems down into objectsBreaks problems down into objects
Top-Down Design (TDD)Top-Down Design (TDD)
• Historically the most common formHistorically the most common form
• StagesStages– Problem statementProblem statement
– DecompositionDecomposition• Structure chartStructure chart
– RefinementRefinement• Separate algorithms for each moduleSeparate algorithms for each module
Example: Video rental Example: Video rental systemsystem
• Build a software system to support the operation of a Build a software system to support the operation of a video rental system. video rental system.
• The system should automate the process of renting The system should automate the process of renting tapes and receiving returned tapes, including tapes and receiving returned tapes, including calculating and printing patron bills, which may or may calculating and printing patron bills, which may or may not be done at the same time the tape is returned. not be done at the same time the tape is returned.
• It must also give the clerk access to information about It must also give the clerk access to information about the tapes, such as the number of copies on the shelf of the tapes, such as the number of copies on the shelf of any given video owned by the store. any given video owned by the store.
• It must be able to add new customers and tapes to and It must be able to add new customers and tapes to and from the database. Each patron and each copy of each from the database. Each patron and each copy of each tape are to be associated with a unique bar-coded tape are to be associated with a unique bar-coded label.label.
Structure chart for Structure chart for initial decomposition of initial decomposition of video rental systemvideo rental system
Video Rental System
Process Transactions
Process Queries
Process Modifications
Structure chart for “process transactions” Structure chart for “process transactions” refinementrefinement
Process Transactions
Rent Tape
Query Tape
Validate Patron
Update Tape
Update Patron
Query Tape
Validate Patron
Compute Bill
Update Tape
Update Patron
Update Patron
Return Tape
Pay Bill
Final decomposition of video rental systemFinal decomposition of video rental system
Process Transactions
Rent Tape
Query Tape
Validate Patron
Update Tape
Update Patron
Query Tape
Validate Patron
Compute Bill
Update Tape
Update Patron
Update Patron
Return Tape
Pay Bill
Video Rental System
Process Queries
Process Modifications
Tape Queries
Patron Queries
Add Patron
Delete Patron
Add Tape
Delete Tape
Criteria for good TDDCriteria for good TDD
• CohesionCohesion– Every part fits naturally in its sectionEvery part fits naturally in its section
• CouplingCoupling– Interconnectedness between separate Interconnectedness between separate
piecespieces
– Avoid interconnections if possibleAvoid interconnections if possible
Object Oriented Design Object Oriented Design (OOD)(OOD)
• MethodologyMethodology– Techniques for creating a designTechniques for creating a design
– Use the requirements specification to Use the requirements specification to find the objectsfind the objects in the system in the system
– Then describe the objects in a formal Then describe the objects in a formal wayway
• C++ provides an ideal languageC++ provides an ideal language
Example: Video Rental Example: Video Rental SystemSystem
• Start by looking at the real world items it Start by looking at the real world items it manipulatesmanipulates
• These are easily identifiable as ‘objects’These are easily identifiable as ‘objects’
• Other things that are ‘objects’Other things that are ‘objects’– Anything that is capable of ‘doing things’Anything that is capable of ‘doing things’
– Example: Date Example: Date • It can increase, decrease, be expressed many It can increase, decrease, be expressed many
waysways
Example: Video rental Example: Video rental systemsystem
• Build a software system to support the operation of a Build a software system to support the operation of a video rental system. video rental system.
• The system should automate the process of renting The system should automate the process of renting videovideo tapestapes and receiving returned tapes, including and receiving returned tapes, including calculating and printing patron calculating and printing patron billsbills, which may or may , which may or may not be done at the same time the tape is returned. not be done at the same time the tape is returned.
• It must also give the It must also give the clerkclerk access to information about access to information about the tapes, such as the number of copies on the shelf of the tapes, such as the number of copies on the shelf of any given video owned by the store. any given video owned by the store.
• It must be able to add new It must be able to add new customerscustomers and tapes to and and tapes to and from the database. Each patron and each copy of each from the database. Each patron and each copy of each tape are to be associated with a unique bar-coded .tape are to be associated with a unique bar-coded .
ClassesClasses
• Each type of object is called a classEach type of object is called a class
• When we ‘find the objects’ we are When we ‘find the objects’ we are really finding the classesreally finding the classes
• ExampleExample– Tape class Tape class
– Patron classPatron class
Finding classesFinding classes• One method of finding classes is to develop One method of finding classes is to develop
scenarios that walk through the problem.scenarios that walk through the problem.
• This allows us to identify objects at every step This allows us to identify objects at every step in the process.in the process.
• Example: for class Date we might considerExample: for class Date we might consider– Video rental dateVideo rental date
– Video due dateVideo due date
Video example classesVideo example classes• Identify these by walking through the scenario the Identify these by walking through the scenario the
program is to mimicprogram is to mimic
• The main actors areThe main actors are– The customerThe customer
– The video tapeThe video tape
– The transaction processThe transaction process• The consoleThe console
• The scannerThe scanner
• The printerThe printer
Responsibility-driven Responsibility-driven designdesign
• The responsibilities of a class areThe responsibilities of a class are– the information it maintains (data the information it maintains (data
state)state)
– the actions it performs (operations)the actions it performs (operations)
• Classes also must work togetherClasses also must work together– they collaborate with other classesthey collaborate with other classes
OOD ProceduresOOD Procedures
• Find the classesFind the classes
• Determine the responsibilities of each Determine the responsibilities of each classclass
• Determine who collaborates with each Determine who collaborates with each classclass
OOD Procedures (con’t)OOD Procedures (con’t)
• Often you realize the need for new Often you realize the need for new classes while describing current classes while describing current onesones
• If you have a class with no If you have a class with no responsibilities you discard it.responsibilities you discard it.
Video Rental System Video Rental System ClassesClasses
• TapeTape
• PatronPatron
• ConsoleConsole
• ScannerScanner
• PrinterPrinter
Class responsibilities Class responsibilities (tape)(tape)
• Class TapeClass Tape– Keep track of tape identification dataKeep track of tape identification data
– Check self outCheck self out
– Check self inCheck self in
– Answer queries about it’s locationAnswer queries about it’s location
Tape classTape class
Class Responsibilities
Tape Keep track of tape identification dataCheck self outCheck self inAnswer queries about location of self
Responsibilities (patron)Responsibilities (patron)
• Class patronClass patron– Keep track of patron identification Keep track of patron identification
numbernumber
– Update patron identification dataUpdate patron identification data
– Update list of rented tapesUpdate list of rented tapes
– Update billing informationUpdate billing information
Patron classPatron class
Class Responsibilities
Patron Keep track of patron identification dataUpdate patron identification dataUpdate list of rented tapesUpdate billing information
Responsibilities (console)Responsibilities (console)
• ConsoleConsole– Check tapes in and outCheck tapes in and out
– Find the location of tapesFind the location of tapes
– Add and remove tapesAdd and remove tapes
– Add, update and remove patronsAdd, update and remove patrons
– Update patron billing informationUpdate patron billing information
Console classConsole class
Class Responsibilities
Console Check tapes in and outFind the location of tapesFind out about patronsAdd and remove tapesAdd, update, and remove patronsUpdate patron billing information
Responsibilities (scanner)Responsibilities (scanner)
• ScannerScanner– Read a bar code and return either a Read a bar code and return either a
tape or a patrontape or a patron
Scanner classScanner class
Class Responsibilities
Scanner Read a bar code and return either a tapeor a patron
Responsibilities (printer)Responsibilities (printer)
• PrinterPrinter– Print billsPrint bills
– Print receiptsPrint receipts
CollaboratorsCollaborators
• After all responsibilities have been After all responsibilities have been listed we can turn our attention to listed we can turn our attention to collaborationscollaborations
• There is a collaboration whenever There is a collaboration whenever one class must get information one class must get information from, or give it to another classfrom, or give it to another class
Problems with these Problems with these classes?classes?
• Class console may be doing too muchClass console may be doing too much– Should we break it up?Should we break it up?
• Class checkoutClass checkout
• Class checkin, etc.Class checkin, etc.
• Class scanner does only one thing?Class scanner does only one thing?
• Designs need to be debugged before Designs need to be debugged before being implementedbeing implemented
Which is better,TDD or Which is better,TDD or OOD?OOD?
• TDD is extensively usedTDD is extensively used– Inertia combined with hierarchical thinkingInertia combined with hierarchical thinking
• OOD provides features TDD does notOOD provides features TDD does not– Most problems are better modeled by Most problems are better modeled by
objects with data (a state) and behavior objects with data (a state) and behavior (operations)(operations)
– It takes work to truly think in an object It takes work to truly think in an object oriented modeoriented mode
Video rental system: Video rental system: Classes, responsibilities, and collaborators (Part 1 Classes, responsibilities, and collaborators (Part 1 of 2)of 2)
Class Responsibilities Collaborators
Tape Keep track of tape identification dataCheck self inCheck self outAnswer queries about location of self
Patron Keep track of patron identification dataUpdate patron identification dataUpdate list of rented tapesUpdate billing information
Tape
Console Check tapes in and outFind the location of tapesFind out about patronsAdd and remove tapesAdd, update, and remove patronsUpdate patron billing information
PatronTapeScannerPrinter
Table 2-6: Video rental system: Table 2-6: Video rental system: Classes, responsibilities, and collaborators (Part 2 Classes, responsibilities, and collaborators (Part 2 of 2)of 2)
Class Responsibilities Collaborators
Scanner Read a bar code and returneither a tape or a patron. (Thissuggests that a database thatlinks bar codes to tapes andpatrons is maintained by theScanner class.)
TapePatron
Printer Print billsPrint receipts
TapePatron