一个需要停止但不是由客户端停止的服务应该如何停止?

发布于 2024-10-13 03:31:05 字数 639 浏览 3 评论 0原文

简单的答案是拥有一个包含所有常规操作的接口,其中还包括 stop() 方法。

interface Service {
   operation( parameters ...);
   somethingElse( parameters ... );

   stop();
}

stop 方法的主要问题是大多数获得服务引用的客户端可能也无法停止该服务。

另一种替代方法是简单地定义两个接口:服务和 Stoppable。

interface Service {
    operation( parameters ...);
    somethingElse( parameters ... );
}

interface Stoppable {
    void stop();
}

这种方法的唯一问题是,如果实现被另一个服务包装,那么 stop 方法就会被隐藏起来。

阻止客户端“停止”你的服务的最初问题仍然是可能的,他们只需要首先检查引用是否是 Stoppable 的实例,然后他们就可以“停止”它。

你会如何解决这个问题?

我有一个想法,可以优雅地解决这个问题(对我来说很好),而无需留下公共停车位。然而在展示之前,我想要一些想法。

The easy answer is to have an interface with all the regular operations which also includes a stop() method.

interface Service {
   operation( parameters ...);
   somethingElse( parameters ... );

   stop();
}

The main problem with the stop method is that most clients who get a reference to a Service should probably not also be able to stop the service.

Another alternative is to simply define two interfaces, the service and the Stoppable

interface Service {
    operation( parameters ...);
    somethingElse( parameters ... );
}

interface Stoppable {
    void stop();
}

The only problem with this approach is if the implementation is wrapped by another Service, then the stop method is hidden away.

The original problem of stopping clients from "stopping" your service is still possible, they just need to first check if the reference is an instance of Stoppable and then they can "stop" it.

How would you solve this problem?

I have an idea which solves the problem elegantly (well for me) without leaving a public stop available. However before I show it, I'd like some ideas.

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

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

发布评论

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

评论(2

如果没结果 2024-10-20 03:31:05

使用接口继承:

interface Service {
    operation( parameters ...);
    somethingElse( parameters ... );
}


interface StoppableService extends Service {
    void stop();
}

只需要原始服务的客户端可以通过适当的工厂方法获得。任何应该能够停止服务的东西,都可以请求 StoppableService。

如果您担心客户端转换为 StoppableService 可以调用 stop(),那么您将需要具体的实现来分离此函数。给他们一个具有无操作版本的 stop() 的具体实现。您仍然可以让您的工厂向任何应该能够阻止它的人提供 stop() 的有效实现。也许当他们要求您的工厂实施时,他们可以传递适当的凭据,以便您可以确定他们应该能够做什么并为他们提供正确的版本。

Use interface inheritance:

interface Service {
    operation( parameters ...);
    somethingElse( parameters ... );
}


interface StoppableService extends Service {
    void stop();
}

Clients that only need the raw Service get given that by an appropriate factory method. Anything that should be able to stop the service, request the StoppableService.

If you are worried about clients casting to StoppableService can calling stop(), then you'll need your concrete implementations to separate out this function. Give them a concrete implementation that has a no-op version of stop(). You can still have your factory provide a working implementation of stop() to anyone who should be able stop it. Perhaps when they ask for an implementation from your factory, they can pass in appropriate credentials so you can determine what they should be able to do and give them the correct version.

梦在深巷 2024-10-20 03:31:05

你可以做类似的事情

interface Service {
    operation( parameters ...);
    somethingElse( parameters ... );

    <E> E getControlInterface(Class<E> clazz);
}

Stoppable stoppable = service.getControlInterface(Stoppable.class);
if (stoppable != null) {
    stoppable.stop();
}

但我认为这过于复杂,实现 Stoppable 接口应该足够了。包装器应该了解 Stoppable 并实现它。

You could do something like

interface Service {
    operation( parameters ...);
    somethingElse( parameters ... );

    <E> E getControlInterface(Class<E> clazz);
}

and then

Stoppable stoppable = service.getControlInterface(Stoppable.class);
if (stoppable != null) {
    stoppable.stop();
}

But I think this is overcomplicating, implementing Stoppable interface should be enough. The wrappers should be aware of Stoppable and implement it also.

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