Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOLID Principles in Software Design

Similar presentations


Presentation on theme: "SOLID Principles in Software Design"— Presentation transcript:

1 SOLID Principles in Software Design
SOLID, DRY, YAGNI, KISS Svetlin Nakov Technical Trainer Software University

2 Table of Contents SOLID Principles DRY – Don't Repeat Yourself
SRP – Single Responsibility Principle OCP – Open / Closed Principle LSP – Liskov Substitution Principle ISP – Interface Segregation Principle DIP – Dependency Inversion Principle DRY – Don't Repeat Yourself YAGNI – You Aren't Gonna Need It KISS – Keep It Simple, Stupid

3 Single Responsibility Principle

4 SRP – Single Responsibility Principle
"The Single Responsibility Principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class." Wikipedia "There should never be more than one reason for a class to change (more than one responsibility)." Robert C. Martin “Uncle Bob”

5 SRP – Single Responsibility Principle (2)
SRP is about strong cohesion and loose coupling Cohesion Relation of responsibilities Focused on single task Coupling Dependency on other modules Relationship between modules Ideal: low coupling + strong cohesion (e.g. a HDD)

6 SRP – Single Responsibility Principle (3)
"A reason to change" Mapped to project requirements More requirements  more possible changes More responsibilities  more changes in the code Multiple responsibilities in one class  coupling More coupling  more errors on change

7 Single Responsibility Principle: Violations
Classic violations of SRP Objects that can print / draw themselves Objects that can save / restore themselves Classic solutions Separate printer class Separate persistence class (or memento)

8 SRP: Solutions and Benefits
More solutions Multiple small interfaces (ISP) Many small classes Distinct responsibilities Benefits of SRP Flexible design Lower coupling Higher cohesion

9 Single Responsibility Principle
Live Demo

10 Open / Closed Principle

11 OCP – Open / Closed Principle
"The Open / Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." Wikipedia Open to extension New behavior can be added later Closed to modification Changes to source or binary code are not required

12 OCP – Open / Closed Principle (2)
Change behavior without changing the code?! Yes, this is possible, e.g. by inheritance or by parameterization Rely on abstractions, not on implementations Do not limit the variety of implementations In OO languages like C++, C#, Java and Python Use interfaces and abstract classes In procedural and functional languages Use parameters and callback functions

13 Open / Closed Principle: Violations
Classic violations of OCP Cascading changes through modules Each change requires re-testing (and possible bugs) Logic depends on conditional statements Classic solutions New classes (nothing depends on them yet) New classes (no legacy coupling)

14 Open / Closed Principle: How?
Three approaches to implement the OCP Parameters Pass delegates / callbacks Inheritance / Template Method pattern Child types override behavior of a base class Composition / Strategy patterns Client code depends on abstraction / interface "Plug in" model

15 Applying the Open / Closed Principle
When to apply OCP? Experience tell you "Fool me once, shame on you" Don't apply OCP at first If module changes once, accept it If it changes a second time, refactor for OCP OCP add complexity to design (TANSTAAFL) No design can be closed against all changes

16 Open / Closed Principle
Live Demo

17 Liskov Substitution Principle

18 LSP – Liskov Substitution Principle
"The Liskov Substitution Principle* states that subtypes must be substitutable for their base types." Agile Principles, Patterns, and Practices in C# * LSP comes from MIT professor Barbara Liskov Substitutability – child classes must not Remove base class behavior Violate base class intent

19 Liskov Substitution Principle Explained
Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Normal OOP inheritance IS-A relationship E.g. Dog is a kind of Animal Liskov Substitution inheritance IS-SUBSTITUTABLE-FOR E.g. Animal is substitutable for Dog Base Child

20 Liskov Substitution Principle: Violations
LSP problems Broken polymorphism Hidden expectations for the client code "Fixing" by adding if-then – nightmare Classic violations of LSP Base class does "type checking" to call different methods Overridden methods say "I am not implemented" Base class depends on its subtypes

21 Liskov Substitution Principle: How?
Solutions to LSP violations "Tell, Don't Ask" Don’t ask for types Tell the object what to do Refactoring to base class Common functionality Introduce third class

22 Liskov Substitution Principle
Live Demo

23 Interface Segregation Principle

24 ISP – Interface Segregation Principle
"The Interface Segregation Principle states that Clients should not be forced to depend on methods they do not use." Agile Principles, Patterns, and Practices in C# Segregate interfaces Prefer small, cohesive interfaces Divide "fat" interfaces into smaller ones

25 Interface Segregation Principle Explained
An interface is: The interface type in Java / C# All public members of a class / module Having "fat" interfaces leads to: Classes having methods they do not need Increased coupling Reduced flexibility Reduced maintainability

26 Interface Segregation Principle: Violations
Classic violations of ISP Interfaces having bad cohesion Unimplemented methods (also in LSP) Use of only small portion of a class How to fix "fat" interfaces? If the "fat" interface is yours, separate it to smaller ones If the "fat" interface is not yours, use "Adapter" pattern

27 Interface Segregation Principle: How?
Solutions to broken ISP Small interfaces Interfaces with strong cohesion Focused interfaces Let the client define interfaces Package interfaces with their implementation

28 Interface Segregation Principle
Live Demo

29 Dependency Inversion Principle

