PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.

Slides:



Advertisements
Similar presentations
And so to Code. Forward, Reverse, and Round-Trip Engineering Forward Engineering Reverse Engineering Round-Trip Engineering.
Advertisements

Chapter 11 Component-Level Design
Design Principles & Patterns
St Louis Day of.NET 2011 Refactoring to a SOLID Foundation Steve Bohlen Senior Software Engineer SpringSource/VMware Blog:
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
Common mistakes Basic Design Principles David Rabinowitz.
Common mistakes Basic Design Principles Amit Shabtay.
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
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,
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)
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SOLID Principles in Software Design
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
 What is SOLID  The S in SOLID  The O in SOLID  The L in SOLID  The I in SOLID  The D in SOLID  Questions.
Software Design Principles
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
Component Design Elaborating the Design Model. Component Design Translation of the architectural design into a detailed (class-based or module- based)
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.
SOLID Principles in Software Design
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
SOLID PHP & Code Smell Wrap-Up
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Andres Käver, IT Kolledž public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression.
Mantas Radzevičius ifm-2/2
OCP and Liskov Principles
Course information Old exam Resit Report Result and walkthrough
Dependency Inversion Principle
Software Design Principles
Software Architecture & Difference from Design
Copyright © by Curt Hill
Software Engineering: A Practitioner’s Approach, 6/e Chapter 11 Component-Level Design copyright © 1996, 2001, 2005 R.S. Pressman & Associates, Inc.
Factory pattern Unit of Work
Adaptive Code Umamaheswaran Senior Software Engineer
Component-Level Design
Software Design Principles
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Object Oriented Practices
15 letters that will change your code
The SOLID Principles.
A (partial) blueprint for dealing with change
European conference.
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
Dependency Inversion principle
Some principles for object oriented design
Interface Segregation Principle
5. OOP OOP © 2003 Microsoft.
Chapter 10 – Component-Level Design
Presentation transcript:

PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D

S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle  The Open Closed Principle  The Liskov Substitution Principle  The Interface Segregation Principle  The Dependency Inversion Principle

S.O.L.I.D Principles  The Open Closed Principle  The Liskov Substitution Principle  The Single Responsibility Principle  The Dependency Inversion Principle  The Interface Segregation Principle  Bringing it all together

S.O.L.I.D Principles How can SOLID help?  Drives good design  Maintenance  Refactorability  Clarity Coupling and Cohesion  We want LOW coupling  And HIGH cohesion

S.O.L.I.D Principles The Open/Closed Principle and The Liskov Substitution Principle

OCP and LSP OCP  “Classes should be open for extension but closed for modification”  Polymorphism / abstraction LSP  “Derived classes must be substitutable for their base class”  Basic polymorphism/inheritance  Has implications with covariance/contravariance

S.O.L.I.D Principles The Single Responsibility Principle

“Every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility” Achieved with  Dependency Inversion  Interface Segregation

The Single Responsibility Principle public class User { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string GenerateUpdate() { return String.Format( "UPDATE Users SET FirstName='{0}', LastName='{1}' WHERE Id={2}", FirstName, LastName, Id); } public string GenerateDelete() { return String.Format( "DELETE FROM Users WHERE Id={0}", Id); } public string GenrateInsert() { if (Id != 0) throw new InvalidOperationException( String.Format( "This user already exists with an ID of {0}", Id)); return String.Format( "INSERT INTO Users VALUES ({0},{1})", FirstName, LastName); } public bool IsValid() { return !String.IsNullOrEmpty(FirstName) && !String.IsNullOrEmpty(LastName); }

S.O.L.I.D Principles The Dependency Inversion Principle

“Depend on abstractions and not concretions” “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.”

The Dependency Inversion Principle Heavily uses  Interface based programming Achieved with  Dependency Injection

The Dependency Inversion Principle Interface based programming public string Concatenate(List parts, string delimeter) { string result = ""; foreach (var part in parts) result += part + delimeter; return result; }

The Dependency Inversion Principle Interface based programming public string Concatenate(List parts, string delimeter) { string result = ""; foreach (var part in parts) result += part + delimeter; return result; } public string Concatenate(string[] parts, string delimeter) { string result = ""; foreach (var part in parts) result += part + delimeter; return result; }

The Dependency Inversion Principle Interface based programming public string Concatenate(IList parts, string delimeter) { string result = ""; foreach (var part in parts) result += part + delimeter; return result; } public string Concatenate(string[] parts, string delimeter) { string result = ""; foreach (var part in parts) result += part + delimeter; return result; }

The Dependency Inversion Principle Interface based programming public interface IList : ICollection, IEnumerable, IEnumerable { // Methods int IndexOf(T item); void Insert(int index, T item); void RemoveAt(int index); // Properties T this[int index] { get; set; } }

The Dependency Inversion Principle Interface based programming public interface ICollection : IEnumerable, IEnumerable { // Methods void Add(T item); void Clear(); bool Contains(T item); void CopyTo(T[] array, int arrayIndex); bool Remove(T item); // Properties int Count { get; } bool IsReadOnly { get; } }

The Dependency Inversion Principle Interface based programming public string Concatenate(IEnumerable parts, string delimeter) { string result = ""; foreach (var part in parts) result += part + delimeter; return result; }

The Dependency Inversion Principle DRY Don’t Repeat Yourself!

The Dependency Inversion Principle Dependency Injection “Depend on abstractions and not concretions” Never call “new” to obtain a dependency  Inject dependencies instead

The Dependency Inversion Principle Dependency Injection public interface IRepository { IQueryable Query(); void Insert(TEntity entity); void Update(TEntity entity); void Delete(TEntity entity); } public interface IValidator { IEnumerable Validate(TEntity entity); }

The Dependency Inversion Principle Dependency Injection public class Repository : IRepository { private readonly IValidator validator; public Repository(IValidator validator) { this.validator = validator; } public void Insert(TEntity entity) { var violations = this.validator.Validate(entity); if (violations.Count() != 0) throw new ValidationException(violations); // Insert if validation passed Session.Save(entity); } // other methods }

S.O.L.I.D Principles The Interface Segregation Principle

 “Clients should not be forced to depend on interfaces that they do not use”  Fat vs Thin Interfaces  Drives low coupling  Helps create self-documenting code

The Interface Segregation Principle Example: public interface ICRUDService { TEntity Find(int id); void Insert(TEntity entity); void Update(TEntity entity) void Delete(TEntity entity); }

The Interface Segregation Principle Example: public interface ICRUDService { TEntity Find(int id); void Insert(TEntity entity); void Update(TEntity entity) void Delete(TEntity entity); }

The Interface Segregation Principle Example public class CountryService : ICRUDService { Country Find(int id) { // bla } void Insert(Country entity) { throw new NotImplementedException(); } void Update(Country entity) { // bla } void Delete(Country entity) { throw new NotImplementedException(); }

The Interface Segregation Principle Example: public interface IQueryService { TEntity Find(int id); } public interface IInsertService { void Insert(TEntity entity); }

The Interface Segregation Principle Example: public class CountryService : IQueryService, IUpdateService { Customer Get(int id) { // bla } void Update(Customer customer) { // bla }

Implementing S.R.P. To achieve SRP  Inject Dependencies by Inverting Control  Deal with abstractions not concretions by using interface based programming  Segregate interfaces into groups of concerns

Moving Forwards Using IoC Containers to help with Dependency Injection Using N-Tier architecture to create layers of concerns to aid with SRP