Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy or Adapter pattern?

I want to create some classes of each Virtual Server Provider, for example:

  • Digital Ocean
  • Linode
  • Amazon AWS

Each Provider has own PHP class (via composer) to use their API interface, I want to use their class library but I want to make sure I can use same method for each provider. For example of shutting down VPS:

  • Linode API method: powerOff()
  • Digital Ocean API method: haltServer()

Rather than using powerOff() and haltServer() - I want to use shutdown() method for any providers classes I will create. Should I use Strategy design or Adaptor pattern?

like image 848
I'll-Be-Back Avatar asked Jan 09 '17 22:01

I'll-Be-Back


2 Answers

Should I use Strategy design or Adaptor pattern?

This is the classic mistake everyone makes when designing an application (including myself). You should ideally not skim through the list of available design patterns and pick one that best matches your problem. Instead, you should come up with an initial class design and then try to identify the pattern that best describes your design. This safeguards you from over-designing i.e, creating unnecessary components which you don't otherwise require. With time, you soon have a vocabulary of design patterns that you have actually used rather than an application that tries to use a particular design pattern.

Leaving design patterns aside, it looks like what you are searching for is a way to provide a common interface for performing the same functionality using different underlying libraries/approaches. This sounds a lot like Abstraction and Delegation.

You can achieve Abstraction by defining a common interface called Provider with the standard operation methods such as shutdown, connect, retry, etc. You can then create one concrete provider class for each type of provider such as AWSProvider and LinodeProvider and implement the shutdown, connect and retry methods. You then use Delegation by calling the provider specific APIs within these methods. For example, call the powerOff method inside shutdown method of LinodeProvider class.

If you now take a good look at your design, you will start to realize that it looks like the Strategy as well as the Aadpter pattern. What distinguishes the two patterns is the point of time where the Abstraction comes into play. If you decide the Provider implementation to be used via a Factory at runtime of the application, you are using the Strategy pattern; however, if this decision is made at compile time, you are using the Adapter pattern. Other than the name of the pattern, your classes will pretty much look the same.

The advantage of this approach is that you have not only identified the right pattern, but you have also safeguarded yourself from over-designing the application by using patterns such as the Command pattern.

like image 189
Chetan Kinger Avatar answered Sep 20 '22 20:09

Chetan Kinger


Answer to your question: Adapter,
Strategy is used when you can have one or more implemenations of the algortihm.

p.s. suggestion with Command pattern is OK as well.

like image 44
dstar55 Avatar answered Sep 22 '22 20:09

dstar55