Member-only story
Prepare For A Coding Tech Interview Part 10— Adapter And Decorator
Do you want to get a job as a coder / software developer or switch to other company? In this series I’ll share information how to solve coding tasks, how to prepare CV and answers to the most popular interview questions for coding positions to land a new coding job!
Hi! My name is Tom Smykowski. I’ve interviewed hundreds of people and also participated in hundreds of interviews to choose companies I worked for. This gives me an insight into how to win a tech interview
In last episodes of the series we explored three introductory design patterns and strategy pattern. Today we’ll learn about the adapter and decorator patterns. These are quite easy.
Adapter
An adapter adapts a feature to a format you want to interact with. It’s useful for example when you work with 3rd party API or components.
Let’s say you use a service to send SMS, but you don’t want to be tied to payload format of that specific provider. So you write an adapter:
class TwixioAdapter implements SmsService {
constructor(private legacySms: TwixioSmsService) {}
sendSms(to: string, message: string): Promise<void> {
return this.legacySms.triggerText({ phoneNumber: to, body: message });
}
}