Skip to content

OOP Refresher

Most design patterns rely on Object-Oriented Programming concepts. Here’s a quick refresher with JavaScript examples.


Keep data private. Expose only what’s needed.

Bundle data (properties) and methods together. Hide internal details.

class BankAccount {
#balance = 0; // private field
deposit(amount) {
if (amount > 0) this.#balance += amount;
}
getBalance() {
return this.#balance;
}
}
const acc = new BankAccount();
acc.deposit(100);
console.log(acc.getBalance()); // 100
// acc.#balance // ❌ Error — private!

Child classes reuse parent behavior.

class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound.`;
}
}
class Dog extends Animal {
speak() {
return `${this.name} barks!`;
}
}
const dog = new Dog('Rex');
console.log(dog.speak()); // "Rex barks!"

Same interface, different behavior.

class Bird extends Animal {
speak() {
return `${this.name} chirps!`;
}
}
const animals = [new Dog('Rex'), new Bird('Tweety')];
animals.forEach(a => console.log(a.speak()));
// "Rex barks!"
// "Tweety chirps!"

Hide complexity. Show only the essential.

class CoffeeMachine {
// Public interface
makeCoffee(type) {
this.#heatWater();
this.#grindBeans();
return `☕ ${type} ready!`;
}
// Private implementation (hidden)
#heatWater() { /* complex heating logic */ }
#grindBeans() { /* complex grinding logic */ }
}
const machine = new CoffeeMachine();
console.log(machine.makeCoffee('Latte')); // ✅
// machine.#heatWater() // ❌ Error!

OOP ConceptRelated Patterns
EncapsulationFacade, Mediator, Proxy — hide complexity
InheritanceTemplate Method, Factory Method — reuse & override
PolymorphismStrategy, State, Visitor — same call, different behavior
AbstractionAdapter, Bridge, Abstract Factory — work with interfaces, not concrete classes

  • Encapsulation: Keep internals private, expose only what’s needed
  • Inheritance: Child classes reuse parent code
  • Polymorphism: Same method name, different implementations
  • Abstraction: Hide complexity behind a simple interface
  • Design patterns use these OOP features to solve problems elegantly