OOP: Encapsulation

What

What is Encapsulation

In a class, make the variables private and all communication with other classes using public methods

Information Hiding

Divide programs into smaller pieces

Each component is responsible to manage its own state

Someone working on the rest of program does not have to be aware of inner workings of each part

No Access Modifier Description
1. Public Open to anyone
2. Protected Open to within class & its parent
3. Private Only within class

Access Modifier

In TypeScript, if not specified, the default access modifier is public.

class Car {
  public isEngineRunning: boolean;
  public speed: number;

  constructor(public name: string, 
    public make: string) {}
}

Without Encapsulation

const car = new Car('X-70', 'Proton');
car.isEngineRunning = true;
car.speed = 0;
car.speed = 10;
car.speed = 20;
car.speed = 10;
car.speed = 0;
car.isEngineRunning = false;

With encapsulation

class Car {
  private isEngineRunning: boolean;
  private speed: number;

  constructor(private name: string, private make: string) {}

  public startEngine() {
    // complex ignition systems to control start/stop engine
    this.isEngineRunning = true;
    this.speed = 0;
  }

  public stopEngine() {
    // complex ignition systems to control start/stop engine
    this.isEngineRunning = false;
    this.speed = 0;
  }

  public speedUp() {
    // complex gear box systems to control speed
    this.speed = this.speed + 10;
  }

  public speedDown() {
    // complex gear box systems to control speed
    this.speed = this.speed - 10;
  }
}
const car = new Car('X-70', 'Proton');
car.startEngine();
car.speedUp();
car.speedDown();
car.stopEngine();

But I need to access them variables.

class Car {
  private _isEngineRunning: boolean;
  private _speed: number;

  constructor(private name: string, private make: string) {}
  public startEngine() {}
  public stopEngine() {}
  public speedUp() {}
  public speedDown() {}

  public set speed(theSpeed: number) {
    if (!this._isEngineRunning) {
      throw new Error('Please start the engine first!');
    }
    if (theSpeed < 0 && theSpeed > 240) {
      throw new Error('The speed is invalid');
    }
    this._speed = theSpeed;
  }

  public get speed() {
    return this._speed;
  }
}
const car = new Car('X-70', 'Proton');
// throws an error
car.speed = 8000;

Getters & Setters

Increase Flexibility & Extensibility without effecting other code

Reduce complexity by hiding complex inner workings

Reduce bugs & accidental human errors  

Advantages

Made with Slides.com