问题
What is "worker" design pattern?
回答1:
It could be that you are after a worker thread pattern, where you use a queue to schedule tasks that you want to be processed "offline" by a worker thread. Some solutions will use a pool of worker threads instead of single thread to achieve performance gains by utilising paralelisation.
回答2:
The worker design pattern
Problem:
- You have a small object which is data and large set of operations which could be performed to that object
- You want to keep the object’s method list small
- The operations can be done in different ways, including different implementations
- Your small object only knows how to read and save itself from the data abstraction layer
- You want to batch process a number of small objects
Solution:
- You have a worker interface which defines the accessor API for the worker and how to add subjects
- You can have multiple workers per subject
- Your worker does the transformation, your subject is transformed
- Your subject is kept light weight
Example:
Your ImageBinary object represents the image binary including height and width (metadata is decoupled). You perform various operations on this object like resizing, cropping, scaling.
$image1 = new ImageBinary(array(‘id’ => 1));
$image2 = new ImageBinary(array(‘id’ => 2));
$image3 = new ImageBinary(array(‘id’ => 3));
$worker = new ImageWorker;
$worker->add($image1);
$worker->add($image2);
$worker->add($image3);
$worker->rotate(90);
Related patterns
- Manager
- Adapter
回答3:
Something like MapReduce comes to mind. Where multiple tasks can be performed in parallel on worker nodes.
Edit: As @larsmans mentioned, this is known as the Master/Worker pattern.
来源:https://stackoverflow.com/questions/4945509/worker-design-pattern