开发者

Is there a Design Pattern for separating preparation of work from execution?

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 ba开发者_JAVA百科tches 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.


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