是否有一种设计模式可以将工作准备与执行分开?

发布于 2024-10-05 15:40:34 字数 369 浏览 0 评论 0原文

是否有一种设计模式可以将工作的准备与执行分开?

一些示例:

  • 数据库通过创建查询计划并执行它来执行查询。
  • 对于我的应用程序,我实现了一种分阶段复制文件的方法,以便可以显示进度条。我首先创建文件复制操作列表,然后循环遍历该列表,根据每个操作的权重(文件大小)更新进度条。
  • 我还实现了一种分阶段的方法来获取数据,在其中准备批次列表,然后执行它们。原因是为了降低复杂性(不同批次有不同的参数)并防止内存过载(批次获取的数据量有最大限制)。

基本模式是有一个“规划器”对象,它将任务分解为可以单独执行的“工作项”。

我只是很好奇,我希望已知的设计模式可以帮助与其他开发人员沟通,也许能给我一些改进设计的想法。

Is there a Design Pattern which separates the preparation of work from its execution?

Some examples:

  • Databases execute queries by creating a query plan and executing it.
  • For my application, I implemented a staged approach for copying files so I could show a progress bar. I first create a list of file copy actions and then loop through the list updating the progress bar based on the weight (file size) of each action.
  • I also implemented a staged approach to fetch data where I prepare a list of batches and then execute them. The reasons were to reduce complexity (different batches have different parameters) and to prevent memory overload (there is a maximum amount of data fetched by a batch).

The basic pattern is that there is a 'Planner' object which breaks down a task into 'Work Items' that can be executed individually.

I'm just curious and I hope that an already known Design Pattern can help in communicating with other developers and perhaps give me some ideas to improve my design.

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

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

发布评论

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

评论(2

老娘不死你永远是小三 2024-10-12 15:40:34

您说

“基本模式是有一个‘规划器’对象,它将任务分解为可以单独执行的‘工作项’。”

这表明您可能需要类似命令模式之类的东西。一个简单的实现只涉及定义一个接口,使用单个方法的“命令”,并创建子类。我真的很喜欢这种模式,因为它很容易实现,并且命令很容易单独测试。

您可能还想查看复合模式。如果适用,您可以创建一个 CompositeCommand 抽象类。这提供了一种执行构成任务的所有命令的干净方法。而且,它真的很容易实现。

您的“Planner”对象将创建满足工作的复合命令(或者如果您不想使用复合命令,则只是一个命令列表)......

You said

"The basic pattern is that there is a 'Planner' object which breaks down a task into 'Work Items' that can be executed individually."

This indicates that you might want something like the Command Pattern. A simple implementation involved just defining an Interface, "Command" with a single method, and creating subclasses. I really like this pattern because it is easy to implement, and the commands are really easy to test in isolation.

You also might want to look at the Composite Pattern. If applicable, you could make a CompositeCommand abstract class. This provides a clean way to execute all the commands that compose a task. Also, its really easy to implement.

Your "Planner" object would create the composite command (or just a list of commands if you dont want to use composite) that satisfies the job...

余生共白头 2024-10-12 15:40:34

在分布式系统中,您经常会看到代理或调度程序将工作分配给工作人员或代理;调度程序可能会在对工作负载有一定了解的情况下实现,并且能够将其拆分,或者可能只是管理将任务从队列分配给工作人员。

我还没有找到此模式的共享描述或名称 - 但请查看此处作为起点:分布式计算中的主从模式

In distributed systems you often see a broker or scheduler allocating work to workers or agents; the scheduler might be implemented with some knowledge of the workload, and be able to split it up, or might just manage allocating tasks from a queue to workers.

I've yet to find a shared description or name for this pattern -- but take a look here for a starting point: Master-Worker pattern in distributed computing.

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