每个适配器具有不同能力的适配器模式
Adapter Pattern with different abilities for each adaptee
我正在编写一个程序,该程序将在少数 API 中使用适配器模式。目标是能够合并和聚合来自每个 API 的数据。问题是,并非每个 API 都具有相同的功能。例如:
API一个
- 列出所有市场
- 列出具体市场价格
- 列出订单
API B
- 列出具体市场价格
- 上市市场 24 小时 high/low
请注意 API B 如何获得 24 小时价格高点和低点,但 API A 不能。同时,API A 可以列出所有可用的市场,而 API B 不能。
处理这种情况的理想方法是什么?
- 我考虑过使用 FLAGS 枚举来列出每个 API 的功能,但这感觉像是一种迂回的做事方式,需要大量额外的代码来进行检查等等。
- 我已经考虑过为 API 可以处理的每个能力单独的适配器,但这可能需要大量的继承,以至于每个 API 都使用 5 个以上的接口来执行操作一个人。
我的目标是使该程序将来可以轻松扩展到其他 API。如果这是我的目标,我应该如何处理? (如果重要的话,我正在使用 C#)
纤薄的界面是你的朋友。
使用您的示例:
public interface IListAllMarkets
{
List<Market> GetAllMarkets();
}
public interface IListMarketPrice
{
List<MarketPrice> GetMarketPrices();
}
public interface IListOrders
{
List<Order> GetOrders();
}
public interface IHighLow
{
List<HighLow> GetHighLows();
}
// then the class that handles the apis
public class MyReusableClass : IListAllMarkets,
IListMarketPrice,
IListOrders,
IHighLow
{
private readonly ApiA _apiA = new ApiA();
private readonly ApiB _apiB = new ApiB();
public List<Market> GetAllMarkets()
{
return _apiA.ListAllMarkets() ;
}
public List<MarketPrice> GetMarketPrices()
{
// pretending that the business logic is try apiA first then fallback to apiB
var prices = _apiA.ListSpecificMarketPrices();
if (prices.Count == 0)
{
prices = _apiB.ListSpecificMarketPrices();
}
return prices;
}
public List<Order> GetOrders()
{
return _apiA.ListAllOrders();
}
public List<HighLow> GetHighLows()
{
return _apiB.ListMarket24HourJHighsLows();
}
}
既然我们已经把代码搞定了,我建议我们避免以 "How can I use this pattern to solve X?" 的方式来处理事情,而是从 "I have this problem X? How can I solve X? Is there a pattern that can help me solve it?"
的角度来处理事情
我正在编写一个程序,该程序将在少数 API 中使用适配器模式。目标是能够合并和聚合来自每个 API 的数据。问题是,并非每个 API 都具有相同的功能。例如:
API一个
- 列出所有市场
- 列出具体市场价格
- 列出订单
API B
- 列出具体市场价格
- 上市市场 24 小时 high/low
请注意 API B 如何获得 24 小时价格高点和低点,但 API A 不能。同时,API A 可以列出所有可用的市场,而 API B 不能。
处理这种情况的理想方法是什么?
- 我考虑过使用 FLAGS 枚举来列出每个 API 的功能,但这感觉像是一种迂回的做事方式,需要大量额外的代码来进行检查等等。
- 我已经考虑过为 API 可以处理的每个能力单独的适配器,但这可能需要大量的继承,以至于每个 API 都使用 5 个以上的接口来执行操作一个人。
我的目标是使该程序将来可以轻松扩展到其他 API。如果这是我的目标,我应该如何处理? (如果重要的话,我正在使用 C#)
纤薄的界面是你的朋友。
使用您的示例:
public interface IListAllMarkets
{
List<Market> GetAllMarkets();
}
public interface IListMarketPrice
{
List<MarketPrice> GetMarketPrices();
}
public interface IListOrders
{
List<Order> GetOrders();
}
public interface IHighLow
{
List<HighLow> GetHighLows();
}
// then the class that handles the apis
public class MyReusableClass : IListAllMarkets,
IListMarketPrice,
IListOrders,
IHighLow
{
private readonly ApiA _apiA = new ApiA();
private readonly ApiB _apiB = new ApiB();
public List<Market> GetAllMarkets()
{
return _apiA.ListAllMarkets() ;
}
public List<MarketPrice> GetMarketPrices()
{
// pretending that the business logic is try apiA first then fallback to apiB
var prices = _apiA.ListSpecificMarketPrices();
if (prices.Count == 0)
{
prices = _apiB.ListSpecificMarketPrices();
}
return prices;
}
public List<Order> GetOrders()
{
return _apiA.ListAllOrders();
}
public List<HighLow> GetHighLows()
{
return _apiB.ListMarket24HourJHighsLows();
}
}
既然我们已经把代码搞定了,我建议我们避免以 "How can I use this pattern to solve X?" 的方式来处理事情,而是从 "I have this problem X? How can I solve X? Is there a pattern that can help me solve it?"
的角度来处理事情