Software design principles every developers should know

Software design principles every developers should know

Some of these Design Principles are,

  1. “Encapsulate what varies”
  2. “Favor composition”
  3. “Program to interfaces”
  4. “Loose coupling”
  5. SOLID principles

Design Patterns vs Design Principles

Design Principles are general guidelines that can guide your class structure and relationships. On the other hand, Design Patterns are proven solutions that solve commonly reoccurring problems. Having said that, most of the practical implementations of these design principles are mostly done using one or more design patterns.

1. “Encapsulate what varies”

Considered as the foundational design principles, this principle is found at work in almost all design patterns.

public void processServiceRequest(String serviceType) {

OnlineService service;

if(service.equals("AirConditioner"))

service = new ACService();

else if(service.equals("WashingMachine"))

service = new WMService();

else if(service.equals("Refrigerator"))

service = new RFService();

else

service = new GeneralService();

service.getinfo();

service.assignServiceRequest();

service.assignAgent();

service.processRequest();

}

public class OnlineServiceFactory {

public OnlineService getOnlineService(String type) {

OnlineService service;

if(service.equals("AirConditioner"))

service = new ACService();

else if(service.equals("WashingMachine"))

service = new WMService();

else if(service.equals("Refrigerator"))

service = new RFService();

else

service = new GeneralService();

return service;

}

}

public void processServiceRequest(String serviceType) {

OnlineService service = new OnlineServiceFactory().getOnlineService(serviceType);

service.getinfo();

service.assignServiceRequest();

service.assignAgent();

service.processRequest();

}

2. “Favor Composition over Inheritance”

Object oriented programming provides 2 types of relationships between classes and its instances. has-a (composition) and is-a (inheritance). This design principle essentially suggests us that “has-a relationship should be preferred over is-a relationship”.

3. “Loose Coupling”

Loose coupling is a principle which suggests that “components interacting with each others should be independent of each others, relying on the knowledge of other component as little as possible”.

4. “Program to interfaces, Not Implementations”

This design principle guides us to make use of abstract types not concrete ones. “Program to interfaces” actually mean program to a super type like an interface or abstract class in java. We are implementing polymorphism by programming to a super type so that the actual runtime object isn’t locked into your code.

ServiceClass service = new ServiceClass();

if(dbType == "POSTGRES") {

service.db = new PostgresDBClient();

} else if(dbType == "MYSQL") {

service.db = new MySQLDBClient();

}

5. SOLID Principles

SOLID is a set of 5 principles which forms the acronym.

  • Use composition to accept new behaviours

public class Singers {

String name;
int age;
String ratings;
String typeOfSinging;

}

public abstract class Contestants {

String name;
int age;
String ratings;

}

public class Singer implements Contestants {

String name;
int age;
String ratings;
String typeOfMusic;

public void sing() {
...
}

}

public class Dancer implements Contestants {

String name;
int age;
String ratings;
String typeOfDance;

public void dance() {
...
}

}

ClassA classA = new ClassA();
classA.doSomething(); // should work fine

ClassA = new ClassB();
classA.doSomething(); // should work fine

Conclusion:

The design principles which covered in this article are kind of the fundamental and foundational one’s. There are few more such principles like DRY, AHA, KISS which I wanted to cover but I don’t feel could be explained in more specific way since they are mostly situational and varies based on the problems we are trying to solve.

  • AHA stands for Avoid Hasty Abstractions which basically suggests us that sometimes abstractions can lead to a rigid codebase. And when it does, its better to avoid it.
  • KISS stands for Keep It Simple Stupid which as the acronym suggests that our implementation should be as simple as possible. Anything introducing complications in the implementation should be decoupled using any of the above principles.

Source: