Designing Software  «Prev  Next»
Lesson 4Lessons learned from patterns
ObjectiveExamine how Patterns can be used in unexpected Ways

Lessons Learned from Design Patterns

One way patterns help is by introducing you to new tricks and techniques that can be used in other places, even though the patterns may not even be used.

Alert dialog

For example, one of the most difficult problems for students learning to program in Java is to write a class, that brings up a standard alert dialog with a single method. Furthermore, I specify that the class should use only this one public method that sets the message.
This makes the class extremely simple to use for client programmers.
Most students begin by extending Java's Dialog class. However they rapidly discover that this requires them to override and expose many public methods. They tend to end up with results like this:

AlertDialog a = new AlertDialog("That action is forbidden");

This is just too complicated. The correct, simpler solution (at least simpler for the client programmer who actually has to use this class) is to wrap a Dialog object inside another class, then construct it and display it through a static method like this:

Message.alert("That action is forbidden");

The use of a static method to create instances that are otherwise hidden in an object's private areas is found in almost every creational pattern including Singleton and Factory Method.
This solution does not use the Singleton or Factory Method patterns, but it uses the concepts learned from these patterns.