Open/Closed Principle of SOLID (BPD)

Today, we will explore the Open/Closed Principle (OCP) from SOLID, which was introduced by Bertrand Meyer in 1988. The principle states:

A software artifact should be open for extension but closed for modification.

Following our goal to Be a Professional Developer, understanding and applying this principle is crucial. According to Robert C. Martin, the second principle of SOLID is considered the most important in object-oriented design. He also stated:

A good software architecture should minimize the amount of code that needs to be changed. Ideally, changes should be minimal or even zero.

Open/Closed Principle

Imagine we have differents databases and we need to retrieve the current time from each of them. We could create something like this:

export class CurrentTime {
    getFromMySQL(): string { return 'SELECT SYSDATE()' }
    getFromPostgreSQL(): string { return 'SELECT CURRENT_TIMESTAMP' }
    getFromSqlServer(): string { return 'SELECT GETDATE()' }
    getFromMongoDB(): string { return 'new Date()' }    
}

Simple, yet dangerours…

Suppose we later update PostgreSQL to a new version, which introduces a new syntax. For example, the new query might be:

SELECT clock_timestamp()

However, with 1,000 clients using the old version of PostgreSQL, directly updating the code could break compatibility for those clients.

Remembering:

A good software architecture should minimize the amount of code that needs to be changed. Ideally, changes should be minimal or even zero.

So in this case a better approach would be:

export interface DatabaseCode {
    getCurrentTime(): string;
}
import { DatabaseCode } from "./database-code";

export class MySqlV5_6 implements DatabaseCode {
    getCurrentTime(): string { return "SELECT SYSDATE()" }
}

export class PostgresqlV8_0 implements DatabaseCode {
    getCurrentTime(): string { return "SELECT NOW()" }
}

export class SqlServerV7_0 implements DatabaseCode {
    getCurrentTime(): string { return "SELECT GETDATE()" }
}

export class MongoDbV3_0 implements DatabaseCode {
    getCurrentTime(): string { return "new Date()" }
}

And now we can create a new class with a new version from PostgreSQL:

export class PostgresqlV9_1 implements DatabaseCode {
    getCurrentTime(): string { return "SELECT clock_timestamp()" }
}

 

Why?

By following the Open/Closed Principle, we ensure that our code is open for extension but closed for modification. It’s riskier to maintain this code together. Using this approach, we change nothing, thereby adhering to the principle that: “changes should be minimal or even zero”.

SOLID

Now we finished the second principle from SOLID and the next article we will see the Liskov Substitution Principle.

[x] – Single Responsibility Principle (SRP)

[x]Open-Closed Principle (OCP)

[  ] – Liskov Substitution Principle (LSP)

[  ] – Interface Segregation Principle (ISP)

[  ] – Dependency Inversion Principle (DIP)

 

Se you soon, bye 🙂

Code

Previous Article

#3 – Single Responsibility Principle of SOLID (BPD)

Leave a Reply