Labels

Friday, December 3, 2010

Design Principles - SOLID

What are Software Design Principles?

 

Software design principles represent a set of guidelines that helps us to avoid having a bad design. The design principles are associated to Robert Martin who gathered them in "Agile Software Development: Principles, Patterns, and Practices".

 

According to Robert Martin there are 4 important characteristics of a bad design that should be avoided. They are not orthogonal, but are related to each other in ways that will become obvious. they are:

rigidity, fragility, immobility, and viscosity.

 

These four symptoms are the tell-tale signs of poor architecture. Any application that show signs of them, is suffering from a design that is rotting from the inside out.

 

 

 

Rigidity

It is hard to change because every change affects too many other parts of the system.

 

Fragility

When you make a change, unexpected parts of the system break.

 

Immobility

Inability to reuse software from parts of the same project or from other projects.

 

It often happens that one engineer will discover that he needs a module that is similar to one that another engineer wrote. However, it also often happens that the module in question has too much baggage that it depends upon.

 

i.e. It is hard to reuse in another application because it cannot be separated from the current application.

 

Viscosity

Viscosity comes in two forms. High Viscosity is a bad design.

·         Viscosity of the design, &

·         Viscosity of the environment.

 

Avoid hacks to solve a problem.  When the hacks are easy to employ than the design preserving methods, the Viscosity Of The Design/Environment is high.

 

 

 

Single Responsibility PrincipleSRP: 

Principle - A class should have only one reason to change.

 

·         In this context a responsibility is considered to be one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes.

·         Each class will handle only one responsibility and on future if we need to make one change we are going to make it in the class which handle it.

·         When we need to make a change in a class having more responsibilities the change might affect the other functionality of the classes.

 

For example, instead of creating a class that has data access code for several different items you should instead separate the data access for each items into a seperate single class.

 

Single Responsibility Principle was introduced Tom DeMarco in his book Structured Analysis and Systems Specification, 1979.

Robert Martin reinterpreted the concept and defined the responsibility as a reason to change.

 

 

Open Close Principle - OCP

Principle - Software entities like classes, modules and functions should be open for extension but closed for modifications.

 

·         OPC is a generic principle. You can consider it when writing your classes to make sure that when you need to extend their behavior you don't have to change the class, rather extend it.

·         The same principle can be applied for modules, packages, libraries.

·         If you have a library containing a set of classes there are many reasons for which you'll prefer to extend it. This extension should happen without changing the code that was already written.

 

For example, you should be able to add a new behavior to a class without affecting the rest of the code. Instead of adding a new case to a switch statement, you should consider re-factoring the code to use separate classes for each case.

 

When referring to the classes, OCP can be ensured by use of Abstract Classes and concrete classes for implementing their behavior. This will enforce having Concrete Classes extending Abstract Classes instead of changing them. Some particular cases of this are Template Pattern & Strategy Pattern.

 

 

Liskov's Substitution Principle - LSP

Principle - Derived types must be completely substitutable for their base types.

That is, a user of a base class should continue to function properly, if a derivative of that base class is passed to it.

 

·         This principle is just an extension of the OCP in terms of behavior, - Meaning that we must make sure that new derived classes are extending the base classes without changing their behavior. The new derived classes should be able to replace the base classes without any change in the code.

 

For example: A typical example that violates LSP is a Square (Derived) class that derives from a Rectangle(Base) class. The Square class always assumes that the width is equal with the height. If a Square object is used in a context where a Rectangle is expected, unexpected behavior may occur because the dimensions of a Square cannot (or rather should not) be modified independently.

 

Liskov's Substitution Principle was introduced by Barbara Liskov in a 1987 Conference on Object Oriented Programming Systems Languages and Applications.

 

 

Interface Segregation Principle - ISP

Principle - Clients should not be forced to depend upon interfaces that they don't use.

 

·         This principle teaches us to take care how we write our interfaces.

·         When we write our interfaces we should take care to add only methods that should be there.

·         If we add methods that should not be there the classes implementing the interface will have to implement those methods as well.

 

For example if we create an interface called Worker and add a method lunch break, all the workers will have to implement it. What if the worker is a robot?

Another example - By having IEnumerable and IDisposable interfaces separate, it's possible for client code to seperately care about dealing with enumerating a collection or disposing of it. Thus not clutter up either operation by mixing two totally different kinds of behaviors.

 

As a conclusion Interfaces containing methods that are not specific to it are called Polluted or Fat Interfaces. We should avoid them.

 

 

Dependency Inversion Principle - DIP

Principle - High-level modules should not depend on low-level modules. Both should depend on abstractions and not concretions.

Abstractions should not depend on details. Details should depend on abstractions.

 

·         Dependency Inversion Principle states that we should decouple high level modules from low level modules, introducing an abstraction layer between the high level classes and low level classes. Further more it inverts the dependency: instead of writing our abstractions based on details, the we should write the details based on abstractions.

·         Dependency Inversion or Inversion of Control are better know terms referring to the way in which the dependencies are realized. In the classical way when a software module(class, framework, …) need some other module, it initializes and holds a direct reference to it. This will make the 2 modules tight coupled. In order to decouple them the first module will provide a hook(a property, parameter, …) and an external module controlling the dependencies will inject the reference to the second one.

·         By applying the Dependency Inversion the modules can be easily changed by other modules just changing the dependency module.

 

Factories and Abstract Factories can be used as dependency frameworks, but there are specialized frameworks for that, known as Inversion of Control Container.

 

Hope this helps.

 

Reference :

Design Principles

Design Principles – By Example

 

 

Thanks & Regards,

Arun Manglick

No comments:

Post a Comment