tvt 2015 design - ut · patterns • common solutions to frequent problems arising in...
TRANSCRIPT
![Page 1: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/1.jpg)
DESIGN
![Page 3: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/3.jpg)
Why design matters?
![Page 4: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/4.jpg)
Longevity.
Code is used for years and years.
![Page 5: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/5.jpg)
productivity vs time
Prod
uctiv
ity
0
25
50
75
100
Time
![Page 6: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/6.jpg)
CLEAN CODE
![Page 7: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/7.jpg)
Clean Code A Handbook of Agile
Software Craftsmanship
Robert C. Martin, 2008“Uncle Bob”
![Page 8: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/8.jpg)
–Ward Cunninghaminventor of Wiki and Fit
co-inventor of eXtreme Programming
“You know you are working on clean code when each routine you read
turns out to be pretty much what you expected.”
![Page 9: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/9.jpg)
FOUR ELEMENTS OF SIMPLE DESIGN
1. Passes all tests
2. No duplication
3. Expresses intent
4. Small
Kent Beck — Creator of Extreme Programming, author of JUnit
![Page 10: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/10.jpg)
NAMINGvariables, methods, classes, ...
![Page 11: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/11.jpg)
let’s look at some examples…
![Page 12: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/12.jpg)
CORRECT ENGLISH ONLY, PLEASE!
• Word-for-word translation might not be the correct solution
• Use correct domain terminology - ask domain experts what they use
• Spelling and grammar
• Can you read your code like a story?
![Page 13: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/13.jpg)
USE THE SAME TERM CONSISTENTLY
• Customer – Client – Party – Counterparty
• state – status
• Contract – Agreement
• get – load – fetch – find – retrieve
• regCode – regNumber – personalCode
• … and don’t reuse the same term in another meaning/context
![Page 14: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/14.jpg)
FUNCTIONS
![Page 15: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/15.jpg)
A GOOD FUNCTION• short
• very few indentation levels
• does only one thing - only one level of abstraction
• “Functions should do one thing. They should do it well. They should do it only.” —Robert “Uncle Bob” Martin
![Page 16: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/16.jpg)
for example…
![Page 17: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/17.jpg)
FUNCTION ARGUMENTS
• Less is more!
• Ideally none, one and two are OK
• Three is usually too much, anything more is a design smell
![Page 18: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/18.jpg)
for example…
![Page 19: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/19.jpg)
NO SIDE-EFFECTS
• A function that seems to be read-only according to its name, should behave as such
• If a function changes state then name it accordingly
![Page 20: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/20.jpg)
COMMENTS
![Page 21: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/21.jpg)
NO COMMENTS!• Comments lie:
• You cannot tie them to code
• You cannot refactor them
• You cannot test them
• The truth is in the code
• Do not use a comment when an appropriately named variable or function would do!
![Page 22: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/22.jpg)
for example…
![Page 23: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/23.jpg)
WHEN ARE COMMENTS OK?“The proper use of comments is to compensate for our failure to
express ourselves in code.”
• Legal comments
• Warning of consequences
• TODO comments
• Amplification
• javadocs in public APIs
![Page 24: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/24.jpg)
KEEP YOUR CODE CLEAN
![Page 25: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/25.jpg)
THE BOY SCOUT RULE
Leave the campground cleaner than you found it
Can you imagine working on a project where code simply got better as time passed?
Do you believe that any other option is professional?
![Page 26: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/26.jpg)
OBJECT-ORIENTED DESIGN
![Page 27: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/27.jpg)
Agile Software Development
Principles, Patterns, and Practices
Robert C. Martin, 2003
![Page 28: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/28.jpg)
INTERFACES AND ABSTRACT CLASSES
Ignore implementation detailsProgram to interfaces
![Page 29: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/29.jpg)
JAVA COLLECTIONS API• Iterable
• Collection
• List: ArrayList, LinkedList
• Set: HashSet, LinkedHashSet
• SortedSet: TreeSet
• Map: HashMap, LinkedHashMap
• SortedMap: TreeMap
![Page 30: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/30.jpg)
JAVA.IO• byte based I/O
• InputStream & OutputStream
• FileInputStream, SocketInputStream, ByteArrayInputStream, GZIPInputStream
• character based I/O
• Reader & Writer
• FileWriter, StringWriter, OutputStreamWriter
![Page 31: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/31.jpg)
abstract class example…
![Page 32: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/32.jpg)
DEPENDENCY INJECTION INVERSION OF CONTROL
• The Hollywood Principle: Don’t call us – we’ll call you
• Don’t create your dependencies - they will be given to you
• Cleaner design
• Easier to test
![Page 33: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/33.jpg)
DI example…
![Page 34: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/34.jpg)
DEPENDENCY INJECTION
• Guice – github.com/google/guice
• Dagger - google.github.io/dagger/
• Spring Framework – projects.spring.io/spring-framework/
• Mockito – mockito.org
![Page 35: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/35.jpg)
FAVOR COMPOSITION OVER INHERITANCE
• Inheritance is for “is-a” relationships - when the same interface is most logical
• Composition is for “consists-of”, “contains”, “uses”, “has” relationships - providing decoupling – independence of each other's changes
![Page 36: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/36.jpg)
for example…
![Page 37: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/37.jpg)
IMMUTABILITY
• An immutable object is an object whose state cannot be modified after it is created
• What is the most common immutable object in the Java SDK?
• Why?Caching, thread-safety, map keys
![Page 38: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/38.jpg)
DESIGN PATTERNS
![Page 39: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/39.jpg)
Design Patterns Elements of Reusable
Object-Oriented Software
[Gang of Four Book – GOF]
Erich GammaRichard Helm Ralph Johnson John Vlissides
1994
![Page 40: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/40.jpg)
PATTERNS
• Common solutions to frequent problems arising in object-oriented design
• A systematized catalog of solutions by skilled and experienced developers
• Simplifies communication between developers –“we are using the Strategy pattern”
![Page 41: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/41.jpg)
CREATIONAL PATTERNS
the process of object creation
Factory MethodAbstract Factory
BuilderPrototypeSingleton
![Page 42: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/42.jpg)
STRUCTURAL PATTERNS the composition of classes
DecoratorAdapter (Wrapper)
ProxyFaçade
FlyweightBridge
CompositeNull Object
![Page 43: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/43.jpg)
BEHAVIORAL PATTERNShow classes or objects interact and distribute
responsibility
Template methodObserver (Publish-Subscribe, Listener)
InterpreterMemento
Chain of Responsibility
![Page 44: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/44.jpg)
BEHAVIORAL PATTERNShow classes or objects interact and distribute
responsibility
StateCommandStrategyIteratorVisitor
Mediator
![Page 45: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/45.jpg)
Refactoring to Patterns
Joshua Kerievsky, 2004
Avoid:- over-engineering
- under-engineering
![Page 46: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/46.jpg)
–Antoine de Saint-Exupery
“A designer knows he has achieved perfection not when there is nothing left to add,
but when there is nothing left to take away.”
![Page 47: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers](https://reader036.vdocuments.site/reader036/viewer/2022063017/5fd85f8c93c7dc56251f74aa/html5/thumbnails/47.jpg)
FINAL WORLDS
The Boy Scout Rule
Experience, experience, experience
Read a lot of others’ code