software design in practice (with java examples)
TRANSCRIPT
![Page 2: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/2.jpg)
Discussion Question
Which one of the following is MOST IGNORED aspect in software development?
❖ Code quality
❖ Software testing
❖ Software design
❖ Programming
![Page 3: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/3.jpg)
![Page 4: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/4.jpg)
![Page 5: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/5.jpg)
"... a delightful, engaging, actionable read... you have in your hand a veritable field guide of smells... one of the more interesting and complex expositions of
software smells you will ever find..."
- From the foreword by Grady Booch (IBM Fellow and Chief Scientist for Software Engineering, IBM Research)
![Page 6: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/6.jpg)
Why Care About Principles?
![Page 7: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/7.jpg)
Poor software quality costs more than $150 billion per year
in U.S. and greater than $500 billion per year worldwide
- Capers Jones
![Page 8: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/8.jpg)
Source: Estimating the Principal of an Application's Technical Debt, Bill Curtis, Jay Sappidi, Alexandra Szynkarski, IEEE Software, Nov.-Dec. 2012.
![Page 9: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/9.jpg)
Source: Consortium of IT Software Quality (CISQ), Bill Curtis, Architecturally Complex Defects, 2012
![Page 10: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/10.jpg)
–Craig Larman
"The critical design tool for software development is a mind well educated in design principles"
![Page 11: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/11.jpg)
For Architects: Design is the Key!
![Page 12: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/12.jpg)
Design Thinking
![Page 13: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/13.jpg)
Fundamental Principles in Software Design
Principles*
Abstrac/on*
Encapsula/on*
Modulariza/on*
Hierarchy*
![Page 14: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/14.jpg)
Proactive Application: Enabling Techniques
![Page 15: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/15.jpg)
Reactive Application: Smells
![Page 16: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/16.jpg)
$ cat limerick.txt There was a young lady of Niger Who smiled as she rode on a tiger. They returned from the ride With the lady inside And a smile on the face of the tiger.
![Page 17: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/17.jpg)
$ cat limerick.txt | tr -cs "[:alpha:]" "\n" | awk '{print length(), $0}' | sort | uniq
1 a 2 as 2 of 2 on 3 And 3 Who 3 she 3 the 3 was 4 They 4 With 4 face 4 from 4 lady 4 ride 4 rode 5 Niger 5 There 5 smile 5 tiger 5 young 6 inside 6 smiled 8 returned
![Page 18: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/18.jpg)
List<String> lines = Files.readAllLines(Paths.get("./limerick.txt"), Charset.defaultCharset());
Map<Integer, List<String>> wordGroups = lines.stream() .map(line -> line.replaceAll("\\W", "\n").split("\n")) .flatMap(Arrays::stream) .sorted() .distinct() .collect(Collectors.groupingBy(String::length));
wordGroups.forEach( (count, words) -> { words.forEach(word -> System.out.printf("%d %s %n", count, word)); });
1 a 2 as 2 of 2 on 3 And 3 Who 3 she 3 the 3 was 4 They 4 With 4 face 4 from 4 lady 4 ride 4 rode 5 Niger 5 There 5 smile 5 tiger 5 young 6 inside 6 smiled 8 returned
![Page 19: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/19.jpg)
Parallel Streams
![Page 20: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/20.jpg)
race conditions
![Page 21: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/21.jpg)
![Page 22: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/22.jpg)
deadlocks
![Page 23: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/23.jpg)
![Page 24: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/24.jpg)
I really really hate concurrency problems
![Page 25: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/25.jpg)
Parallel code
Serial code
![Page 26: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/26.jpg)
long numOfPrimes = LongStream.rangeClosed(2, 100_000) .filter(PrimeNumbers::isPrime) .count();
System.out.println(numOfPrimes);
Prints 9592
2.510 seconds
![Page 27: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/27.jpg)
Parallel code
Serial code
Let’s flip the switch by calling parallel() function
![Page 28: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/28.jpg)
long numOfPrimes = LongStream.rangeClosed(2, 100_000) .parallel() .filter(PrimeNumbers::isPrime) .count();
System.out.println(numOfPrimes);
Prints 9592
1.235 seconds
![Page 29: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/29.jpg)
Wow! That’s an awesome flip switch!
![Page 30: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/30.jpg)
Internally, parallel streams make use of fork-join framework
![Page 31: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/31.jpg)
Discussion Question
What is refactoring?
❖ Needless rework!
❖ Changing internal structure without changing external behaviour
❖ Changing external quality (scalability, performance, etc)
❖ Fixing hidden or latent defects
![Page 32: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/32.jpg)
What’s that smell?public'Insets'getBorderInsets(Component'c,'Insets'insets){'
''''''''Insets'margin'='null;'
'''''''''//'Ideally'we'd'have'an'interface'defined'for'classes'which'
''''''''//'support'margins'(to'avoid'this'hackery),'but'we've'
''''''''//'decided'against'it'for'simplicity'
''''''''//'
''''''''if'(c'instanceof'AbstractBuEon)'{'
'''''''''''''''''margin'='((AbstractBuEon)c).getMargin();'
''''''''}'else'if'(c'instanceof'JToolBar)'{'
''''''''''''''''margin'='((JToolBar)c).getMargin();'
''''''''}'else'if'(c'instanceof'JTextComponent)'{'
''''''''''''''''margin'='((JTextComponent)c).getMargin();'
''''''''}'
''''''''//'rest'of'the'code'omiEed'…'
![Page 33: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/33.jpg)
Refactoring
![Page 34: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/34.jpg)
Refactoring
!!!!!!!margin!=!c.getMargin();
!!!!!!!!if!(c!instanceof!AbstractBu8on)!{!
!!!!!!!!!!!!!!!!!margin!=!((AbstractBu8on)c).getMargin();!
!!!!!!!!}!else!if!(c!instanceof!JToolBar)!{!
!!!!!!!!!!!!!!!!margin!=!((JToolBar)c).getMargin();!
!!!!!!!!}!else!if!(c!instanceof!JTextComponent)!{!
!!!!!!!!!!!!!!!!margin!=!((JTextComponent)c).getMargin();!
!!!!!!!!}
![Page 35: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/35.jpg)
Design Smells: Example
![Page 36: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/36.jpg)
Discussion Example
![Page 37: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/37.jpg)
Design Smells: Example
![Page 38: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/38.jpg)
Discussion Example
// using java.util.Date Date today = new Date(); System.out.println(today);
$ java DateUse Wed Dec 02 17:17:08 IST 2015
Why should we get the time and timezone details if I only want a date? Can
I get rid of these parts? No!
![Page 39: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/39.jpg)
So What!Date today = new Date(); System.out.println(today); Date todayAgain = new Date(); System.out.println(todayAgain);
System.out.println(today.compareTo(todayAgain) == 0);
Thu Mar 17 13:21:55 IST 2016 Thu Mar 17 13:21:55 IST 2016 false
What is going on here?
![Page 40: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/40.jpg)
Refactoring for Date
Replace inheritance with delegation
![Page 41: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/41.jpg)
Joda API
JSR 310: Java Date and Time API
Stephen Colebourne
![Page 42: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/42.jpg)
java.time package!
![Page 43: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/43.jpg)
Refactored SolutionLocalDate today = LocalDate.now(); System.out.println(today); LocalDate todayAgain = LocalDate.now(); System.out.println(todayAgain); System.out.println(today.compareTo(todayAgain) == 0);
2016-03-17 2016-03-17 true
Works fine now!
![Page 44: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/44.jpg)
Refactored Example … You can use only date, time, or even timezone, and combine them as
needed!
LocalDate today = LocalDate.now(); System.out.println(today); LocalTime now = LocalTime.now(); System.out.println(now);
ZoneId id = ZoneId.of("Asia/Tokyo"); System.out.println(id);
LocalDateTime todayAndNow = LocalDateTime.now(); System.out.println(todayAndNow);
ZonedDateTime todayAndNowInTokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo")); System.out.println(todayAndNowInTokyo);
2016-03-17 13:28:06.927 Asia/Tokyo 2016-03-17T13:28:06.928 2016-03-17T16:58:06.929+09:00[Asia/Tokyo]
![Page 45: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/45.jpg)
More classes in Date/Time API
![Page 46: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/46.jpg)
What’s that smell?
![Page 47: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/47.jpg)
Liskov’s Substitution Principle (LSP)
It#should#be#possible#to#replace#objects#of#supertype#with#objects#of#subtypes#without#
altering#the#desired#behavior#of#the#program#
Barbara#Liskov#
![Page 48: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/48.jpg)
Refactoring
Replace inheritance with delegation
![Page 49: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/49.jpg)
What’s that smell?switch'(transferType)'{'
case'DataBuffer.TYPE_BYTE:'
byte'bdata[]'='(byte[])inData;'
pixel'='bdata[0]'&'0xff;'
length'='bdata.length;'
break;'
case'DataBuffer.TYPE_USHORT:'
short'sdata[]'='(short[])inData;'
pixel'='sdata[0]'&'0xffff;'
length'='sdata.length;'
break;'
case'DataBuffer.TYPE_INT:'
int'idata[]'='(int[])inData;'
pixel'='idata[0];'
length'='idata.length;'
break;'
default:'
throw' new' UnsupportedOperaQonExcepQon("This'method' has' not' been' "+' "implemented'for'transferType'"'+'transferType);'
}'
![Page 50: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/50.jpg)
Replace conditional with polymorphism
protected(int(transferType;! protected(DataBuffer(dataBuffer;!
pixel(=(dataBuffer.getPixel();(
length(=(dataBuffer.getSize();!
switch((transferType)({(
case(DataBuffer.TYPE_BYTE:(
byte(bdata[](=((byte[])inData;(
pixel(=(bdata[0](&(0xff;(
length(=(bdata.length;(
break;(
case(DataBuffer.TYPE_USHORT:(
short(sdata[](=((short[])inData;(
pixel(=(sdata[0](&(0xffff;(
length(=(sdata.length;(
break;(
case(DataBuffer.TYPE_INT:(
int(idata[](=((int[])inData;(
pixel(=(idata[0];(
length(=(idata.length;(
break;(
default:(
throw( new( UnsupportedOperaRonExcepRon("This( method(has( not( been( "+( "implemented( for( transferType( "( +(transferType);(
}!
![Page 51: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/51.jpg)
Discussion Question
What is the biggest deterrent to performing refactoring?
❖ Deadlines! (Lack of time and resources)
❖ Fear of breaking the working code
❖ Not able to get management buy-in for refactoring
❖ Lack of technical skills to perform refactoring
![Page 52: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/52.jpg)
Configuration Smells!
![Page 53: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/53.jpg)
Language Features & Refactoring public static void main(String []file) throws Exception { // process each file passed as argument
// try opening the file with FileReader try (FileReader inputFile = new FileReader(file[0])) { int ch = 0; while( (ch = inputFile.read()) != -1) { // ch is of type int - convert it back to char System.out.print( (char)ch ); } } // try-with-resources will automatically release FileReader object }
public static void main(String []file) throws Exception { Files.lines(Paths.get(file[0])).forEach(System.out::println); }
Java 8 lambdas and streams
![Page 54: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/54.jpg)
Tangles in JDK
![Page 55: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/55.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved
![Page 56: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/56.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved
![Page 57: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/57.jpg)
Structural Analysis for Java (stan4j)
![Page 58: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/58.jpg)
JArchitect
![Page 59: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/59.jpg)
InFusion
![Page 60: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/60.jpg)
SotoArc
![Page 61: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/61.jpg)
CodeCity
![Page 62: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/62.jpg)
Name the first object
oriented programming
language
![Page 63: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/63.jpg)
BANGALORE CONTAINER CONFERENCE 2017
Sponsorship Deck
Apr 07Bangalore
www.containerconf.in
![Page 64: Software Design in Practice (with Java examples)](https://reader036.vdocument.in/reader036/viewer/2022081520/588795931a28ab5b1a8b5c89/html5/thumbnails/64.jpg)
[email protected] @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/ganeshsg