doupo5178 2010-10-15 09:40
浏览 59
已采纳

开发API层。 需要一些关于Decorator模式使用的建议和反馈

I am developing an api layer for my application. I have designed a structure and need some advice/feedback for it. You can find the basic implementation of the structure at the bottom.

Here are my requirements for the structure:

  • Response from API commands may need to be formatted in different formats (JSON,XML,etc.)
  • Some API commands may require authentication, some may not
  • Every API command should be open to extension via plugins (Notification on events, filtering of input/output paramters, etc.)

With these requirements in mind I have applied Decorator pattern to my API layer. I am not sure if I have designed the structure right and need to be sure about it.

The last item in requirements list is not covered in the implementation below because I am still trying to figure out how to do that.

What do you think? Am I on the right path?

<?php

// Interfaces
interface I_API_Command {}

// Abstract classes
abstract class A_API_Command implements I_API_Command
{
    abstract public function run();
}

abstract class A_Decorator_API_Command implements I_API_Command
{
    protected $_apiCommand;
    public function __construct(I_API_Command $apiCommand) {
        $this->_apiCommand = $apiCommand;
    }
    abstract public function run();
}

// Api command class
class APIC_Tasks_Get extends A_API_Command
{
    public function run() {
        // Returns tasks
    }
}

// Api command decorator classes
class APICD_Auth extends A_Decorator_API_Command
{
    public function run() {
        // Check authentication
        // If not authenticated: return error

        // If authenticated:
        return $this->_apiCommand->run()
    }
}

class APICD_JSON_Formatter extends A_Decorator_API_Command
{
    public function run() {
        return json_encode($this->_apiCommand->run());
    }
}

// Usage
$apiCommand = new APICD_JSON_Formatter(new APICD_Auth(new APIC_Tasks_Get()));
$apiCommand->run();

?>
  • 写回答

1条回答 默认 最新

  • dongzhuanlei0768 2010-10-16 09:41
    关注

    In my opinion... i think that old-classic MVC would be enough..

    Response from API commands may need to be formatted in different formats (JSON,XML,etc.)

    The controller would read the request and change the view to output the information in the selected format. Or you could pass the request to the View and the view would change the output format.

    Some API commands may require authentication, some may not

    That's also a task for the controller, to read and validate the request. If the user is not authenticated it would change the response

    Every API command should be open to extension via plugins (Notification on events, filtering of input/output paramters, etc.)

    Now this is the tricky part.. you could modify the controller and implement the strategy pattern. That's a good idea if you're planning to constantly change your plugins.

    In my case, i have multiple controllers and i use a Controller Factory that reads the request and returns a controller that manages that request.

    I am not really sure how do you want to implement your plugins. When you say Notification on events it sounds to me that you could use the observer pattern, and filtering of input/output paramters seems like a controller's task.

    I hope this helps. Good Luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加