OO Principles

Few months ago I read Head First Design Patterns. In this book writers mention about different OO Principles which I think every software developer should know. In this post I am going to write down these principles for my reference but anyone can find them useful.

Encapsulate what varies

If every new requirement forces you to change your code, then it is time to pull out that part of the code and separate it from the code that doesn’t change. It helps to reduce the unintended consequences from code change and also makes the system flexible.

Favour composition over inheritance

Using composition a class can achieve polymorphic behaviour and code reuse by containing other classes. When a system is created using composition, it gets more flexibility. You can encapsulate a family of algorithms into their own set of classes and also change behaviour at runtime.

Program to interface, not implementation

“Program to an interface” really means “Program to a supertype”. What does it mean? Let’s see the below example:

Programming to an implementation (this is to code to a concrete implementation)

Dog d = new Dog();
d.bark();

Programming to an interface/supertype

public interface Animal {
   makeSound();
}

public class Dog implements Animal {
   makeSound(){
     bark();
   }
   bark(){
     //bark sound
   }
}

public class Cat implements Animal {
   makeSound(){
     meow();
   }
   meow(){
     //meow sound
   }
}
Animal animal = new Dog();
animal.makeSound();

Animal anotherAnimal = new Cat();
anotherAnimal.makeSound();

In the above example Animal is a supertype of Dog and Cat, which could be an abstract class or interface. Here variables animal and anotherAnimal can have any concrete implementation of the supertype Animal, which also means that it doesn’t have to know about the actual object types.

Strive for loosely coupled designs between objects that interact

Loosely coupled objects can interact together with having very little knowledge of each other. Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the interdependency between objects.

Classes should be open for extension but closed for modification

This is also called Open-Closed principle. Classes should be easily extended to incorporate new behaviour without modifying existing code. Using observer and decorator design pattern, this Open-Closed principle can be implemented. Though Inheritance can be used to extend an existing class, but it does not always provide flexibility. Other way to add new behaviours dynamically is to use composition and delegation. Decorator pattern provides the option to add new behaviours dynamically which is an alternative to inheritance.

Depend on abstractions. Do not depend on concrete classes

This principle tells us we should write our code that depends on abstractions, not concrete classes.

Maintain low level of coupling in the design (talk only to your friends)

When designing a system, for any object, be careful of the number of classes it interacts with and also how it comes to interact with those classes. Your system can become fragile when there is lot of dependencies between many classes. This type of system is costly to maintain and complex for others to understand.

Don’t call us, we’ll call you

This is also called Hollywood principle. This principle states that though low level components can hook themselves with high level components, they can never call high level components directly. High level components determine when and how to call low level components. Some of well known patterns like Observer and Dependency Injection follow this principle.

A class should have only one reason to change

When a class has many reasons to change, then you will see many problems can creep in due to these changes. This principle is also called Single Responsibility Principle. Always try to make a module or a class highly cohesive. High cohesive module or class is easier to maintain than a class that has multiple responsibilities and low cohesion.

Advertisements
OO Principles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s