GofPatterns Gofpatterns  

Design Patterns  «Prev  Next»
Lesson 4Lessons learned from patterns
ObjectiveExamine how Patterns can be used in unexpected Ways
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
Alert dialog
For example, one of the most difficult problems for students in my Intro to Java Programming course 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:
Head First Design PatternsHead First Design Patterns

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.