Design Patterns .used in Java API
Following is the list of Design Patterns used in Java API
Creational Pattern
- Factory Method - getInstance() method in java.util.Calendar
- Singleton - java.lang.System and java.sql.DriverManager although they are not implemented using the approach recommended in the GoF book but with static methods.
- Prototype pattern - clone() method defined in class Object and the use of java.lang.Cloneable interface to grant permission for cloning.
- The Proxy pattern - is used extensively in the implementation of Java's Remote Method Invocation (RMI) and Interface Definition Language (IDL) features.
- Composite pattern - The structure of Component and Container classes in java.awt provide a good example of the same
- Bridge pattern - can be found in the separation of the components in java.awt (e.g., Button and List), and their counterparts in java.awt.peer.
- Adapter pattern - is used extensively by the adapter classes in java.awt.event. and wrapper classes.
- Decorator - The java.io package provides, among other things, a set of classes for reading input streams. Known as readers, each of those classes has a name that follows the pattern: xxxReader.
Readers provide specialized functionality; for example, one reader reads from a file, another tracks line numbers, and yet another pushes characters back on the input stream. In all, eight different readers exist to read input streams.
It's often necessary to combine the capabilities offered by java.io readers; for example, you might want to read from a file, keep track of line numbers, and push certain characters back on the input stream, all at the same time. The java.io package's designers could have used inheritance to provide a wide array of such reader combinations; for example, a FileReader class could have a LineNumberFileReader subclass, which could in turn have a PushBackLineNumberFileReader subclass. But using inheritance to compose the most widely used combinations of reader functionality would result in a veritable explosion of classes. So how did the java.io package's designers create a design that allows you to combine reader functionality in any way you desire with only 10 reader classes? As you might guess, they used the Decorator pattern.
- Observer pattern - The Java 1.1 event model. In addition, the interface java.util.Observable and the class java.util.Observer provide support for this pattern.
- Command pattern - The Java Swing classes implement this pattern by providing an Action interface and an AbstractAction class.
- Strategy pattern - Java's Abstract Window Toolkit (AWT) provides implementations of common user interface components such as buttons, menus, scrollbars, and lists. Those components are laid out -- meaning sized and positioned -- inside containers such as panels, dialog boxes, and windows. But AWT containers do not perform the actual layout; instead, those containers delegate layout functionality to another object known as a layout manager. That delegation is an example of the Strategy design pattern.
Labels: Design Pattern