What is Agile Design? SRP OCP LSP DIP ISP

Slides:



Advertisements
Similar presentations
1 Object-Oriented Reengineering © R. Marinescu Lecture 5 Radu Marinescu Principles of Object-Oriented Design.
Advertisements

Since 1995, we’ve been building innovative custom solutions specifically designed to meet the unique needs of America’s most recognized companies. If you.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
Error Management with Design Contracts Karlstad University Computer Science Error Management with Design Contracts Eivind J. Nordby, Martin Blom, Anna.
17.10 Java Beans Java beans are a framework for creating components in Java. AWT and Swing packages are built within this framework Made to fit in with.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
CSc 335: Three More OO Design Principles
TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Principles of Object-Oriented Design SEI, SJTU WEB APPS and SERVICES.
OODP Prudent OO Design. OODP-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance Polymorphism.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
SOLID Principles in Software Design
Design Principles iwongu at gmail dot com.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
1 Design by Principles (Robert Martin) Software Engineering.
Principles of Object Oriented Design
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Dependency Inversion By Steve Faurie. Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin.
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
SOLID PHP & Code Smell Wrap-Up
1 OO Class Design PrinciplesStefan Kluth 3OO Class Design Principles 3.1Dependency Management 3.2The Copy Program 3.3Class Design Principles.
CSCE 240 – Intro to Software Engineering Lecture 3.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
Mantas Radzevičius ifm-2/2
Interface Segregation / Dependency Inversion
Course information Old exam Resit Report Result and walkthrough
Refactoring Architecture Patterns Design Patterns
Architecture Patterns Design Patterns
More Design Heuristics
Software Design Principles
Architecture Patterns and Refactoring
Software Architecture & Difference from Design
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Copyright © by Curt Hill
Advanced Programming Behnam Hatami Fall 2017.
Software Design Principles
Objects First with Java
lecture 08, OO Design Principle
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Decorator Pattern Richard Gesick.
Design Tips.
Object Oriented Practices
15 letters that will change your code
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Principles of Object-Oriented Design
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Software Development An overview of careers in Business Application Development Sources: Dice.com, money.CNN.com, InfoWorld.com, money.USNews.com,
Object Oriented Design & Analysis
11.1 The Concept of Abstraction
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

What is Agile Design? SRP OCP LSP DIP ISP

Agile Design What is Agile Design? SRP OCP LSP DIP ISP XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 2

What is Agile Design? Design is the structure of the system, modules, classes, methods Some aspects of it may be illustrated in UML diagrams Design commonly degrades as the system is changed Requirements do change, so the system has to change Initially sound design rots to pieces Result suffers from code smells Rigidity, Fragility, Immobility, Viscosity, Needless Complexity (YAGNI), Needless Repetition (DRY), Opacity Changes are often made simple instead of design preserving (opacity) Example: Copy program Cluttered with fixes instead of refactored to Open-Closed Principle XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 3

The Copy Program Original program Copy from keyboard to printer void Copy() { int c; while ((c=RdKbd()) != EOF) WrtPrt(c); } Change requirements Allow input to be taken from paper tape as well Allow output to go to the paper punch as well

The Modified Copy Program The modified program bool ptFlag = false; bool punchFlag = false; // remember to reset these flags void Copy() { int c; while ((c=(ptFlag ? RdPt() : RdKbd()) != EOF) punchFlag ? WrtPunch(c) : WrtPrt(c); } Design flaws Rigid (changes ripple), Fragile (breaks easily), Immobile (hard to reuse parts), Complex (all logic entangled in main loop), Redundant (replicated logic), Opaque (hard to understand) Modified requirements squeezed into original design

The Refactored Copy Program Refactoring to design standards (after first requirement change) public abstract class Reader{ abstract int read(); } public class KeyboardReader : Reader{ override int read(){ return RdKbd(); } } KeyboardReader defaultReader = new KeyboardReader(); void Copy() { Copy(defaultReader); } void Copy(Reader reader){ int c; while ((c=reader.read()) != EOF) WrtPrt(c); }

Principles and Patterns in Refactored Copy Program Open-Closed Principle (OCP) New input devices do not require source code changes any more Solution is open for extensions, but closed for modifications With respect to changes in input devices Dependency Inversion Principle (DIP) In original design, main module (policy) was dependent on low-level details Made it sensitive to changes in submodules (policy implementations) After refactoring, submodules are dependent on higher-level interface Main module uses same interface, insensible to implementation changes Strategy Design Pattern Detailed low-level implementation supplied to main module from the outside Allows change in behaviour without main module caring about details Avoid Needless Complexity (YAGNI) Wait as long as possible before refactoring Until there is a need to change output devices, don’t make a solution for it

Agile Design What is Agile Design? The Single Responsibility Principle, SRP OCP LSP DIP ISP XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 8

A Class should represent One and only One Concept It should be Coherent (all elements functionally related) It should have only one Reason to Change Anti-example: Bowling Game Responsible of both current frame and of calculating score Separate in Game to keep track of frames, and Scorer to calculate score Each responsibility is an axis of change Separating them allows changes to be made for fewer reasons Allows for less fragile design Only if that change actually occurs Typical mixtures of responsibilities Logic and user input/output, or logic and persistence control Change at different rates and for different reasons and should not be mixed

