Avatar
Dart Vader
9f9fda7f64317a17c9293f1ef06879e2e99ead7de18cd56b8af01dfde5e82a43
Fox Vector Local
Replying to Avatar Fox Vector

The Adapter Pattern in TypeScript

### What is the Adapter Pattern?

The Adapter Pattern is a structural design pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between two interfaces, enabling integration without modifying existing code.

In simple terms: **it adapts one interface to another**.

### Real-World Analogy

Imagine you have a U.S. laptop charger and you travel to Europe. The charger plug won't fit into the European socket. You need a **plug adapter** to convert the U.S. plug into a European-compatible one. The charger stays the same, but the adapter allows it to work in a new context.

## When to Use the Adapter Pattern

- You want to use an existing class but its interface doesn't match your needs.

- You want to create a reusable class that cooperates with classes of incompatible interfaces.

- You need to integrate third-party APIs or legacy systems with your application.

## Implementing the Adapter Pattern in TypeScript

Let’s go through a practical example.

### Scenario

Suppose you’re developing a payment system. You already have a `PaymentProcessor` interface that your application uses. Now, you want to integrate a third-party payment gateway with a different method signature.

### Step 1: Define the Target Interface

```javascript

ts

CopyEdit// The interface your application expects

interface PaymentProcessor {

pay(amount: number): void;

}

```

### Step 2: Create an Adaptee (incompatible class)

```javascript

ts

CopyEdit// A third-party library with a different method

class ThirdPartyPaymentGateway {

makePayment(amountInCents: number): void {

console.log(`Payment of $${amountInCents / 100} processed via third-party gateway.`);

}

}

```

### Step 3: Implement the Adapter

```javascript

ts

CopyEdit// Adapter makes the third-party class compatible with PaymentProcessor

class PaymentAdapter implements PaymentProcessor {

private gateway: ThirdPartyPaymentGateway;

constructor(gateway: ThirdPartyPaymentGateway) {

this.gateway = gateway;

}

pay(amount: number): void {

const amountInCents = amount * 100;

this.gateway.makePayment(amountInCents);

}

}

```

### Step 4: Use the Adapter in Client Code

```javascript

ts

CopyEditconst thirdPartyGateway = new ThirdPartyPaymentGateway();

const adapter: PaymentProcessor = new PaymentAdapter(thirdPartyGateway);

// Application uses a standard interface

adapter.pay(25); // Output: Payment of $25 processed via third-party gateway.

```

## Advantages of the Adapter Pattern

- **Decouples code** from third-party implementations.

- **Promotes code reuse** by adapting existing components.

- **Improves maintainability** when dealing with legacy systems or libraries.

## Class Adapter vs Object Adapter

In languages like TypeScript, which do not support multiple inheritance, the **object adapter** approach (shown above) is preferred. However, in classical OOP languages like C++, you may also see **class adapters**, which rely on inheritance.

## Conclusion

The Adapter Pattern is a powerful tool in your design pattern arsenal, especially when dealing with incompatible interfaces. In TypeScript, it helps integrate third-party APIs and legacy systems seamlessly, keeping your code clean and extensible.

By learning and applying the Adapter Pattern, you can make your applications more robust and flexible—ready to adapt to ever-changing requirements.

https://fox.layer3.press/articles/cdd71195-62a4-420b-9e24-e23d78b27452

wer234523452345

Replying to Avatar Joker

🌍 Too Young. Too Idealistic. Too Alive.

A message came in recently that stopped me cold.

It was from someone young—16 years old—but you’d never guess it from the depth of what they wrote. They spoke of having dreams so big they scare people. They’d had a spiritual awakening at 14, but instead of being nurtured, it was dismissed. Surrounded by people dulled by bitterness and fear, they were told to be realistic. To grow up. To face “reality.”

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

And that reality, to them, looked like a life that doesn’t feel like living at all.

They wrote that their biggest fear wasn’t failure—it was *settling*. Dimming their fire. Growing into someone they never chose to be.

And that—more than anything—scared them.

They told me that my book, *I Don’t Want to Grow Up,* brought them to tears because it validated what they already knew to be true. That they’re not alone. That it’s okay to want something different. That it’s okay to feel everything.

It’s messages like this that remind me why I write.

As many of you know, I include my personal email address at the back of all my books. And I read—and respond to—every single message that comes in. Whether it’s a few sentences or a life story, I see them all. And again and again, I’m reminded: there are so many of us out here quietly carrying the same truth.

Maybe you’ve felt the same. Maybe you still do.

Maybe you’ve been told your dreams are too big, too unrealistic. Maybe people around you—people who love you—try to shrink them down to something more “manageable.” Maybe they call it protection. Maybe they call it love.

But it feels like fear.

The path you wish to walk might be lonelier at first.

It might not make sense to the people around you.

But if it lights you up—*follow it*.

Because when you do, you give silent permission to others to do the same. You become living proof that another kind of life is possible. And that’s how we build a better world.

So to the person who wrote to me—and to every soul who feels the same way:

Keep going. Keep dreaming. Keep *burning*.

You are not too young. You are not too idealistic.

You are just deeply, *radically* alive.

And that is not a problem. That is a gift.

** If this speaks to you, my book *I Don’t Want to Grow Up* was written for this very reason—to remind you that your wildness is sacred, your truth is valid, and you’re not alone.**

*Paperback/Kindle/Audiobook available here: [scottstillmanblog.com](http://scottstillmanblog.com)*

[]()

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

https://connect-test.layer3.press/articles/041a2dc8-5c42-4895-86ec-bc166ac0d315

nice!

Hands On System Design with "Distributed Systems Implementation - 254-Lesson’s curriculum"

Table of content

https://stas.layer3.press/articles/2de9fde0-4c2a-4250-875b-19369b0017f5

Spring eats, unstacked

Sarah Stanback-Young curates recipes for the new season

https://stas.layer3.press/articles/7b9c4aef-406d-4797-ba06-16858d998961

From the temple to the garden

Participating in the birth of a new media system

https://stas.layer3.press/articles/41a8895a-139e-4fcd-beb9-c83ed94e07db

Stacked diffs and tooling at Meta with Tomas Reimers

Tomas Reimers, ex-Meta engineer and Graphite co-founder, shares how Meta’s custom developer tools pioneered practices like stacked diffs and monorepos—shaping industry standards and the future of AI-assisted coding.

https://stas.layer3.press/articles/ec0b310c-04c4-48ab-95b0-3be9c1b864fe

Stacked diffs and tooling at Meta with Tomas Reimers

Tomas Reimers, ex-Meta engineer and Graphite co-founder, shares how Meta’s custom developer tools pioneered practices like stacked diffs and monorepos—shaping industry standards and the future of AI-assisted coding.

https://stas.layer3.press/articles/ec0b310c-04c4-48ab-95b0-3be9c1b864fe

War plans leaked in national security nightmare, Trump clueless, Fox defends

The David Pakman Show - March 26, 2025

https://stas.layer3.press/articles/37c8da99-1ea0-4508-b3db-28b09152244d

Trump's attack on the American mind

Behind his closure of the Education Department and his assault on higher education, science, libraries, and museums lie the oligarchs of the techno-state.

https://connect-test.layer3.press/articles/4baf3d4d-f1b3-45c3-99f3-ab761920c9ce

With Nostr, Jack is well ahead of what Elon and Zuck have learned about censorship

https://connect-test.layer3.press/articles/a001f7dc-82d1-4a84-8853-a0ff699db672