简体   繁体   English

适用于类似Web服务实现的设计模式

[英]A suitable design pattern for similar web service implementations

I'm consuming a SOAP web service that creates a separate service point and WSDL for each of its customers. 我正在使用一个SOAP Web服务,该服务为其每个客户创建一个单独的服务点和WSDL。 I don't know why the do that. 我不知道为什么要这么做。 But eg if they have two clients A and B, the service designates two different service addresses with different WSDL addresses. 但是,例如,如果它们有两个客户端A和B,则该服务将使用不同的WSDL地址指定两个不同的服务地址。 These separate WSDLs are 90% the same objects and same functions but some of them are different based on the type of the customer. 这些单独的WSDL具有90%相同的对象和相同的功能,但是其中一些根据客户的类型而不同。 Therefore the created objects are eventually not the same even though they work exactly the same way. 因此,即使创建的对象以完全相同的方式工作,它们最终也不相同。

So in order to fetch the correct service, I store the name of the customer somewhere on a table ("A" or "B") and my program has to know which customer its dealing with every run. 因此,为了获取正确的服务,我将客户的名称存储在表的某个位置(“ A”或“ B”),并且我的程序必须知道每次运行都与哪个客户打交道。 I don't want to have different programs for each customer. 我不想为每个客户提供不同的程序。 I just want my program to get the customer name and based on that understand which model and which controller functions it will use. 我只希望我的程序获得客户名称,并在此基础上了解它将使用的型号和控制器功能。

What is the design pattern(s) that will help me facilitate this issue? 什么设计模式可以帮助我解决此问题? Chances are, in the future there will be an additional customer, so I want my code to be as loosely-coupled as it gets. 将来可能会有更多的客户,所以我希望我的代码尽可能地做到松耦合。

I have always wanted to use design patterns correctly in my code so I guess it's time to do so. 我一直想在代码中正确使用设计模式,所以我想是时候这样做了。 Should I use a Strategy Pattern ? 我应该使用策略模式吗? Can you briefly explain what is the best solution for this? 您能否简要说明什么是最佳解决方案?

I would use two design patterns in your case. 在您的情况下,我将使用两种设计模式。 The first one would be the Facade pattern . 第一个是Facade模式 Use the facade pattern to simplify the interface of the web services your application has to deal with. 使用外观模式可以简化应用程序必须处理的Web服务的界面。 Make sure you only need to change the implementation of the facade when the webservice contract changes. 确保仅在Web服务合同更改时才需要更改Facade的实现。 Convert the objects from the service into objects under your control and call functions with names and parameters that fit your domain and abstraction level. 使用适合您的域和抽象级别的名称和参数,将服务中的对象转换为您控制和调用函数下的对象。

The second design pattern would be the Adapter pattern . 第二个设计模式是Adapter模式 In your case, you should determine if you can decide on a common interface for both web services. 在您的情况下,您应该确定是否可以为两个Web服务确定一个公共接口。 So, if the 10% difference between the two services can be converted into one interface (of objects and/or functions) you use in your application. 因此,如果可以将两种服务之间10%的差异转换为您在应用程序中使用的一个接口(对象和/或功能的接口)。

The facade would use adapters to convert the 10% difference into common objects and functions. 外墙将使用适配器将10%的差异转换为常见的对象和功能。 After that, the facade uses the common objects and functions, as well as the other 90% of the web services, to supply a proper abstraction layer for your application. 之后,Facade使用公共对象和功能以及其他90%的Web服务为您的应用程序提供适当的抽象层。

If there are additional customers in the future, you'll most likely only need to add or modify an adapter. 如果将来有更多客户,您很可能只需要添加或修改适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM