A Design Pattern is a solution which experienced software developers have been using over the years when they have to solve a given type of problem. Useful design patterns include:
- creational patterns like Abstract factory and Singleton,
- structural patterns like Adapter (or Façade) and Composite and
- behavioral patterns like Observer and Strategy
One good way to learn about Design Patterns is to write real software applications which, at their core, use patterns in order to solve real problems.
First exercise: SecurityManager
In a lot of applications, there exist classes which need to have only one instance (one object created at runtime). Examples are database connections, security managers to verify passwords, credit card processors, etc.
Write a Java class called SecurityManager which only have one instance throughout the application. Use the Singleton design pattern.
Second exercise: IOFacade
Frequently, we need to hide a set of classes with a complex interface behind a simpler class. This happens, for example, when reusing code written by another team of programmers.
Write a Java class called IOFacade which simplify the use of the Java I/O facilities by making exceptions less intrusive. Use the Façade design pattern. (This pattern is somewhat related to the Adapter as used in InputStream -> InputStreamReader -> BufferedReader.)
Third exercise: Observers
In a lot of operating systems and online services, observers register their interest for a specific event and, when this event happens, the subject of the event notifies all the observers. This allows all the observers to know that the event has occurred without having to resort to polling (which is wasteful of resources).
Write a Java program allows Observers to be added to Subjects. When the state of the Subject changes, all Observers should be immediately notified. Use the Observer design pattern.
Fourth exercise: Solar System
Finally, quite a lot of applications e.g. desktop environments such as Gnome or Windows, vectorial drawing apps, word processors, ERP systems, etc. need to model hierarchies e.g. graphical widgets, shapes, words and people / products.
As a tribute to Dennis Ritchie, write a Java program that models the whole of the Solar System including the star, planets, natural satellites and artificial satellites. Use the Composite design pattern. The class diagram for the Solar System application is: