In this episode, we tackle a common problem: hardcoding jobs when dealing with different types of tasks. Rather than stuffing all your logic inside an observer, we look for a cleaner solution using the factory pattern.
We start by considering a scenario where, for example, users can upload a video for conversion. Depending on what type of conversion is needed, we may have different job batches. Instead of creating a separate model for every job variation, we introduce a ServerTypeFactory
that helps decide which jobs to run based on the type of server (or task) being processed.
We build a class to represent each server type (like AppServer
) and give it a method to return an ordered list of jobs to run for that type. Then, we use our factory to resolve which class to use based on the type information, and pull out the corresponding job list.
You get to see all this wired up, tested, and working—no more hardcoded job lists! When you try creating a server, the right set of jobs gets bundled into the batch automatically. Plus, if you ever need to support more types (like a dedicated database server), it's as easy as adding another class and updating your factory—everything stays neat and easy to maintain.