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条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站