object oriented design solid principles

44
OBJECT ORIENTED DESIGN : BASIC PRINCIPLES PRESENTED BY : M. M.AL-FAROOQUE SHUBHO http://bd.linkedin.com/in/thisisshubho

Upload: rainynovember12

Post on 05-Dec-2014

7.341 views

Category:

Documents


5 download

DESCRIPTION

Object Oriented Design - Discussion on basic principles

TRANSCRIPT

Page 1: Object Oriented Design SOLID Principles

OBJECT ORIENTED DESIGN : BASIC PRINCIPLES

PRESENTED BY : M. M.AL-FAROOQUE SHUBHO

http://bd.linkedin.com/in/thisisshubho

Page 2: Object Oriented Design SOLID Principles

A classic quote

“ Walking on water and developing software from a specification are easy if both are frozen. “ - Edward V. Berard

Page 3: Object Oriented Design SOLID Principles

How a typical development is carried out?

We Get and analyze requirement. We develop Some typical layers (DAL,

BLL, UI etc.). We Implement each functional

requirement in each layer (From UI towards data storage) as is expected.

We test and deliver.

Page 4: Object Oriented Design SOLID Principles

What do we do if there is a change?

We re-factor codes, and, re-factor a lot.

And, what if we are asked to change again?

We re-factor again!

Page 5: Object Oriented Design SOLID Principles

Why do we need to re-factor?

Because, our codes are not developed in a “Flexible”, “Re-usable” and “Extensible” manner.

Page 6: Object Oriented Design SOLID Principles

What is the universal truth of software world?

“Your software is bound to change”

Why?

Because, your software solves a real life business problem, and, the real life business process evolves and changes - always.

Your software does what it is supposed to do today and does it good enough. But, is this smart enough to support “Change”?

If not, you don’t have a smartly ddsigned software.

Page 7: Object Oriented Design SOLID Principles

What is a “Smartly designed software?

If your software can absorb changes in minimal effort and re-factor, you have a smartly designed software.

Your software does what it is supposed to do today and does it good enough. But, is this smart enough to support “Change”?

If not, you don’t have a smartly developed software.

A smartly developed software can adjust change easily, it can be extended and it is re-usable.

Page 8: Object Oriented Design SOLID Principles

What defines customer satisfaction?

You may satisfy your customer for the time being with a software that does what it is supposed to do only.

But, “Change” is the most obvious thing in business and when the software requires the change and you can’t implement that easily, you are in risk of losing the customer.

Page 9: Object Oriented Design SOLID Principles

What defines customer satisfaction?...continued

So, to retain long running and growing business, you need to have satisfied customers. You have to understand How they are satisfied.

Customers are satisfied if your software:

Works, Keeps working (Important), Can be upgraded easily, Can be re-used to build other software, Flexible.

Page 10: Object Oriented Design SOLID Principles

How to design/develop your software to satisfy your customers?

Design your software that meets the criteria mentioned already.

How to achieve such design?

Apply Object Oriented Design (OOD) to develop your software.

Page 11: Object Oriented Design SOLID Principles

Wait a minute!

“You are talking about Object Oriented Design. But, that’s what we do, no? We use classes and objects everywhere. So, what is the big deal?”

OK, using classes and objects doesn’t mean that you are really using an Object Oriented Design.

Page 12: Object Oriented Design SOLID Principles

When your design is Object Oriented? You are doing Object Oriented Design if your

codes are:

Of course, Object Oriented. Re-usable, Can be changed with minimal effort. Can be extended without changing existing

codes.

Page 13: Object Oriented Design SOLID Principles

You are not alone Many people have strived already to achieve

good Object Oriented Design.

They already have identified some basic principles for doing Object Oriented Designs (Some basic inspirations).

They also have identified some common design patterns that are applicable to some common scenario (Based upon the basic principles).

Page 14: Object Oriented Design SOLID Principles

Object Oriented Design : Basic principles

There are many design principles out there, but, at the basic level, there are 5 principles, which are abbreviated as the SOLID principles.

S = Single Responsibility Principle O = Opened Closed Principle L = Liscov Substitution Principle I = Interface Segregation Principle D = Dependency Inversion Principle

Page 15: Object Oriented Design SOLID Principles

Single Responsibility principle

Page 16: Object Oriented Design SOLID Principles

Single Responsibility principle : Definition

“THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.”

Or, differently said,

A class should have one and only one type of responsibility.

