solid principles of oo design

21
Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Upload: confiz

Post on 07-May-2015

1.509 views

Category:

Technology


1 download

DESCRIPTION

The presentation discusses software design. It discusses the characteristics of a good and bad design. Then it talks about how to achieve a good design. Then finally we discuss the SOLID Principles of Object Oriented Design. These are 5 principles compiled by Rober Cecil Martin aka Uncle Bob. The benefit of these principles is to achieve a good OO design which is high in cohesion and low in coupling thus easily adaptable to change

TRANSCRIPT

Page 1: Solid principles of oo design

Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Page 2: Solid principles of oo design

Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

SOLID Principles of OO Design Waleed Bin Dawood | Software Engineer

Page 3: Solid principles of oo design

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand" Martin Fowler

SOLID Principles of Object Oriented Design

● Software design

● What makes a design good or bad ?

● Design/Code Smells

● SOLID Principles

Waleed Bin Dawood | Software Engineer

Page 4: Solid principles of oo design

SOLID Principles

What is software design ?

Waleed Bin Dawood | Software Engineer

• The source code is the design

• UML diagram represents part of a design

• Software design process includes coding, testing,

refactoring…

• The programmer is the actual software designer.

Page 5: Solid principles of oo design

SOLID Principles

Why do we need a good design ?

Waleed Bin Dawood | Software Engineer

• To deliver fast

• To manage change easily

• To deal with complexity

Page 6: Solid principles of oo design

SOLID Principles

How to identify a bad design ?

Waleed Bin Dawood | Software Engineer

But may be we need some better criteria :)

“In my BillG review meeting, the whole

reporting hierarchy was there...and a

person...whose whole job during the

meeting was to keep an accurate

count of how many times Bill said the F

word. The lower the f***-count, the

better.”

-- Joel Spolsky, My First BillG Review

Page 7: Solid principles of oo design

SOLID Principles

How to identify a bad design ?

Waleed Bin Dawood | Software Engineer

Design/Code Smells

● Rigidity - The design is hard to change

● Fragility - The design is easy to break

● Immobility - The design is hard to reuse

● Viscosity - It is hard to do the right thing

Page 8: Solid principles of oo design

SOLID Principles

Good design

Waleed Bin Dawood | Software Engineer

How to achieve a good design ?

• Follow programming practices of your

language/framework

• Follow OO design principles

• Use design patterns

What are the characteristics of a good design ?

● High Cohesion

● Low Coupling

Page 9: Solid principles of oo design

SOLID Principles

Let’s go SOLID

Waleed Bin Dawood | Software Engineer

Initial Stands for

(acronym) Concept

S SRP Single responsibility principle

a class should have only a single responsibility.

O OCP Open/closed principle

“software entities … should be open for extension, but closed for modification”.

L LSP Liskov substitution principle

“objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”.

I ISP Interface segregation principle

“many client-specific interfaces are better than one general-purpose interface.”

D DIP Dependency inversion principle

one should “Depend upon Abstractions. Do not depend upon concretions.”

Page 10: Solid principles of oo design

SOLID Principles

Single Responsibility Principle

Waleed Bin Dawood | Software Engineer

• “There should never be more than one reason for a class

to change.” -- Robert Martin, SRP paper

• Easier : A class should concentrate on doing one thing

and one thing only

• It give you high cohesion

• It’s often hard to identify different responsibilities

Page 11: Solid principles of oo design

SOLID Principles

Single Responsibility Principle

Waleed Bin Dawood | Software Engineer

• Is SRP violated here ?

• Two responsibilities:

o Connection management

o Data Communication

Page 12: Solid principles of oo design

SOLID Principles

Single Responsibility Principle

Waleed Bin Dawood | Software Engineer

• Solution :

• Two responsibilities:

o Connection management

o Data Communication

Page 13: Solid principles of oo design

SOLID Principles

Open/Closed Principle

Waleed Bin Dawood | Software Engineer

• “Software entities should be open for extension, but

closed for modification.” -- Robert Martin paraphrasing

Bertrand Meyer, OCP Paper

• Easier : you should be able to extend the behaviour of a

module without changing it

• Abstraction is the key

Page 14: Solid principles of oo design

SOLID Principles

Open/Closed Principle

Waleed Bin Dawood | Software Engineer

• Is OCP violated here ?

Page 15: Solid principles of oo design

SOLID Principles

Open/Closed Principle

Waleed Bin Dawood | Software Engineer

• Solution:

Page 16: Solid principles of oo design

SOLID Principles

Liskov Substitution Principle

Waleed Bin Dawood | Software Engineer

• “Functions that use pointers or references to base

classes must be able to use objects of derived classes

without knowing it.” -- Robert Martin, LSP Paper

• Easier : Subclasses should behave nicely when used in

place of their parent class

Page 17: Solid principles of oo design

SOLID Principles

Liskov Substitution Principle

Waleed Bin Dawood | Software Engineer

• How would you model the relationship between a square

and a rectangle ?

• Should the square class extends rectangle ?

• Many of you would say “yes” or “why not”. Square is a

kind of Rectangle after all. An obvious IS-A relationship

• Issues in this Approach:

o Square has unnecessary attribute ‘width’

o What happens when we want to calculate area ?

o What happens if the client code is assuming height

and width to be independent of each other

Page 18: Solid principles of oo design

SOLID Principles

Dependency Inversion Principle

Waleed Bin Dawood | Software Engineer

• “A. High level modules should not depend upon low level

modules. Both should depend upon abstractions.

B. Abstractions should not depend upon details. Details

should depend upon abstractions.”-- Robert Martin, DIP

paper

• Easier:

o Use lot of interfaces (program to interface)

o Use abstractions

Page 19: Solid principles of oo design

SOLID Principles

Interface Segregation Principle

Waleed Bin Dawood | Software Engineer

• “Clients should not be forced interfaces that they do not

use.”-- Robert Martin, ISP paper

• Easier : avoid fat interfaces (cohesion)

Page 20: Solid principles of oo design

Human Computer Interaction

Thank you for your time

Waleed Bin Dawood | Software Engineer

If you have any questions,

please ask now or forever

hold your peace

Page 21: Solid principles of oo design

Human Computer Interaction

References

Waleed Bin Dawood | Software Engineer

• http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

• http://www.slideshare.net/bbossola/geecon09-solid

• http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

• http://www.slideshare.net/intellizhang/the-oo-design-principles

• http://www.slideshare.net/enbohm/solid-design-principles-9016117

• http://www.oodesign.com/design-principles.html

• http://objectmentor.com/resources/publishedArticles.html