Tips & Tricks

Write Object-Oriented TypeScript: Encapsulation

This is part 3 of a 4 part article on how to program in TypeScript using object-oriented techniques. If you are just starting with TypeScript and WebStorm, see our blog post on Getting Started with TypeScript.

Implement encapsulation in TypeScript

A key aspect of object-oriented programming, encapsulation enables you to perform what’s called “data hiding”. It’s necessary to hide certain data so that it’s not changed accidentally or purposefully by other components or code in the program. For example, you don’t want any code from outside of the CheckingAccount class calling its private methods directly. But the data that is inside the CheckingAccount class can and should be changed by other code in the CheckingAccount class itself. If encapsulated, the data can be exposed thoughtfully to other classes through methods and properties that enforce business rules.

To achieve encapsulation, use the proper access modifiers combined with the proper member types to limit or expose the scope of data. Access modifiers are markers on code that designate where that code can be used, and are as follows:
publicprivate, and protected. A public field does not encapsulate since the data can be changed by any calling code at any time.

The following code sample shows that the minimumBalance a simple public field. This means that both the BankAccount class and any code that creates instances of the BankAccount class can modify the minimumBalance. When the consuming code can modify the members directly, then accounts may contain less money than is allowed, therefore breaking the bank’s business rules (i.e., less than the minimum balance). On the other hand, the _balance field is properly encapsulated because it is private. Only code within the BankAccount class can modify it. Calling code may only read it through the balance getter method. As members of BankAccount, deposit and withdraw modify the balance from within the BankAccount class, and that imposes business rules correctly, producing the desired behavior through encapsulation.

class BankAccount {

  public minimumBalance: number;

  get balance(): number {
    return this._balance;
  }
  private _balance: number;

  deposit(amount: number): number {
    this._balance = this._balance + amount;
    return this._balance;
  }

  withdraw(amount: number): number {
    if (amount <= this.minimumBalance) {
      this._balance = this._balance - amount;
      return this._balance;
    }
  }
}

Encapsulate the minimumBalance field by using the Create Getter and Setter intention in WebStorm. Press Alt + Enter on the member and select one of the available options. Additionally, WebStorm will convert the public member to private for internal use only, or protected for access by derived classes only.

Create getter and setter using an intention action

As you can see, the developer can change certain parts of the code without causing bugs in other parts, since the data is encapsulated. This increases the validity and integrity of the of data.

Summary

Encapsulation enables you to develop robust software because you set the appropriate access boundaries to class data from calling code. It gives you better control of class attributes and methods, and the option to make certain members read or write only. This fine-tuning of access to class data helps to ensure that your code is of higher quality and easier to maintain.

Part 1: Write Object-Oriented TypeScript: Inheritance
Part 2: Write Object-Oriented TypeScript: Abstraction
[This article] Part 3: Write Object-Oriented TypeScript: Encapsulation

image description