Spots

# Week 06 Task 02: Strategy and Adapter Patterns in JavaScript

In Week 06 Task 02, I continued learning Design Patterns in JavaScript. For this task, I implemented two important design patterns: The goal was to understand how design patterns can help make code more flexible, reusable, and easier to maintain. I also created tests using Node.js's built-in assert module to verify that the implementations worked correctly. What is the Strategy Pattern? The Strategy Pattern allows us to define multiple ways of performing an operation and choose between them at runtime. Instead of putting all the logic inside one large if-else or switch statement, we create separate strategies. For example, an online shopping application may have different discount rules: Each strategy handles its own discount calculation. I created three discount strategies: This strategy returns the original price.

For example, a 10% discount on ₹1000 gives

For example, a 10% discount on ₹1000 gives: For example, a ₹200 discount on ₹1000 gives: I created a Checkout class that uses a discount strategy. Now the discount strategy can be changed without changing the Checkout class. We can also change the strategy: Why is the Strategy Pattern Useful? Without the Strategy Pattern, we might write something like: As more discount types are added, this code becomes harder to maintain. With the Strategy Pattern, each discount algorithm has its own class. Better separation of responsibilities Runtime strategy selection What is the Adapter Pattern? The Adapter Pattern allows two incompatible interfaces to work together. A simple real-world example is a travel plug adapter. The device and socket may use different interfaces, but the adapter allows them to communicate. The same idea can be used in software.

Suppose our application expects a payment gateway with

Suppose our application expects a payment gateway with this method: But an old payment system provides: The interfaces are different. Instead of changing the entire application or rewriting the old gateway, we can create an adapter. I created a legacy payment gateway: The application expects a simpler pay() method. So I created a PaymentAdapter: The adapter converts the application's expected input into the format required by the legacy gateway. The application provides: The adapter converts it to: The application does not need to know about the legacy gateway's interface. I also created a service that works with the expected payment interface. Now the service can work with the adapter: Although both patterns help organize code, they solve different problems. I used Node.js's built-in assert module to test both patterns. The adapter correctly calls the legacy gateway.

The amount is converted correctly. The checkout service

The amount is converted correctly. The checkout service can use the adapted gateway. The final Task 02 structure was: Each file had a specific responsibility:

I learned that the Strategy Pattern allows us

I learned that the Strategy Pattern allows us to separate different algorithms into individual classes and choose the required behavior at runtime. Instead of modifying one large class whenever a new algorithm is added, we can create a new strategy.

I learned that the Adapter Pattern is useful

I learned that the Adapter Pattern is useful when we need to integrate existing code with a new system that expects a different interface. Instead of changing the existing implementation, we can place an adapter between the two systems. The main concepts I learned from this task are: Week 06 Task 02 helped me understand two more important JavaScript design patterns: Strategy and Adapter.

The Strategy Pattern showed me how to make

The Strategy Pattern showed me how to make behavior interchangeable, while the Adapter Pattern showed me how to make incompatible interfaces work together.

Implementing both patterns and writing tests helped me

Implementing both patterns and writing tests helped me understand not only what these patterns are, but also when and why they should be used.

These concepts are useful when building larger applications

These concepts are useful when building larger applications because they help keep code modular, flexible, and easier to maintain. For further actions, you may consider blocking this person and/or reporting abuse

News

# Week 06 Task 02: Strategy and Adapter Patterns in JavaScript

In Week 06 Task 02, I continued learning Design Patterns in JavaScript.

@spots #dev
Source: Dev.to
See more like this