evolutionary design solid

40
Agile Evolutionary Design & SOLID Principles

Upload: xebia-it-architects

Post on 10-May-2015

1.152 views

Category:

Technology


2 download

DESCRIPTION

This ppt.is presented by Sai Venkatakrishnan (Developer in test at Thoughtworks) at Agile NCR 2009 Conference held on 18th July at Park Premier Hotel.

TRANSCRIPT

Page 1: Evolutionary Design Solid

Agile Evolutionary Design & SOLID Principles

Page 2: Evolutionary Design Solid

Introduction

● Typical Design Process● Agile Design Process● SOLID principles● And coding time :)● Questions anytime.

Page 3: Evolutionary Design Solid

Typical Evolution of System

Dream system designed to perfection and beauty. Meaning of beauty and perfection are subjective. What does it mean for you?

Page 4: Evolutionary Design Solid

Typical Evolution of System

Adding features

Page 5: Evolutionary Design Solid

Typical Evolution of System

Changing features

Page 6: Evolutionary Design Solid

Birth of Legacy System

Somewhere in near future

Page 7: Evolutionary Design Solid

Agile Design Process

Page 8: Evolutionary Design Solid

Beautiful System

Beautiful System

Page 9: Evolutionary Design Solid

Adding a feature

Page 10: Evolutionary Design Solid

Clean the mess

We will see some of the techniques which will help us tokeep the system clean in this presentation.

Page 11: Evolutionary Design Solid

After a period of time

Beautiful System.

Page 12: Evolutionary Design Solid

What is Agile Design?

● Not doing things upfront like having an interface for every class we write (speculative generalization) or writing a system to dynamically load the components.

● Not doing an anticipatory design

● But still setting a stage for design to evolve for tomorrow

But what helps to set the stage?

Page 13: Evolutionary Design Solid

XP Practices

Page 14: Evolutionary Design Solid

But at code level what helps to create simple and evolutionary design?

Page 15: Evolutionary Design Solid

Sample Application – Reporting AppConcentrate on the principles and not on the app

Reporter App

File

Read the flat file and generate the report

Reporter App

● WriteReport● ReadFile● FormatReport

Page 16: Evolutionary Design Solid

SOLID Principles

Page 17: Evolutionary Design Solid

SOLID Principles

● Single Responsibility Principle● Open Closed Principle● Liskov Substitution Principle● Interface Segregation Principle● Dependency Inversion Principle

Page 18: Evolutionary Design Solid

Single Responsibility Principle

Page 19: Evolutionary Design Solid

Single Responsibility Principle

● A class should have one, and only one, reason to change.

● Or a class should not handle more than one responsibility

● Design smell – Class changed when unrelated responsibilities change. High afferent coupling? Or multiple classes changed when a responsibility changes. High Efferent Coupling?

● Shotgun surgery and divergent change

● If it is not possible to separate the responsibilities at least show the presence of multiple roles played by the object.

● Applies not just for classes but also for modules, functions as well

● Better cohesion, encapsulation and less coupled design

Page 20: Evolutionary Design Solid

Better design applying SRP

Reporter App

File Reader

Report Formatter

CSV File JSON File

Requirement Changes

● Support for more file formats● May be will add more report formats (Graphical etc..)

Page 21: Evolutionary Design Solid

Open Closed Principle

Page 22: Evolutionary Design Solid

Open Closed Principle

● You should be able to extend software entities (Classes, Modules, Functions, etc.), without modifying them.

● Design smell - Change ripples through code base.

● Abstraction and polymorphism are the key.

● Chain of responsibility, Strategy pattern, Aspects...

● Separate creation from usage of objects.

● No module is 100% closed (Open/Closed/Open principle)

● Easy to lead to anticipatory design. Better to burn finger at least once.

● Service and framework interface follow OCP. Open for extension, closed for modification

Page 23: Evolutionary Design Solid

Better design applying OCP

File Reader

