57
Three Ways to Apply Design Principles in Practice Ganesh Samarthyam Consultant, Trainer, and Author [email protected] www.designsmells.com

Three ways to apply design principles in practice

Embed Size (px)

Citation preview

Page 1: Three ways to apply design principles in practice

Three Ways to Apply Design Principles in Practice

Ganesh SamarthyamConsultant, Trainer, and Author

[email protected] www.designsmells.com

Page 2: Three ways to apply design principles in practice

Why care about design quality and design principles?

Page 3: Three ways to apply design principles in practice

The city metaphor

Source: http://indiatransportportal.com/wp-content/uploads/2012/04/Traffic-congestion1.jpg

“Cities grow, cities evolve, cities have parts that simply die while

other parts flourish; each city has to be renewed in order to meet the needs of its populace… Software-

intensive systems are like that. They grow, they evolve,

sometimes they wither away, and sometimes they flourish…”

Page 4: Three ways to apply design principles in practice

Messy city of software

Page 5: Three ways to apply design principles in practice

“Applying design principles is the key to creating high-quality software!”

Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation

Page 6: Three ways to apply design principles in practice

Example of beautiful design

int arr[] = {1, 4, 9, 16, 25}; // some values // find the first occurrence of 9 in the array int * arr_pos = find(arr, arr + 4, 9); std::cout<< “array pos = “<< arr_pos - arr << endl;

vector<int> int_vec; for(int i = 1; i <= 5; i++) int_vec.push_back(i*i); vector<int>::iterator vec_pos = find (int_vec.begin(), int_vec.end(), 9); std::cout<< “vector pos = “<< (vec_pos - int_vec.begin());

Page 7: Three ways to apply design principles in practice

"The critical design tool for software development is a mind well educated in design principles"

- Craig Larman

Page 8: Three ways to apply design principles in practice

SOLID principles•  There&should&never&be&more&than&one&reason&for&a&class&to&change&&

Single'Responsibility'Principle'(SRP)'

•  So6ware&en88es&(classes,&modules,&func8ons,&etc.)&should&be&open&for&extension,&but&closed&for&modifica8on&

Open'Closed'Principle'(OCP)'

•  Pointers&or&references&to&base&classes&must&be&able&to&use&objects&of&derived&classes&without&knowing&it&

Liskov’s'Subs<tu<on'Principle'(LSP)'

•  Depend&on&abstrac8ons,&not&on&concre8ons&Dependency'Inversion'Principle'(DIP)'

• Many&clientGspecific&interfaces&are&beHer&than&one&generalGpurpose&interface&

Interface'Segrega<on'Principle'(ISP)'

Page 9: Three ways to apply design principles in practice

Booch’s fundamental principles

Principles*

Abstrac/on*

Encapsula/on*

Modulariza/on*

Hierarchy*

Page 10: Three ways to apply design principles in practice

How to apply principles in practice?Abstraction Encapsulation Modularization Hierarchy

Code

How to bridge the gap?

Page 11: Three ways to apply design principles in practice

Real scenario #1Initial design

Page 12: Three ways to apply design principles in practice

Real scenario #1

❖ How will you refactor such that:

❖ A specific DES, AES, TDES, … can be “plugged” at runtime?

❖ Reuse these algorithms in new contexts?

❖ Easily add support for new algorithms in Encryption?

Next change: smelly design

Page 13: Three ways to apply design principles in practice

Time to refactor!Three strikes and you

refactor

Martin Fowler

Page 14: Three ways to apply design principles in practice

Potential solution #1?Broken

hierarchy!

Page 15: Three ways to apply design principles in practice

Potential solution #2?Algorithms not

reusable!

Page 16: Three ways to apply design principles in practice

Potential solution #3?

Page 17: Three ways to apply design principles in practice

Can you identify the pattern?

Page 18: Three ways to apply design principles in practice

You’re right: Its Strategy pattern!

Page 19: Three ways to apply design principles in practice

Real scenario #2

Initial design

Page 20: Three ways to apply design principles in practice

Real scenario #2How to add support for new

content types and/or algorithms?

Page 21: Three ways to apply design principles in practice

How about this solution?

Page 22: Three ways to apply design principles in practice

Can you identify the pattern structure?

Page 23: Three ways to apply design principles in practice

You’re right: Its Bridge pattern structure!

Page 24: Three ways to apply design principles in practice

Wait, what principle did we apply?

Page 25: Three ways to apply design principles in practice

Open Closed Principle (OCP)

Bertrand Meyer

Software entities should be open for extension, but closed for modification

Page 26: Three ways to apply design principles in practice

Variation Encapsulation Principle (VEP)

Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides

Encapsulate the concept that varies

Page 27: Three ways to apply design principles in practice

Fundamental principle: Encapsulation

The principle of encapsulation advocates separation of concerns andinformation hiding through techniques such as hiding implementation

details of abstractions and hiding variations

Page 28: Three ways to apply design principles in practice

Enabling techniques for encapsulation

Page 29: Three ways to apply design principles in practice

Use “enabling techniques” to apply design principles in practice

Key-takeaway #1

