400 likes | 483 Views
CSC 213 – Large Scale Programming. Lecture 3: Designing Programs & Finding Classes. Today’s Goal. Improve design skills to make usable designs Noun extraction & UML class diagram reviewed Connections to use for fields in diagrams detailed
E N D
CSC 213 –Large Scale Programming Lecture 3:Designing Programs & Finding Classes
Today’s Goal • Improve design skills to make usable designs • Noun extraction & UML class diagram reviewed • Connections to use for fields in diagrams detailed • Describe process to find classes’ public methods • Using textual analysis to start our search for methods • Learn to test designs & find potential errors • Discover reasons for iterative nature of this process • Indentify & fix common errors that occur in designs • Learn about using CRC cards to check design validity
Designing Programs • Designing programs just like making music • How do you get to Carnegie Hall?
Designing Programs • Designing programs just like making music • How do you get to Carnegie Hall? (old joke)
Good News For The Term • Designing program ultimately like any other skill • Get many opportunities at this over term • Why is this important? Why should you care?
Good News For The Term • Designing program ultimately like any other skill • Get many opportunities at this over term • Why is this important? Why should you care?
First Step In Design: Classes • Find (& underline) nouns in requirements • Go through & eliminate any external to solution • Select entity classes(main classes in program) • Look at remaining nouns that not abstract & singular • Find boundary classes(I/O & recording states) • From options, use singular form of abstract nouns • Adjectives describing entity classes could work, too • Determine control classes(structs. & algorithms) • Needed for any plurals lacking specific number • Important algorithms that do not have a name
Next Step: Fields • Look for possessives during reading of text • Should also look for text suggesting data possession • Consider boundary classes; often used for fields • May rely on common understandings: use your brain • Can now publicize design for entire team to use • You’ve done your part: let suckers do the hard work • Use UML class diagrams to avoid writing lots of words
UML Class Diagrams • Classes drawn in 3-part box • Class name written in top portion of box • Attributes (fields) written in middle portion • Operations (methods) written in bottom portion • All variables writtenusing following format : name : type • Show important relationships between classes • Inheritence & important fields normally shown here • Solid lines for classes and dashed lines for interfaces
Optional Relationships • Aggregation • Used when target instances used outside of source • CaraggregatesCarParts(e.g., tires, engine, lights…) • Composition • Use when objects from target used only within source • ChessboardcomposesSquares • Association • Source object has field of target type • Often used to imply source calls target's methods
Required Connection • Generalization (“inheritance”) • Shows subclass-superclass relationship • Does not involve fields, multiplicities, or anything else • Makes this a different type of connection than others • Must draw all generalizations within the diagram • For implementation to be correct, these are critical • Cannot specify any other way, so vital to be shown
Actions Are Verbs • Now need to find methods for all of our classes • At this point in time, many not know all methods • Important public methods described in text, however • To find methods, look for verbs in requirements • Circle each verb since it is a possible method • Since often not helpful, check all “to be” & “has” uses • Circle algorithm names and longer action descriptions • Looking through text, only public methods found • Find more methods later when looking at algorithms
Looking for verbs 1. Circle all the verbs in the text Buttonsin elevatorsand on the floors control the movementof nelevators in a buildingwith mfloors. Buttons illuminate when pressed to request the elevator stop at a specific floor; the illuminationis canceled when the requesthas been satisfied. When an elevator has no requests, it remains at its current floor with its doorsclosed.
Looking for verbs 1. Circle all the verbs in the text Buttons in elevatorsand on the floorscontrol the movementof nelevators in a buildingwith mfloors. Buttonsilluminate when pressed to request the elevatorstop at a specific floor; the illuminationis canceled when the requesthas been satisfied. When an elevatorhas no requests, it remains at its current floor with its doorsclosed.
Looking for verbs 2. Double-check all uses of “to be” and “has” Buttonsin elevatorsand on the floorscontrol the movementof nelevators in a buildingwith mfloors. Buttonsilluminate when pressed to request the elevatorstop at a specific floor; the illuminationis canceled when the requesthas been satisfied. When an elevatorhas no requests, it remains at its current floor with its doorsclosed.
Looking for verbs 2. Double-check all uses of “to be” and “has” Buttonsin elevatorsand on the floorscontrol the movementof nelevators in a buildingwith mfloors. Buttonsilluminate when pressed to request the elevatorstop at a specific floor; the illuminationis canceled when the requesthas been satisfied. When an elevatorhasno requests, it remains at its current floor with its doorsclosed.
Looking for verbs 3. Circle algorithm names & action descriptions Buttonsin elevatorsand on the floorscontrol the movementof nelevators in a buildingwith mfloors. Buttonsilluminate when pressed to request the elevatorstop at a specific floor; the illuminationis canceled when the requesthas been satisfied. When an elevatorhasno requests, it remains at its current floor with its doorsclosed.
Looking for verbs 3. Circle algorithm names & action descriptions Buttonsin elevatorsand on the floorscontrol the movementof nelevators in a buildingwith mfloors. Buttonsilluminate when pressed to request the elevatorstop at a specific floor; the illuminationis canceled when the requesthas been satisfied. When an elevatorhasno requests, it remains at its current floor with its doorsclosed.
Which Class Gets the Method? • Look for SRP violations for path to true laziness
Which Class Gets the Method? • Look for SRP violations for path to true laziness
Single Responsibility Principle • Goal of every class & method written • Make sure that class represents single concept • Methods perform single action & not “bucket of mud” • Many benefits accrue when code follows SRP • Easier to write code, since design becomes simpler • More focused results make debugging a snap • Modifying design quicker when changes needed
No Free Cheeseburgers • Achieving SRP means 1 last step in design • More classes & methods often created by this process • But also create smaller, simpler classes & methods • Less total time requiredeven with this "extra" step • To get SRP, people found CRC cards are easiest • 3x5 cards & a brain are all this process takes
CRC Cards Explained • Divide 3x5 Card into 3 parts • Class name from UML design document • Responsibilities are the methods class will need • Collaborations are classes it will interact with
Responsibilities • Initially you should start with actions you found • Add facts class knows, since these are getter methods • If class should not be responsible, move action • Instead, the action placed in class where makes sense • Make classes collaborators after action moved • Pays off in long run, but may make new classes now
Responsibilities • Initially you should start with actions you found • Add facts class knows, since these are getter methods • If class should not be responsible, move action • Instead, the action placed in class where makes sense • Make classes collaborators after action moved • Pays off in long run, but may make new classes now How to know if responsibility logical?
Seriously! Go Yourself
If the Responsibility Fits • Try each responsibilityinto following statement • Will find statement is valid only if responsibility fits The(class name)(responsibility)itself • Move actions to class that works in statement The(new class) (responsibility) (old class)
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
Let’s Try It For Car • Which of these responsibilities belong to car Starts engine Kills engine Changes tires Gets driven Gets washed Changes oil Warns oil is low Reports on fuel level
So Remember Go Yourself
For Next Lecture • Reading for Wednesday available on Angel • How can we solve bugs BEFORE we code? • Can we prevent getting trapped and lost? • Prevent your name from becoming swear word! • Weekly assignment problem due in class Wed. • Can be found on Angel as problem #3 for Week #1