CSV File JSON File

File Reader Service

Reporter App(UI + Formatter)

Page 24: Evolutionary Design Solid

Liskov Substitution Principle

Page 25: Evolutionary Design Solid

Liskov Substitution Principle

Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.

Page 26: Evolutionary Design Solid

Liskov Substitution Principle

● Functions that use references to base classes must be able to use objects of derived classes without knowing it.

● Used when inheritance was a main form of reuse.

● Design smell – Type checking in client code and changing the behavior for a specific type in client.

● Due to forcing an unrelated object into an inheritance hierarchy

● Prefer composition over inheritance whenever possible

Page 27: Evolutionary Design Solid

LSP Violation

File Reader

CSV File JSON File

File Reader Service Reporter (UI + Formatter)Database

New Requirement - Adding Database as a data source for report data.Adding it to File Reader Service as it is conveniently there

Page 28: Evolutionary Design Solid

Better design confirming to LSP

File Reader

CSV File JSON File

File Reader Service

Reporter (UI + Formatter)Database Database Reader Service

Keep Database separate as it doesn't fit in File Reader Service

Page 29: Evolutionary Design Solid

Interface Segregation Principle

Page 30: Evolutionary Design Solid

Interface Segregation Principle

● Clients should not be forced to depend on interface they don't use.

● Make fine grained interfaces that are client specific or clients should not be forced to implement interfaces they don't need

● Design smell – NotImplementException thrown for some methods (which are really not needed by the client)

● Flat and non cohesive interface problem - SRP?

● Role based interface design – Intent Revealing Interfaces by Udi Dahan (IExtractReportData)

Page 31: Evolutionary Design Solid

ISP Violation

Page 32: Evolutionary Design Solid

Better design confirming to ISP

● WriteReport● ReadFile● FormatReport

ReporterReporter

FormatReport

Report Formatter

WriteReport

Report Writer

GetData

FileReaderService

GetData

DatabaseReaderServiceInitial Interface

Current Interface

Page 33: Evolutionary Design Solid

Dependency Inversion Principle

Page 34: Evolutionary Design Solid

Dependency Inversion Principle

● High level modules should not depend upon low level modules. Both should depend upon abstractions

● Abstractions should not depend upon details. Details should depend upon abstractions

● Design smell – Unable to change the choice of implementation & code untestable in isolation

● Classic - Code for an interface not for an implementation

● DI frameworks to the rescue if object graph is complex

● Less coupled design

Page 35: Evolutionary Design Solid

DIP Violation

Text Report Formatter

Reporting App

CSV File Reader

JSON File Reader

Stuck with CSV File Reader andText Report Formatter implementations !!!

Arrows are Dependency

Page 36: Evolutionary Design Solid

Fixing for DIP

Text Report Formatter

Reporting App

CSV Data Extractor

JSON Data Extractor

IFormatReport

IExtractReportData

Page 37: Evolutionary Design Solid

Design confirming to DIP

IExtractReportData

CSV Data Extractor

JSON Data Extractor

Data Extractor Service Reporter WriterDatabase Reader Service

XML Data Extractor

Reporting Application

Report Data

Page 38: Evolutionary Design Solid

Credits

● SOLID Motivational Posters, by Derick Bailey, is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

● Uncle Bob for consolidation of these principles and his books Agile Software Development: Principles, Practices and Patterns and Clean Code

● Presentation is under Creative Commons Attribution-Share Alike 2.5 India License

Page 39: Evolutionary Design Solid

About Me

● Developer in Test at Thoughtworks

● Interested in OO, DDD, Functional Programming languages, Agile, Lean, TDD, Mocks, Automated Testing, ATDD.....

● Author and maintainer of ChromeWatir and FireDriver

● Places to find me -

● http://developer-in-test.blogspot.com● http://twitter.com/sai_venkat● http://github.com/saivenkat

Page 40: Evolutionary Design Solid

Thank you