多个API调用:设计模式

Sha*_*ari 7 java oop design-patterns

我必须调用具有多个API的多个服务.其中很少有人基本上阅读(他们返回一些数据),其中很少有人改变几个对象的状态(他们基本上更新了几个对象的状态).

我正在寻找一个design pattern可以应用于上述场景的.

代码示例

让我们举一个服务A的小例子

AccountInfo A.getAccountInfo() 
void A.setAccountData(AccountInfo) 
AccountStatus A.getStatusForAccount 
Run Code Online (Sandbox Code Playgroud)

...

我想过有一个通用接口

interface CallAPI<Input , Output> {
   public Output execute(Input)
}
Run Code Online (Sandbox Code Playgroud)

每个API调用都会实现这个接口,我可以使用Factory模式来获取API的实例.

我想知道是否有更好的模式,或者它可以以不同的方式重构.API和服务只会增加,应该更容易设置新的API,客户端不应该为新API编写适配器带来额外的开销.

小智 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!