Page 17: Object Oriented Design SOLID Principles

Single Responsibility principle : Explained

If you have a class that has more than one reason to change, you need to split the class into multiple classes, based upon their responsibility.

(Note : That doesn’t mean that, multiple methods cannot be there in a single class. That means, methods in a class should be implemented with a single purpose)

Why splitting is important? Because:

--Each responsibility is an axis of change. --Codes become coupled if classes have more than one

responsibility. So, one change effect another.

Page 18: Object Oriented Design SOLID Principles

Single Responsibility principle : Example Consider the following Rectangle class

Two applications are using this Rectangle class:

-Computational Geometry Application uses this class to calculate the Area

-Graphical Application uses this class to draw a Rectangle in the UI

Page 19: Object Oriented Design SOLID Principles

This violates SRP design principle as the Rectangle class has two responsibilities:

--It calculates the value of the Rectangular area. --It renders the Rectangle in the UI.

So, what is the problem?

--We must include the GUI in the computational geometry application. While deployment of the geometry application, we must include GUI library.

--A change to the Rectangle class for Graphical application may lead to change, build and test the Computational geometry application.

Single Responsibility principle : Example

Page 20: Object Oriented Design SOLID Principles

Single Responsibility principle : Example

So, what to do?

Separate the responsibilities into two different classes, such as:

Rectangle : Defines the area() method.

RectangleUI : Inherits the Rectangle class and Defines the Draw() method.

Page 21: Object Oriented Design SOLID Principles

Open-Closed Principle

Page 22: Object Oriented Design SOLID Principles

Open-Closed Principle “SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.)

SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.”

At the most basic level, that means : You should be able to extend a classes behavior, without modifying it.

1. “Open For Extension” :

This means that the behavior of the module/class can be extended and we can make the module behave in new and different ways as the requirements changes, or to meet the needs of new applications.

2. “Closed for Modification” :

The source code of such a module is inviolate. No one is allowed to make source code changes to it. It would seem that these two

Page 23: Object Oriented Design SOLID Principles

Open-Closed Principle : Explained Following is an example that doesn’t support the

Open-Closed principle

In this example, the Client and Server classes are concrete.

So, if for any reason, the Server implementation/class is changed, the Client also needs change.

Page 24: Object Oriented Design SOLID Principles

Open-Closed Principle : Explained Following is the corrected example that supports the Open-Closed principle

In this example, there is an Abstract Server class added and client holds reference to the Abstract class.

The Concrete Server class implements the Abstract Server class.

So, if for any reason, the Server implementation is changed, the Client is likely not to require any change.

The Abstract Server class here is closed for modification and the Concrete class implementations here are Open for extension.

Page 25: Object Oriented Design SOLID Principles

Likov's Substitution Principle

Page 26: Object Oriented Design SOLID Principles

Likov's Substitution Principle : Definition

“SUBTYPES MUST BE SUBSTITUTABLE FOR THEIR BASE TYPES”

Or, differently Said,

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

Page 27: Object Oriented Design SOLID Principles

Likov's Substitution Principle : Explanation

In basic Object Oriented Principle, “Inheritance” is usually described as an "is a" relationship.

If a “Developer” is a “SoftwareProfessional”, then, the “Developer” class should inherit the “SoftwareProfessional” class.

Such “Is a” relationship is very important in class designs, but, its easy to get carried away and end up in wrong design with bad inheritance.

The “Likov's Substitution Principle” is a way of ensuring that inheritance is used correctly.

Page 28: Object Oriented Design SOLID Principles

Likov's Substitution Principle : Example Take a look at the following class hierarchy

example:

Here, the KingFisher class extends the Bird base class and hence, inherits the Fly() method.

Page 29: Object Oriented Design SOLID Principles

Likov's Substitution Principle : Example continued Now take a look at the following example:

Ostrich is also a Bird (Definitely it is!) and hence it inherits the Bird class. Now, can it fly? No! Here the design violates the LSP.

So, even if in real world this seems natural, in the class design, Ostrich should not inherit the Bird class and there should be a separate class for birds that can’t really fly and Ostrich should inherit that.

Page 30: Object Oriented Design SOLID Principles

Likov's Substitution Principle : Example continued

Why The LSP is so important?

1. If LSP is not maintained, class hierarchies would be a mess and if subclass instance was passed as parameter to methods method, strange behavior might occur.

2. If LSP is not maintained, unit tests for the Base classes would never succeed for the subclass.

