dongnai3960 2017-01-09 22:33
浏览 71
已采纳

策略或适配器模式?

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?

  • 写回答

3条回答 默认 最新

  • dsewbh5588 2017-01-10 11:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能