Agile Design What is Agile Design? SRP The Open-Closed Principle, OCP LSP DIP ISP XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 10

Open for Extension, Closed for Modification Software entities (classes, modules, functions, etc.) should be Open for extension It is possible to add new behaviour to it Closed for modification Extension does not cause modifications in source or binary code Make client modules work with abstractions Not implementations But only when there actually is a change (YAGNI) Resisting premature abstraction is as important as abstraction itself You may stimulate or anticipate (YAGNI risk!) the change Abstractions may be Abstract classes or interfaces Abstract methods, Template Method (”Hollywood”) Design Pattern Implemented in subclasses

Example in C#: IComparer In C#, the generic class List<T> has a static method Sort aList.Sort(aComparer) aComparer class implements IComparer<T> Sort compares pairs of elements in the list using the comparer if (aComparer.Compare(e1, e2) > 0) Swap(e1, e2); Compare returns a negative, zero or positive number indicating sorting order The comparer knows about sorting order of elements of type T The list does not The class List<T> is Open for extension of new sorting orders Closed for modification with regard to such extensions

Agile Design What is Agile Design? SRP OCP The Liskov Substitution Principle, LSP DIP ISP XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 13

How to apply inheritance and conform to the OCP? Subtypes must be substitutable for their base types public class B{ virtual void f(){...} } public class D : B{ override void f(){...} } In the client: B anObject = new D(); anObject.f(); The client will behave as if it had a B object So f in D must behave as f in B, or the client will be confused

Counter-example: Square ”is a” Rectangle public class Square : Rectangle {...} public Rectangle GimmeAShape(){...}// returns either Rectangle client getting a rectangle Rectangle r = GimmeAShape(); // gets a rectangle r.setWidth(3); r.setHeight(4); if (12 != r.getArea()) driveMeNuts(); // puh, ok Square client getting a square Rectangle r = GimmeAShape(); // gets a square r.setWidth(3); r.setHeight(4); if (12 != r.getArea()) driveMeNuts(); // arghhh, 16! A square does not behave like a rectangle So the client gets confused when square says it is a rectangle

LSP, Design by Contract, and TDD A subclass obeys the LSP if, for all overriding methods The precondition is the same or weaker than in the base class The postcondition is the same or stronger than in the base class For all cases that satisfy the original precondition from the base class Or, said differently, the contract is the same or weaker than in the base class A test case for a base class method should run unchanged for a subclass override The input conditions should satisfy the override The override should satisfy all the outcome expectations The collection of test cases implicitly defines the contract

LSP Example: Stack Abstract class Stack, with UnboundedStack, BoundedStack public abstract class Stack { public void push(Element e) {...} // Pre: !IsFull() abstract bool IsFull(); } public class UnboundedStack : Stack { override bool IsFull() {return false;} } public class BoundedStack : Stack { override bool IsFull() {return this.spaceLeft() == 0;} } Client code is independent of which kind of stack it uses Stack s = GimmeAStack(); // may return either if (!s.IsFull()) s.push(new Element()); // works with either

Agile Design What is Agile Design? SRP OCP LSP The Dependency Inversion Principle, DIP ISP XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 18

Depend on Abstractions, Not on Details High-level modules should not depend on low-level modules Both should depend on abstractions Abstractions should not depend on details Details should depent on abstractions Define an interface for services needed by a higher (policy) layer Let lower (mechanism) layers implement that interface Invert top-down dependency to bottom-up If lower-layer classes are likely to change (beware of YAGNI) The upper layer client needs to own the interface Breaks dependency on lower, implementation layers Makes the upper layer module more reusable

DIP Example: Light Switch Button, when pressed, turns on or off a lamp public class Button{ private Lamp lamp = new Lamp(); public void Poll(){ if (/*some condition*/) lamp.On(); else lamp.Off(); } } Button directly depends on lamp Cannot be used with any other device

Invert Depencencies Reuse design for other devices, like motors public interface SwitchableDevice{ void On(); void Off(); } Make both button and lamp depend on SwitchableDevice public class Lamp : SwitchableDevice{ // implement On and Off } public class Button{ private SwitchableDevice device; public Button(SwitchableDevice device) {...} public void Poll(){ if (/*some condition*/) device.On(); } } Both button and SwitchableDevice are separately reusable

Agile Design What is Agile Design? SRP OCP LSP DIP The Interface-Segregation Principle, ISP XP: eXtreme Programming, Kent Beck et al DSDM: Dynamic System Development Method, DSDM Consortium, UK Crystal: Crystal Clear and other Crystal methods by Alistair Cockburn Evo: Evolutionary development, Tom Gilb FDD: Feature Driven Development, Jeff De Luca Lean: Lean Software Development, Mary and Tom Poppendieck 22

Clients should Know about Cohesive Interfaces Fat classes cause bizarre and harmful couplings between their clients When one client forces a change on the interface, all the others are affected Avoid accumulated, fat interfaces A client should not be forced to depend on methods that it does not use Should only relate to methods it uses itself A class chould not be forced to be recompiled Because some other class causes a change in the interface unrelated to our class Separate fat interfaces in cohesive blocks Separation through delegation: Using an object adapter Separation through Multiple Inheritance

Timed Door

Separation through ”multiple inheritance”