It is difficult to know how to design a class so that its benefits justify the extra complexity.  The first step is to identify the concept or thing that the class is going to represent and choose a name that best describes it.  Then really clearly define what the class represents so there is no ambiguity.  The best place to do this is in the Javadoc at the top of the class.

/**
 * The base class for all TranscoderRunners, which are used to transcode
 * videos from one format into the format we support.
  
 * It contains any shared logic that specific runner
 * subclasses will reuse. This class should contain
 * no specific information relating to a kind of runner.
 */
public abstract class TranscoderRunner
{

Next think of all the things/steps the class needs to do.  Each one is probably a method of its own.  For example, a TranscoderRunner might need some methods like these to be able to transcode a video file:

:
public abstract class TranscoderRunner
{
    public void prepareTranscodeDisk();
    public TranscodeResult transcodeVideo(File sourceVideo);
    public StatusReport getStatus();
    public void forceStop();
}

Still confused?  If the steps and classes that need to be written are not immediately obvious, it is OK to write one large procedural type static method first.  Once you have it working, then consider the kinds of things the code is doing and split it into classes and methods if appropriate.  Eventually you will skip this initial step.

If a parameter passed into a method or constructor of a class that doesn't directly relate to it, the parameter should be removed.  For example, TranscoderRunner does not need web application information to translate videos, so it would be an immediate red flag to see an HttpServletRequest passed into it:


:
public abstract class TranscoderRunner
{
    public void prepareTranscodeDisk();
    public TranscodeResult transcodeVideo(HttpServletRequest userRequest,
                                          File sourceVideo);
    public StatusReport getStatus();
    public void forceStop();
}

Since TranscoderRunner has an HttpServletRequest it implies it is trying to satisfy two requirements (video conversion and user status) instead of just one (video conversion).

blog comments powered by Disqus