Below I describe what I'm trying to do and how I think I will do it. What I'm hoping is that a design pattern exists for using one object to configure another. This would allow me to have a nice generic Job class rather than an ever-increasing list of Job subclasses.

If there is such a design pattern, what's it called and how would I apply it to my use case? I'm using the Symfony framework and working in PHP.

Part of my application is a Task list for user activities. UserA creates Task $task1 and it shows up in the Task list. UserB accepts $task1 which changes the task's state to "accepted"; UserB starts $task1 which changes the task's state to "started"; etc. I've implemented the preceding as a state machine and it works well.

However, each Task carries a Payload. The Payload has 2 properties:

  • Payload$sop is an SOP object that contains configuration information.
  • Payload$inputvars is an array of input vars provided by the Task creator.

and a method:

  • makeJob(SOP $sop = null, $inputvars = null), a factory method that creates a Job object configured according to the SOP object. This is the "and magic happens" spot in my code.

When $task1->status changes to "started", $task1->payload->makeJob() is called and a new Job $job is created. UserB will interact with a form to input fields required by the SOP that produced $job. When UserB changes $job->status to "completed", the state of the $task1 will also be changed to "completed".

So, is this a reasonable way to reduce subclassing of Job? Is there a more efficient way, given the large number of possible Job subclasses (we're talking hundreds here).

  • What varies between different Job instances? – Robert Harvey Sep 9 '15 at 19:50
  • @RobertHarvey, a Job might be "run an NMR experiment" which would require input sample type, input volume, output trace file (which would be parsed based on it being an NMR experiment), or "run a time|concentration course experiment for enzymeY against substanceX" which would require size of time steps, size of concentration steps, range of time and concentration, etc. – dnagirl Sep 9 '15 at 19:58
  • I prefer your factory method approach. If you have a lot of common functionality, perhaps you can inherit from a class that contains that common functionality. – Robert Harvey Sep 9 '15 at 20:44
  • @RobertHarvey, I considered instantiating a specific Job subclass based on an SOP template, but that would mean that I'd need hard-coded Job subclasses (e.g. NMRJob, TimeCourseJob, etc.), one for each possible SOP. But SOP is generic- a user will define an SOP using a form. He will pick input types from a list and set validation rules for each one. He will pick output types from a list. His choices will be saved to a database. What I'd like is to have one Job class with dynamic properties based on the content of the chosen SOP. – dnagirl Sep 9 '15 at 20:49
  • Are you just transferring data, or is there behavior in each of these classes? – Robert Harvey Sep 9 '15 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.