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 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式