30 DIP – Dependency Inversion Principle
"Dependency Inversion Principle says that high-level modules should not depend on low-level modules. Both should depend on abstractions." "Abstractions should not depend on details. Details should depend on abstractions." Agile Principles, Patterns, and Practices in C# Goal: decoupling between modules through abstractions Programming through interfaces

31 Dependencies and Coupling
Depend on abstractions Depend directly on other modules

32 Dependencies A dependency is any external component / system:
Framework Configuration Third party library The new keyword Database Static method File system Global function Random generator Web service Console System resource (e.g. clock)

33 Dependencies in Traditional Programming
High level modules use lower lever modules E.g. UI depends on the Business Layer Business layer depends on Infrastructure, database, utilities External libraries Static methods (Façade for example) Classes are instantiated everywhere

34 DI: Depend on Abstractions
How it should be? Classes should declare what they need Constructors should require dependencies Dependencies should be abstractions How to do it Dependency Injection (DI) The Hollywood principle "Don't call us, we'll call you!"

35 Depend on Abstractions
Depend on abstractions means to work through interfaces instead directly use dependent classes

36 Dependency Inversion Principle: How?
Constructor injection Dependencies passed through constructors Pros Classes self-documenting requirements Works well without container Always valid state Cons Many parameters Some methods may not need everything

37 Constructor Injection – Example
class Copy { private IReader reader; private IWriter writer; public Copy(IReader reader, IWriter writer) this.reader = reader; this.writer = writer; } // Read / write data throght the reader / writer var copy = new Copy(new ConsoleReader(), new FileWriter("out.txt"));

38 Dependency Inversion Principle: How? (2)
Property injection Dependencies – through setters Pros Can be changed anytime Very flexible Cons Possible invalid state Less intuitive

39 Property Injection – Example
class Copy { public IReader Reader { get; set; } public IWriter Writer { get; set; } public void CopyAllChars(reader, writer) // Read / write data throght the reader / writer } Copy copy = new Copy(); copy.Reader = new ConsoleReader(); copy.Writer = new FileWriter("output.txt"); copy.CopyAllChars();

40 Dependency Inversion Principle: How? (3)
Parameter injection Dependencies – through method parameter Pros No change in rest of the class Very flexible Cons Many parameters Breaks method signature

41 Parameter Injection – Example
class Copy { public CopyAllChars(IReader reader, IWriter writer) // Read / write data throght the reader / writer } Copy copy = new Copy(); var reader = new ConsoleReader(); var writer = new FileWriter("output.txt"); copy.CopyAllChars(reader, writer);

42 Dependency Inversion Principle: Violations
Classic violations of DIP Using of the new keyword Using static methods / properties How to fix broken DIP? Extract interfaces + use constructor injection Inversion of Control (IoC) container

43 Inversion of Control and Dependency Injection
A library / framework calls your code and injects context Inversion of control container (IoC) Several ways to take dependencies when programming through interfaces (dependency injection) Constructor injection Property injection / interface injection Parameter injection

44 Inversion of Control Containers (IoC)
IoC containers Responsible for object instantiation Map interfaces to classes E.g. maps IReader to ConsoleReader Classes initiated at application start-up Interfaces are registered into the container Dependencies on interfaces are injected at runtime Examples – StructureMap, Ninject, Spring Framework and more

45 Dependency Inversion Principle
Live Demo

46 Other Principles

47 Don't Repeat Yourself (DRY)

48 Don't Repeat Yourself (DRY) Principle
"Every piece of knowledge must have a single, unambiguous representation in the system." The Pragmatic Programmer "Repetition in logic calls for abstraction. Repetition in process calls for automation." 97 Things Every Programmer Should Know DRY principle variations: Once and Only Once (OOO) Duplication Is Evil (DIE)

49 Don't Repeat Yourself (DRY): Violations
Classic violations of DRY Lots of duplicate code (typically copy-pasted) Duplicated logic in multiple locations Magic strings / values Repeated if-then logic Conditionals instead of polymorphism Static methods everywhere

50 Don't Repeat Yourself Live Demo

51 You Aren't Gonna Need It

52 YAGNI – You Aren't Gonna Need It
"A programmer should not add functionality until deemed necessary." Wikipedia "Always implement things when you actually need them, never when you just foresee that you need them." Ron Jeffries, XP co-founder

53 You Aren't Gonna Need It (YAGNI) Principle
Over-engineering (YAGNI) disadvantages Time for adding, testing, improving (costs money) Debugging, documenting, supporting May lead to adding even more features Larger and complicated software May be not know to clients Design for extension but don't implement unneeded features!

54 You Aren't Gonna Need It Live Demo

55 Keep It Simple, Stupid (KISS)

56 KISS – Keep It Simple, Stupid
"Most systems work best if they are kept simple." U.S. Navy "Simplicity should be a key goal in design and unnecessary complexity should be avoided." Wikipedia

57 KISS – Keep It Simple, Stupid: Violations
Typical violations of KISS Over-engineering (need a bike, but build an aircraft) Using design patterns when not needed Using dependency injection and IoC for simple applications Using complex framework for simple tasks Using "enterprise" framework like Java EE Typical KISS languages: PHP, Python, Ruby Typical "heavy" languages: Java, sometimes C#

58 KISS Principle Live Demo

59 SOLID Principles in Software Design
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

60 License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "High Quality Code" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

61 Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "SOLID Principles in Software Design"

Similar presentations


Ads by Google