Page 30: Three ways to apply design principles in practice

Supporting new requirementsRevised design with new requirements

TextView

+ Draw()

BorderedTextView

+ Draw()+ DrawBorder()

ScrollableTextView ScrollableBorderedTextView

- borderWidth

+ Draw()+ ScrollTo()

- ScrollPosition

+ Draw()+ ScrollTo()+ DrawBorder()

- ScrollPosition- borderWidth

Page 31: Three ways to apply design principles in practice

Real scenario

❖ How will you refactor such that:

❖ You don't have to “multiply-out” sub-types? (i.e., avoid “explosion of classes”)

❖ Add or remove responsibilities (e.g., scrolling) at runtime?

Next change: smelly design

Page 32: Three ways to apply design principles in practice

How about this solution?VisualComponent

+ Draw()

TextView

+ Draw()

ScrollDecortor BorderDecorator

+ Draw()+ ScrollTo()

- ScrollPosition

+ Draw()+ DrawBorder()

- borderWidth

Decorator

+ Draw() component->Draw()

Decorator::Draw()DrawBorder()Decorator::Draw()

ScrollTo()

Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994

Page 33: Three ways to apply design principles in practice

At runtime (object diagram)

Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994

Page 34: Three ways to apply design principles in practice

Can you identify the pattern?VisualComponent

+ Draw()

TextView

+ Draw()

ScrollDecortor BorderDecorator

+ Draw()+ ScrollTo()

- ScrollPosition

+ Draw()+ DrawBorder()

- borderWidth

Decorator

+ Draw() component->Draw()

Decorator::Draw()DrawBorder()Decorator::Draw()

ScrollTo()

Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994

Page 35: Three ways to apply design principles in practice

You’re right: Its Decorator pattern!

Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994

Page 36: Three ways to apply design principles in practice

Identify pattern used in this code

LineNumberReader lnr = new LineNumberReader( new BufferedReader( new FileReader(“./test.c")));

String str = null;

while((str = lnr.readLine()) != null) System.out.println(lnr.getLineNumber() + ": " + str);

Page 37: Three ways to apply design principles in practice

Decorator pattern in Reader

Page 38: Three ways to apply design principles in practice

Scenarioa) How do we treat files

and folders alike?b) How can we handle

shortcuts?

File

+ GetName()+ GetSize()+ …

- name: String- size: int- type: FileType- data: char[]- …

Folder

+ GetName()+ GetFiles()+ GetFolders()+ …

- name: String - files[]: File- folders[]: Folder

Page 39: Three ways to apply design principles in practice

How about this solution?FileItem

+ GetName()+ GetSize()+ Add(FileItem)+ Remove(FileItem)

Folder

+ GetFiles()+ …

File

- files: FileItem

+ GetType()+ GetContents()+ …

- type: FileType- data: char[]

Shortcut

+ GetLinkedFileItem()+ …

- linkToFile: FileItem

Page 40: Three ways to apply design principles in practice

Composite pattern

Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994

Page 41: Three ways to apply design principles in practice

Enabling techniques for encapsulation

Page 42: Three ways to apply design principles in practice

Refactor “bad smells” to apply design principles in practice

Key-takeaway #2

Page 43: Three ways to apply design principles in practice

Tool driven approach for design quality

Page 44: Three ways to apply design principles in practice

Comprehension Tools

STANhttp://stan4j.com

Page 45: Three ways to apply design principles in practice

Comprehension Tools

Code Cityhttp://www.inf.usi.ch/phd/wettel/codecity.html

Page 46: Three ways to apply design principles in practice

Comprehension Tools

Imagix 4Dhttp://www.imagix.com

Page 47: Three ways to apply design principles in practice

Critique, code-clone detectors, and metric tools

Infusionwww.intooitus.com/products/infusion

Page 48: Three ways to apply design principles in practice

Critique, code-clone detectors, and metric tools

Designitewww.designite-tools.com

Page 49: Three ways to apply design principles in practice

Critique, code-clone detectors, and metric tools

PMD Copy Paste Detector (CPD)http://pmd.sourceforge.net/pmd-4.3.0/cpd.html

Page 50: Three ways to apply design principles in practice

Critique, code-clone detectors, and metric tools

Understandhttps://scitools.com

Page 51: Three ways to apply design principles in practice

Technical Debt quantification/visualization tools

Sonarqubehttp://www.sonarqube.org

Page 52: Three ways to apply design principles in practice

Refactoring tools

ReSharperhttps://www.jetbrains.com/resharper/features/

Page 53: Three ways to apply design principles in practice

Use design analysis tools to apply design principles in practice

Key-takeaway #3

Page 54: Three ways to apply design principles in practice

Key take-aways

❖ Thee effective ways to apply design principles in practice:

❖ Use “enabling techniques”

❖ Refactoring “bad smells”

❖ Use design analysis tools

Page 55: Three ways to apply design principles in practice

Principles and enabling techniques

Page 56: Three ways to apply design principles in practice

“Applying design principles is the key to creating high-quality software!”

Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation

Page 57: Three ways to apply design principles in practice

[email protected]@GSamarthyam

www.designsmells.com @designsmells