A class should be designed for a single purpose. In the mistaken belief that time is being saved, some classes are designed to do many different kinds of things, much like a programmatic Swiss army knife.
In the following example, the Schedule class is primarily designed to contain the data for single schedule, a set of events. However, the developer added some extra functionality to the class to give it two uses:
representing a Schedule, and
schedule management.
public class Schedule
{
private String name;
private List events;
private static List allSchedules;
public void addEvent(Event event)
{
events.add(event);
}
public static synchronized void addSchedule(Schedule schedule)
{
allSchedules.add(schedule);
}
:
}
The two purposes are significantly different and would confuse others, so the class is easily split in two as follows:
public class Schedule
{
private String name;
private List events;
public void addEvent(Event event)
{
events.add(event);
}
:
}
public class ScheduleManager
{
private static List allSchedules;
public static synchronized void addSchedule(Schedule schedule)
{
allSchedules.add(schedule);
}
:
}