提供客户端-服务器向后兼容性的正确模式是什么?

发布于 2024-10-14 08:07:13 字数 182 浏览 2 评论 0原文

我正在开发一个客户端-服务器应用程序,不能强迫用户不使用旧版本的客户端,甚至其他客户端,因为协议是 WebDAV。

我需要的是支持所有这些,并具有某种向后兼容性,以便服务器端的行为方式有所不同,具体取决于它所使用的客户端版本。

我的问题是在面对这种情况之前如何准备我的申请。我需要使用一些设计模式吗?如何设计向后兼容?

I'm developing a client-server application and can't force users not to use older versions of client, or even other clients, since the protocol is WebDAV.

What I need is to support all of them, with some kind of backward compatibility, so that server side will behave different way, depending on what client version it is working with.

My question is how to prepare my application to this situation, before facing it. Do I need to use some design pattern? How to design backward compatibility?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清秋悲枫 2024-10-21 08:07:13

如果您为您的服务器创建一个合适的 API(Facade 设计模式),那么您的生活就会变得更加美好将来会更容易。

假设您的服务器提供了一个由服务 A、B 和服务 A 组成的 API。 C. 这些服务是在业务逻辑层实现的。客户端对这些服务的访问始终是通过外观,而不是直接访问。所以你的门面(版本 1)暴露了 A、B 和 B。 C. 到目前为止没什么大不了的...

现在假设您需要添加服务 D、删除服务 B 并更改服务 C。您创建一个新的外观(版本 2),它公开服务 A、D 和更新的 C。你的业务层你会添加服务D的逻辑,将B标记为“过时”,至于C中的更改,则取决于更改是否向后兼容。如果是的话很简单,只需添加一个重载即可。如果服务 C 现在的工作方式完全不同,请将其实现为新服务。有时虽然有些变化会破坏旧客户端...

外观本身可以是一个Web服务(在大多数情况下我的首选解决方案),并且没有自己的业务逻辑,它唯一的责任是将客户端的调用委托给适当的服务。

If you create a proper API (Facade design pattern) for your server, you'd make your life easier in the future.

suppose your server provides an API that consists of services A, B & C. These services are implemented in the business logic layer. access to these services from the clients is always through the facade, no direct access. so your facade (version 1) exposes A, B & C. no big deal so far...

now suppose you need to add service D, remove service B, and change service C. you create a new facade (version 2), which exposes services A, D and an updated C. in your business layer you would add the logic for service D, mark B as "obsolete", and as for the change in C, it depends if the change is backward compatible. if yes it's easy, just add an overload. if service C now works completely different, implement it as a new service. sometimes though there are changes that break old clients...

the Facade itself could be a web service (my preferred solution in most cases), and has no business logic of it's own, its only responsibility is to delegate the call from the client to the approproate service.

待"谢繁草 2024-10-21 08:07:13

我认为您棘手的设计问题与设计模式所解决的级别不同。

当然你可以在你的核心业务逻辑前面应用Facades,暴露不同的接口

MyServiceV1  { // the original interface

MyServiceV2  { // the new interface 

等等。我从如何使用新实现来实现旧接口中看到了有趣的设计点。例如,假设在旧界面中您有一个创建某些业务项目的方法

createItem( String name,
            Integer value);

,而在新版本中您有

 createItem( String name,
            Integer value,
            String justification
 );

这样的方法,那么当 v1 请求到达外观时,它不会有用于“理由”的数据,那么外观应该做什么?可能添加一些“未知”的值,但您所做的与其说是设计问题,不如说是理解业务需求的问题。显然,根据创建新版本服务时所做的不同类型的更改,存在许多此类棘手的问题。

因此,首先通过接口逐个接口地了解需求和处理不同变化的策略。这将导致各种实现问题,当您遇到这些问题时,您可能会开始在实现中看到促使您采用显式设计模式的模式。

I think your tricky design problems are at a different level than Design Patterns address.

Of course you can apply Facades in front of your core business logic, exposing different interfaces

MyServiceV1  { // the original interface

MyServiceV2  { // the new interface 

and so on. I see the interesting design points coming from how you implement the old interface with the new implementation. For example suppose in the old interface you have a method creating some business item

createItem( String name,
            Integer value);

and in the new version you have

 createItem( String name,
            Integer value,
            String justification
 );

So when a v1 request arrives at the facade it won't have data for "justification", so what should the facade do? Possibly add in some "UNKNOWN" value, but what you do is not so much a matter of design as a matter of understanding the business requirements. Clearly there are much tricker problems of this kind depending upon the different kinds of change made in creating the new version of the services.

So, first work through interface by interface understanding the requirements, the policies for dealing with the different chnages. This will lead to various implementation problems and when you get to those you may start to see patterns in the implementation that drive you to adopt explicit Design Patterns.

壹場煙雨 2024-10-21 08:07:13

我认为在这种情况下使用的设计模式是策略设计模式

大致...想法是:

  • Server 实例始终侦听新连接。
  • 当与 Server 建立新连接时,Server 会创建一个 ClientConnection 实例来管理它。从该连接接收到的所有后续数据都将路由到实例化的 ClientConnection 并由其处理。
  • 每个ClientConnection 管理服务器 和单个远程客户端主机之间的所有通信。
  • ClientConnection 所做的第一件事是与远程客户端协商,并确定它是哪个版本。完成此操作后,ClientConnection 将实例化 ServerBehaviorStrategy 的适当实现,以处理 ClientConnection 的所有后续通信。

编程都在 ServerBehaviorStrategy 接口的单独实现中完全隔离。

I think the design pattern to use in this situation is the strategy design pattern.

Roughly...the idea is that:

  • the Server instance is always listening for new connections.
  • when new connections are established with the Server, the Server creates an instance of ClientConnection to manage it. all subsequent data received from that connection is routed to and handled by the instantiated ClientConnection.
  • each ClientConnection manages all communications between the Server and an individual remote client host.
  • the first thing a ClientConnection does is negotiate with the remote client, and figure out which version it is. once this is done, the ClientConnection will instantiate the appropriate implementation of the ServerBehaviorStrategy to handle all subsequent communications of the ClientConnection.

UML class diagram of architecture

This way, all the programming of behavior specific things are cleanly isolated in the separate implementations of the ServerBehaviorStrategy interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文