So I decide I'm not going to modify my application as I've indicated above. First, I'm going to redesign it with what I consider a better approach:
So I will replace MainProcedural.java with these classes:
import java.util.Date;
public class MainPatterns {
public static void main(String[] args) {
Date date = new Date();
int weekday = -1;
MainPatterns taskMgr = new MainPatterns();
DayTasks taskDisplayer;
//-- Allow the weekday to be set by a parameter so we can test the program
if (args.length >0 ) {
try {
weekday = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("Parameter must be 1 - 7");
}
}
else {
weekday = date.getDay();
}
//-- Retrieve the appropriate DayTasks from the factory:
taskDisplayer = DayTaskFactory.getDayTaskHandler(weekday);
try {
taskDisplayer.showTasks();
}
catch (Exception e) {
System.out.println("No handler found. Weekday: " + weekday);
}
}
}
public class DayTaskFactory {
public static DayTasks getDayTaskHandler(int weekday) {
DayTasks taskDisplayer;
switch (weekday) {
case 1:
taskDisplayer = new MondayTasks();
break;
case 2:
taskDisplayer = new TuesdayTasks();
break;
case 3:
taskDisplayer = new WednesdayTasks();
break;
case 4:
taskDisplayer = new ThursdayTasks();
break;
case 5:
taskDisplayer = new FridayTasks();
break;
default:
taskDisplayer = null;
}
return taskDisplayer;
}
}
public abstract class DayTasks {
public abstract void showTasks();
}
public class MondayTasks extends DayTasks {
public void showTasks() {
System.out.println("Monday: Take trash to the curb");
}
}
public class TuesdayTasks extends DayTasks {
public void showTasks() {
System.out.println("Tuesday: Put out recyclables");
}
}
public class WednesdayTasks extends DayTasks {
public void showTasks() {
System.out.println("Wednesday: Watch \"Lost\"");
}
}
public class ThursdayTasks extends DayTasks {
public void showTasks() {
System.out.println("Thursday: Take trash to the curb");
}
}
public class FridayTasks extends DayTasks {
public void showTasks() {
System.out.println("Friday: Buy beer");
}
}
These classes will each extend DayTasks.java and manage the specific message for the day of the week.
When I run this application, my output is identical to my procedural design. That's not a surprise. We don't employ a pattern approach for the application end-user's benefit (not directly, anyway).
Question: What can I accomplish with design patterns that can't be done with a more traditional procedural approach?
Answer: If you're talking about application functionality, the answer is: probably nothing. Patterns deal with how you organize your code. The example in this article demonstrates how the procedural approach builds a routine and the pattern approach builds a framework. This pattern approach lends itself to simpler extensibility because decision-making is handled in the factory and operational specifics are handled in the DayTasks subclasses. With the procedural approach, the separation of functionality wasn't organized (or separated) as well.
So my initial modification plans:
Are replaced with these plans:
Here's the modified factory:
public class DayTaskFactory {
public static DayTasks getDayTaskHandler(int weekday) {
DayTasks taskDisplayer;
switch (weekday) {
case 0:
taskDisplayer = new SundayTasks();
break;
case 1:
taskDisplayer = new MondayTasks();
break;
case 2:
taskDisplayer = new TuesdayTasks();
break;
case 3:
taskDisplayer = new WednesdayTasks();
break;
case 4:
taskDisplayer = new ThursdayTasks();
break;
case 5:
taskDisplayer = new FridayTasks();
break;
case 6:
taskDisplayer = new SaturdayTasks();
break;
default:
taskDisplayer = null;
}
return taskDisplayer;
}
}
And here are the new classes for Saturday and Sunday
public class SaturdayTasks extends DayTasks {
public void showTasks() {
System.out.println("Saturday: Cut the lawn");
}
}
public class SundayTasks extends DayTasks {
public void showTasks() {
System.out.println("Sunday: Call Mom");
}
}