Facade Design Pattern

The Facade Pattern provides a simplified interface to a complex software system by hiding its underlying components. It helps to reduce dependencies and improves usability.
We will discuss the benefits of this pattern by introducing a Typescript example for Encryption algorithm. We call this class as the EncryptionFacade.
The facade introduces a single-entry point to the system, hiding the complexity of multiple systems. The EncryptionFacade class provides two methods encryptData() and decryptData() instead of exposing multiple encryption classes.
The facade pattern provides loose coupling thereby decoupling the client from the system. This helps the user to avoid the complex cryptographic algorithms. The encryption logic centralised reducing duplication and making future updates easier. If encryption logic changes, only EncryptionFacade needs modification, not every client that uses encryption.
import * as crypto from 'crypto';
import { AESEncryption } from './AESEncryption';
// FACADE: Simplifies Encryption & Decryption
class EncryptionFacade {
private encryptionService = new AESEncryption();
encryptData(data: string): string {
return this.encryptionService.encrypt(data);
}
decryptData(encryptedData: string): string {
return this.encryptionService.decrypt(encryptedData);
}
}
function main() {
const facade = new EncryptionFacade();
const encryptedText = facade.encryptData("Confidential Data");
console.log("Encrypted:", encryptedText);
const decryptedText = facade.decryptData(encryptedText);
console.log("Decrypted:", decryptedText);
}
main();
The code snippet provides the details for the facade class. The user interacts using the two methods encryptData() and decryptData() without having to know the intricate details.
The actual AES encryption class is as below:
import * as crypto from 'crypto';
export class AESEncryption {
private key = crypto.createHash('sha256').update('my-secured-key').digest();
private generateIV(): Buffer {
return crypto.randomBytes(16);
}
encrypt(data: string): string {
const iv = this.generateIV();
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, iv);
let encrypted = cipher.update(data, 'utf-8', 'hex');
encrypted += cipher.final('hex');
return `${iv.toString('hex')}:${encrypted}`; // Store IV with encrypted data
}
decrypt(encryptedData: string): string {
const [ivHex, encrypted] = encryptedData.split(':');
const iv = Buffer.from(ivHex, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, iv);
let decrypted = decipher.update(encrypted, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}
}
The GITHUB link for the code is available HERE.
For the readers who are curious to know about the AES algorithm details, they can read the comments here.
Subscribe to my newsletter
Read articles from Ganesh Rama Hegde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ganesh Rama Hegde
Ganesh Rama Hegde
Passionate Developer | Code Whisperer | Innovator Hi there! I'm a senior software developer with a love for all things tech and a knack for turning complex problems into elegant, scalable solutions. Whether I'm diving deep into TypeScript, crafting seamless user experiences in React Native, or exploring the latest in cloud computing, I thrive on the thrill of bringing ideas to life through code. I’m all about creating clean, maintainable, and efficient code, with a strong focus on best practices like the SOLID principles. My work isn’t just about writing code; it’s about crafting digital experiences that resonate with users and drive impact. Beyond the code editor, I’m an advocate for continuous learning, always exploring new tools and technologies to stay ahead in this ever-evolving field. When I'm not coding, you'll find me blogging about my latest discoveries, experimenting with side projects, or contributing to open-source communities. Let's connect, share knowledge, and build something amazing together!