Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and...
Transcript of Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and...
![Page 1: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/1.jpg)
Review: Cohesion and Coupling, Mutable, Inheritance
Screen Layouts
Software methodologies – Extreme ProgrammingSoftware methodologies – Extreme Programming
Object-Oriented Design – CRC Cards
- UML class diagrams
![Page 2: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/2.jpg)
![Page 3: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/3.jpg)
Analysis
Design
ImplementationSoftware
Implementation
Testing
Deployment
Maintenance
Software Development
![Page 4: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/4.jpg)
� Standardized approaches intended to:◦ Reduce costs
◦ Increase predictability of results
� Examples:� Examples:◦ Waterfall model
◦ Spiral model
◦ “Rational Unified Process”
![Page 5: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/5.jpg)
� Do each stage to completion� Then do the next stage
Analysis
Design
Pipe dream model?
Implementation
Testing
Deployment
![Page 6: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/6.jpg)
� Repeat phases in a cycle
� Produce a prototype at end of each cycle
� Get early feedback, incorporate changes
� Schedule overruns� Scope creep
PrototypePrototype
Deployment
![Page 7: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/7.jpg)
� Like the spiral model with veryveryveryvery short cycles
� Pioneered by Kent Beck
One of several “agile” methodologies, focused � One of several “agile” methodologies, focused on building high quality software quickly
� Rather than focus on rigid process, XP espouses 12 key practices…
![Page 8: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/8.jpg)
� Realistic planning
� Small releases
� Shared metaphors
� Pair programmingPair programmingPair programmingPair programming
� Collective ownership
� Continuous integration
� Simplicity
� TestingTestingTestingTesting
� RefactoringRefactoringRefactoringRefactoring
� 40-hour week
� On-site customer
� Coding standardsCoding standardsCoding standardsCoding standards
When you see opportunity to make code better, do it
Use descriptive names Q1
![Page 9: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/9.jpg)
It starts with good classes…
![Page 10: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/10.jpg)
� Often come from nouns in the problem description
� May…◦ Represent single concepts
� Circle, Investment� Circle, Investment
◦ Be abstractions of real-life entities
� BankAccount, TicTacToeBoard
◦ Be actors
� Scanner, CircleViewer
◦ Be utilities
� Math
Q2Q2Q2Q2
![Page 11: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/11.jpg)
� Can’t tell what it does from its name
◦ PayCheckProgram
� Turning a single action into a class
◦ ComputePaycheck◦ ComputePaycheck
� Name isn’t a noun◦ Interpolate, Spend
Q3Q3Q3Q3
![Page 12: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/12.jpg)
� Cohesion
� Coupling
![Page 13: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/13.jpg)
� A class should represent a single concept
� Public methods and constants should be cohesive
� Which is more cohesive?
CashRegister
double NICKEL_VALUEdouble DIME_VALUE
double QUARTER_VALUE
void add(int nickels, intdimes, int quarters)
…
CashRegister
void add(ArrayList<Coin> coins)…
Coin
double getValue()Q4Q4Q4Q4
![Page 14: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/14.jpg)
� When one classes requires another class to do its job, the first class depends on the second
� Shown on UML
CashRegister
void add(ArrayList<Coin> coins)� Shown on UML diagrams as:◦ dashed line
◦ with open arrowhead
void add(ArrayList<Coin> coins)…
Coin
double getValue()Q5Q5Q5Q5
![Page 15: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/15.jpg)
� Lots of dependencies == high coupling
� Few dependencies == low coupling
� Which is better? Why?
Q6Q6Q6Q6
![Page 16: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/16.jpg)
� High cohesion
� Low coupling
Immutable where practical, document where not� Immutable where practical, document where not
� Inheritance for code reuse
� Interfaces to allow others to interact with your code
![Page 17: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/17.jpg)
A practical technique
![Page 18: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/18.jpg)
� We won’t use full-scale, formal methodologies◦ Those are in later SE courses
� We will practice a common object-oriented design technique using CRC CardsCRC CardsCRC CardsCRC Cardsdesign technique using CRC CardsCRC CardsCRC CardsCRC Cards
� Like any design technique, the key to success is practicethe key to success is practicethe key to success is practicethe key to success is practice
![Page 19: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/19.jpg)
1.1.1.1. Discover classes Discover classes Discover classes Discover classes based on requirements
2.2.2.2. Determine responsibilities Determine responsibilities Determine responsibilities Determine responsibilities of 2.2.2.2. Determine responsibilities Determine responsibilities Determine responsibilities Determine responsibilities of each class
3.3.3.3. Describe relationships Describe relationships Describe relationships Describe relationships between classes
Q2
![Page 20: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/20.jpg)
� Brainstorm a list of possible classes◦ Anything that might work
◦ No squashing
� Prompts:◦ Look for nounsnounsnounsnouns
Tired of hearing this yet?
◦ Look for nounsnounsnounsnouns
◦ Multiple objects are often created from each class � so look for plural conceptsplural conceptsplural conceptsplural concepts
◦ Consider how much detail a concept requires:
� A lot? Probably a class
� Not much? Perhaps a primitive type
� Don’t expect to find them all � add as needed
![Page 21: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/21.jpg)
� Look for verbsverbsverbsverbs in the requirements to identify responsibilitiesresponsibilitiesresponsibilitiesresponsibilities of your system
� Which class handles the responsibility?
� Can use CRC Cards CRC Cards CRC Cards CRC Cards to discover this:
◦ CCCClasseslasseslasseslasses
◦ RRRResponsibilitiesesponsibilitiesesponsibilitiesesponsibilities
◦ CCCCollaboratorsollaboratorsollaboratorsollaborators
![Page 22: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/22.jpg)
� Use one index card per class
Class name
CollaboratorsResponsibilities
Q3
![Page 23: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/23.jpg)
1. Pick a responsibility of the program
2. Pick a class to carry out that responsibility◦ Add that responsibility to the class’s card
3. Can that class carry out the responsibility by itself?itself?◦ Yes � Return to step 1
◦ No �
� Decide which classes should help
� List them as collaborators on the first card
� Add additional responsibilities to the collaborators’ cards
![Page 24: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/24.jpg)
� Spread the cards out Spread the cards out Spread the cards out Spread the cards out on a table◦ Or sticky notes on a whiteboard instead of cards
� Use a “token” Use a “token” Use a “token” Use a “token” to keep your place◦ A quarter or a magnet
� Focus on highFocus on highFocus on highFocus on high----level responsibilitieslevel responsibilitieslevel responsibilitieslevel responsibilities� Focus on highFocus on highFocus on highFocus on high----level responsibilitieslevel responsibilitieslevel responsibilitieslevel responsibilities◦ Some say < 3 per card
� Keep it informalKeep it informalKeep it informalKeep it informal◦ Rewrite cards if they get to sloppy
◦ Tear up mistakes
◦ Shuffle cards around to keep “friends” together
![Page 25: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/25.jpg)
� Classes usually are related to their collaborators
� Draw a UML class diagram showing how
� Common relationships:◦ InheritanceInheritanceInheritanceInheritance: only when subclass is ais ais ais a special case◦ InheritanceInheritanceInheritanceInheritance: only when subclass is ais ais ais a special case
◦ AggregationAggregationAggregationAggregation: when one class has ahas ahas ahas a fieldfieldfieldfield that references another class
◦ DependencyDependencyDependencyDependency: like aggregation but transient, usually for method parameters, “has a” temporarily“has a” temporarily“has a” temporarily“has a” temporarily
◦ AssociationAssociationAssociationAssociation: any other relationship, can label the arrow, e.g., constructsconstructsconstructsconstructs
NEW!
![Page 26: Review: Cohesion and Coupling, Mutable, Inheritance Screen ...€¦ · Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies –Extreme Programming](https://reader033.fdocuments.in/reader033/viewer/2022051311/603e377f9ee5d3297807e034/html5/thumbnails/26.jpg)
Q4