使用 REST 的策略模式 API
Strategy pattern to consume REST API
关于 VoIP,我必须使用两个不同的 REST API 提供程序。 API 都对不同的端点和参数执行相同的操作。我正在建模 类 作为策略模式,我遇到的问题是每个方法策略的参数不同。
public interface VoIPRequests
{
string ApiKey { get; set; }
string GetExtensionsList();
string TriggerCall();
string DropCall();
string RedirectCall();
}
我如何根据实现更改每个方法的参数?。
在这种情况下使用策略模式是个好主意吗?
还有另一种模式更适合吗?
谢谢。
每个评论线程:
TriggerCall(), one api only needs one parameter "To" , and other api has two mandatory parameters "extension" and "destination"
然后我将重点放在 TriggerCall
上,让您从那里推断。
实施 1
public class VoIPRequests1 : VoIPRequests
{
private readonly object to; // Give this a more appropriate type
public VoIPRequests1(object to)
{
this.to = to;
}
public string TriggerCall()
{
// Use this.to here and return string;
}
// Other interface members go here...
}
实施 2
public class VoIPRequests2 : VoIPRequests
{
private readonly object extension; // Give this a more appropriate type
private readonly object destination; // Give this a more appropriate type
public VoIPRequests2(object extension, object destination)
{
this.extension = extension;
this.destination = destination;
}
public string TriggerCall()
{
// Use this.extension and this.destination here and return string;
}
// Other interface members go here...
}
关于 VoIP,我必须使用两个不同的 REST API 提供程序。 API 都对不同的端点和参数执行相同的操作。我正在建模 类 作为策略模式,我遇到的问题是每个方法策略的参数不同。
public interface VoIPRequests
{
string ApiKey { get; set; }
string GetExtensionsList();
string TriggerCall();
string DropCall();
string RedirectCall();
}
我如何根据实现更改每个方法的参数?。 在这种情况下使用策略模式是个好主意吗? 还有另一种模式更适合吗? 谢谢。
每个评论线程:
TriggerCall(), one api only needs one parameter "To" , and other api has two mandatory parameters "extension" and "destination"
然后我将重点放在 TriggerCall
上,让您从那里推断。
实施 1
public class VoIPRequests1 : VoIPRequests
{
private readonly object to; // Give this a more appropriate type
public VoIPRequests1(object to)
{
this.to = to;
}
public string TriggerCall()
{
// Use this.to here and return string;
}
// Other interface members go here...
}
实施 2
public class VoIPRequests2 : VoIPRequests
{
private readonly object extension; // Give this a more appropriate type
private readonly object destination; // Give this a more appropriate type
public VoIPRequests2(object extension, object destination)
{
this.extension = extension;
this.destination = destination;
}
public string TriggerCall()
{
// Use this.extension and this.destination here and return string;
}
// Other interface members go here...
}