Blog

OOP: Object-Oriented Programming

Published 1 min read

OOP :
A programming approach that treats real-world things as objects, extracts the features and functions you want from those objects, and moves the real world into a program.

OOP pros :

  1. Code becomes more reusable.
  2. It's easy to maintain.
  3. Code becomes more concise.

OOP cons :

  1. Processing takes longer.
  2. You have to invest a lot of thought and time into the design.

The 5 OOP principles S.O.L.I.D :

SRP :
A class should have only one purpose and should change for only one reason.

OCP :
It should be open to extension but closed to modification.
(Extension: extend functionality by adding new behavior, modification: you shouldn't change existing code, you should add behavior)

LSP :
Replacing an object of a supertype with a subtype should still work correctly.
(It doesn't mean it should compile correctly, it means it should work according to the program's design logic)

ISP :
Several interfaces for different purposes are better than one general-purpose interface.

DIP :
Low-level modules should depend on high-level modules.
(Low-level module: an abstract module far from IO, related to business)
(High-level module: a concrete module close to IO, related to HTTP, DB, cache, etc.)

Procedural programming vs object-oriented programming :

Procedural programming :

  1. Programming that values sequential processing, like water flowing downward
  2. It resembles the computer's processing structure, so it runs fast.
  3. If the order of the code changes, the result easily changes too.
  4. C is the typical example.

Object-oriented programming :

  1. Programming that treats real things as objects and moves the real world into code
  2. It runs slower than procedural programming.
  3. You can use techniques like polymorphism, encapsulation, and inheritance.
  4. JAVA is the typical example.

Comments

Leaving a comment stores your nickname (or GitHub profile info). Privacy policy

GitHub