多个API调用:设计模式

7
我需要调用多个具有多个API的服务。其中几个基本上是读取(它们返回给我一些数据),而另外一些则更改了几个对象的状态(它们基本上更新了几个对象的状态)。
我正在寻找一个可以应用于上述情况的设计模式。
代码示例
让我们以服务A的小例子为例。
AccountInfo A.getAccountInfo() 
void A.setAccountData(AccountInfo) 
AccountStatus A.getStatusForAccount 

我想设计一个通用接口。
interface CallAPI<Input , Output> {
   public Output execute(Input)
}

每个API调用都会实现这个接口,我可以使用工厂模式来获取API的实例。我想知道是否有更好的模式可以实现相同的功能或者它可以以不同的方式重构。API和服务将不断增加,应该更容易设置新的API,并且客户端不应该为新的API编写适配器而增加额外负担。

看一下 Retrofit,它有一个类似于你尝试实现的方式。 - Mohamed Ibrahim
1个回答

0

最好的方法是从数据库、基础设施细节等方面脱离出来,以坚实的设计为基础。使用FactoryMethod是正确的选择,如果想要创建一组对象,则可以使用抽象工厂,这样您就可以拥有SqlFamily对象、JsonFamily对象等。然后,您可以创建业务规则,这样您就可以使用多个服务,做一些更有趣的事情。良好的设计意味着使用多种设计模式,因此除了工厂之外,另一个选项是使用模板方法进行代码重用,还有策略模式等。以下是代码,您可以在其中识别上述不同的设计模式:

public interface IRead<T>
{
    T Fetch();
}

public abstract class ServiceA
{
    public abstract void OperationA();
    public abstract void OperationB();
}

public abstract class ServiceB
{
    public abstract void OperationD();
    public abstract void OperationE();
}

public abstract class JsonServiceAReaderTemplate : IRead<ServiceA>
{
    public abstract ServiceA Fetch();
}

public sealed class JsonServiceAFetcher : JsonServiceAReaderTemplate
{
    /// <summary>
    /// Get parameters needded to load from json
    /// </summary>
    public JsonServiceAFetcher()
    {

    }
    public override ServiceA Fetch()
    {
        //Load from Json store
        throw new NotImplementedException();
    }
}

public abstract class JsonServiceBReaderTemplate : IRead<ServiceB>
{
    public abstract ServiceB Fetch();
}

public sealed class JsonServiceBFetcher : JsonServiceBReaderTemplate
{
    /// <summary>
    /// Get parameters needded to load from json
    /// </summary>
    public JsonServiceBFetcher()
    {

    }
    public override ServiceB Fetch()
    {
        //Load from Json store
        throw new NotImplementedException();
    }
}

public sealed class ServicesGateway
{
    public ServiceA ServiceA { get; }
    public ServiceB ServiceB { get; }

    public ServicesGateway(IRead<ServiceA> serviceA, IRead<ServiceB> serviceB)
    {
        ServiceA = serviceA.Fetch();
        ServiceB = serviceB.Fetch();
    }
}

public interface IBusinessRule
{
    void Execute();
}

public sealed class BusinessRuleServiceAServiceB : IBusinessRule
{
    private readonly ServicesGateway servicesGateway;

    public BusinessRuleServiceAServiceB(ServicesGateway servicesGateway)
    {
        this.servicesGateway = servicesGateway;
    }

    public void Execute()
    {
        var serviceA = servicesGateway.ServiceA;
        var serviceB = servicesGateway.ServiceB;
        serviceA.OperationA();
        serviceA.OperationB();
        serviceB.OperationD();
        serviceB.OperationE();
    }
}

public sealed class ClientCode
{
    public void Run()
    {
        //Inject from IoC
        ServicesGateway gateway = new ServicesGateway(new JsonServiceAFetcher(), new JsonServiceBFetcher());
        var businessRule = new BusinessRuleServiceAServiceB(gateway);
        businessRule.Execute();
    }
}

希望这能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接