Worker design pattern

lital maatuk picture lital maatuk · Feb 9, 2011 · Viewed 19.9k times · Source

What is "worker" design pattern?

Answer

Abdur Rahman picture Abdur Rahman · Dec 20, 2011

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