尽管这个问题是广泛而主观的,但我会尝试一下。
如果是我,我会创建一个“服务”类,称为类似于Orders
您的应用程序与之交互的东西。在其中,它处理确定调用哪个 API 和数据规范化的逻辑。它不会执行个别服务特定的逻辑,它会代理它。
// You might call something like this in your controller
$orders = Orders::all();
在那个订单类里面:
class Orders {
function __construct() {
$this->serviceHandlers = [
new service1(),
new service2(),
];
}
function all() {
// Final output array
$orders = [];
foreach ($this->serviceHandlers as $handler) {
// Get orders from specific service
$results = $handler->getOrders();
// Normalize data and add them to the orders
foreach ($results as $result) {
$orders[] = $handler->normalize($result);
}
}
return $orders;
}
}
然后每个处理程序类都应该有一个抽象,强制它们实现 agetOrders()
和normalize()
方法。
最好让该服务基于配置文件动态加载服务 API 类,这样您就不会对它们进行硬编码。