Page 31: Object Oriented Design SOLID Principles

The Interface segregation principle

Page 32: Object Oriented Design SOLID Principles

The Interface segregation principle : Defined

“CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE.”

Page 33: Object Oriented Design SOLID Principles

The Interface segregation principle : Explained

Interfaces with too many methods are less re-usable.

Such "fat interfaces“ with additional useless methods lead to inadvertent coupling between classes.

Doing this also introduce unnecessary complexity and reduces maintainability or robustness in the system.

The ISP ensures that, Interfaces are developed so that, each of them have their own responsibility and thus they are re-usable.

Page 34: Object Oriented Design SOLID Principles

The Interface segregation principle : Example

The following interface is a “Fat interface” which violates the Interface Segregation principle

Note that, the IBird Interface have many bird behaviour defined along with the Fly() behaviour.

Now, if a Bird class (Say, Ostrich) implements this interface, it has to implement the Fly() behaviour unnecessarily (Because, Ostrich doesn’t fly)

Page 35: Object Oriented Design SOLID Principles

The Interface segregation principle : Example continued..

The “Fat Interface” is broken down into two different interfaces IBird and IFlyingBird where the IFlyingBird inherits the IBird.

If there is a bird that can’t fly, (Say, Ostrich), they would implement the IBird interface.

And, if there is a bird that can fly (Say, KingFisher), they would implement the IFlyingBird interface.

Page 36: Object Oriented Design SOLID Principles

The Dependency Inversion Principle

Page 37: Object Oriented Design SOLID Principles

The Dependency Inversion Principle : Defined

“HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS.”

Page 38: Object Oriented Design SOLID Principles

The Dependency Inversion Principle : Explained

Lets consider a real world example

Your car is composed of lots of objects like the Engine, the wheels, the Air Conditioner and others things.

None of these things are rigidly built within a single thing, rather, each of these things are “Pluggable” so that,

When the Engine, or the wheel has problem, you can repair it (Without repairing other things) and you can replace it.

While replacement, you just have to ensure that, the Engine/Wheel conforms to the car’s design (Say, the card would accept any 1500 cc engine and will run on any 18 inch wheel)

Also, the car might allow you to put a 2000 CC engine in place of the 1500 CC given the fact that, the manufacturer (Say, Toyota) is the same.

Page 39: Object Oriented Design SOLID Principles

The Dependency Inversion Principle : Explained

What if the different parts of your car were not built in such “Pluggable nature”

That would be horrible!

In that case, if your car’s engine was out of order, you had to fix the whole car or purchase a new one!

Page 40: Object Oriented Design SOLID Principles

The Dependency Inversion Principle : Explained

How the “Pluggable nature” is to be achieved?

“Abstraction” is the key.

In the real world, the Car is the Higher level module/entity and it depends on the Lower level modules/entities like the Engines or Wheels.

But, rather than directly depending on the Engines or Wheels, the car depends on the Abstraction of some specification of Engine or Wheels so that, if any Engine or Wheel conforms to the abstraction, these could be assembled with the car and the card would run.

Page 41: Object Oriented Design SOLID Principles

The Dependency Inversion Principle : Example

In the above Car class, notice that, there are two properties and both of these are of abstract type (Interface) , rather than concrete type.

So, the Engine and Wheels are pluggable because, the card would accept any object implementing the declared interfaces and that will not require any change in the Car class.

Page 42: Object Oriented Design SOLID Principles

The Dependency Inversion Principle : Benefits

If Dependency inversion is not implemented in the code, you run the risk of

--Damaging the Higher level codes that uses the lower level classes.

--Requiring too much time and effort to change and higher level codes when a change is occurred in the lower level classes.

--Producing less-reusable codes.

Page 43: Object Oriented Design SOLID Principles

Other principles There are many other Object Oriented Principles, other

than the SOLID principle. Some are:

“Composition over Inheritance” : Favor composition over inheritance.

“Principle of least knowledge” : The less your class knows, the better.

“The Common Closure Principle” : Related classes should be package together

“The Stable Abstractions Principle” : The more stable a class is, the more it must consist of abstract class.

Page 44: Object Oriented Design SOLID Principles

Design patterns

The design patterns (Commonly suggested design suggestions in common recurring situation) are mainly inspired by the Object Oriented Design principles.

The Patterns can be thought of “Frameworks” and the OOD principles can be thought of “Specifications”

Suggested Study : Design Patterns