草庐IT

java - 多个 API 调用 : Design Patterns

coder 2024-03-21 原文

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

我正在寻找可以应用于上述场景的设计模式

代码示例

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

AccountInfo A.getAccountInfo() 
void A.setAccountData(AccountInfo) 
AccountStatus A.getStatusForAccount 

...

我想到了一个通用接口(interface)

interface CallAPI<Input , Output> {
   public Output execute(Input)
}

每个 API 调用都会实现这个接口(interface),我可以使用工厂模式来获取 API 的实例。

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

最佳答案

最好的方法是从数据库、基础结构细节等不可知的可靠设计开始。方法是使用 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();
    }
}

希望这对您有所帮助!

关于java - 多个 API 调用 : Design Patterns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37755525/

有关java - 多个 API 调用 : Design Patterns的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  5. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  6. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby-on-rails - ActionController::RoutingError: 未初始化常量 Api::V1::ApiController - 2

    我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc

  9. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  10. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

随机推荐