6

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?

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213

3 Answers3

16

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.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • 3
    Amen. While one should be aware of patterns, they should not drive design decisions so much as emerge as the design takes shape. – dbugger Jan 10 '17 at 13:04
  • "What distinguishes the two patterns is the point of time where the Abstraction comes into play" - Where did you get that from? "ideally not skim through the list of available design patterns and pick one that best matches your problem" - That makes no sense at all, patterns are there to solve common problems in a proven way. – plalx Jan 10 '17 at 14:10
  • @plalx Thanks for the comment. 1. The Adapter pattern comes into play when you need to adapt an existing implementation/library to your codebase. This is something that cannot be done without writing and compiling new components/code. The goal of the Strategy pattern on the other hand is to allow you to choose the appropriate implementation (usually at runtime) without a lot of compile time changes (read factory). Thus, the point in time governs their name. 2. You don't have to agree but read [this](http://softwareengineering.stackexchange.com/questions/141854/design-patterns-do-you-use-them) – Chetan Kinger Jan 10 '17 at 15:16
  • @CKing Thanks for detailed answer, much appreciated. I have just done a quick pseudo do you can suggestion me what kind of pattern would be suited: http://pastebin.com/yYkwPpAG – I'll-Be-Back Jan 10 '17 at 21:37
  • @U'll-Be-Back I am not a php programmer but what is the purpose of the `shutdown` and `powerOn` classes?. That code should ideally be placed inside the provider implementations directly IMO. – Chetan Kinger Jan 11 '17 at 06:43
1

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.

dstar55
  • 938
  • 6
  • 11
  • Thanks for answer. I have just done a quick pseudo code so you can suggestion me what kind of pattern would be suited for that kind of logic. http://pastebin.com/yYkwPpAG – I'll-Be-Back Jan 10 '17 at 21:56
  • well I am not expert in PHP but check this example, https://sourcemaking.com/design_patterns/adapter/php, class BookAdpater is place where adaptation is implemeted – dstar55 Jan 11 '17 at 07:20
0

The best fitting design pattern here is neither Strategy nor Adaptor. In my view you should check Command pattern.

This pattern solves the exact same problem you are asking. It'll provide a single method as you asked in your case shutdown and through dependence it can call any method exposed by the dependency.

Ubercool
  • 1,029
  • 2
  • 14
  • 29
  • Even though suggest that the OP uses the `Command` pattern, your justification sounds more like the `Strategy` pattern. One of the components of the command pattern is an `Invoker`. Can you specify what would the `Invoker` be in this particular case? – Chetan Kinger Jan 10 '17 at 11:05