Design patterns are used and supported extensively throughout the Java APIs. Here are some examples:
· The Model-View-Controller design pattern is used extensively throughout the Swing API.
· The getInstance() method in java.util.Calendar is an example of a simple form of the Factory Method design pattern.
· The classes java.lang.System and java.sql.DriverManager are examples of the Singleton pattern, although they are not implemented using the approach recommended in the GoF book but with static methods.
· The Prototype pattern is supported in Java through the clone() method defined in class Object and the use of java.lang.Cloneable interface to grant permission for cloning.
· The Java Swing classes support the Command pattern by providing an Action interface and an AbstractAction class.
· The Java 1.1 event model is based on the observer pattern. In addition, the interface java.util.Observable and the class java.util.Observer provide support for this pattern.
· The Adapter pattern is used extensively by the adapter classes in java.awt.event.
· The Proxy pattern is used extensively in the implementation of Java's Remote Method Invocation (RMI) and Interface Definition Language (IDL) features.
· The structure of Component and Container classes in java.awt provide a good example of the Composite pattern.
· The 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.
· Iterator pattern is supported through java.util.Enumeration and java.util.Iterator
· Command pattern is also used by the Swing Undo framework
· Singleton in used by java.lang.Runtime and java.awt.Toolkit
· The Decorator pattern is used by the I/O streams.
Many design patterns were used in EJB, and some of them are clearly identifiable by their naming convention. Here are several:
1. Factory Method: Define an interface for creating classes, let a subclass (or a helper class) decide which class to instantiate.
This is used in EJB creation model. EJBHome defines an interface for creating the EJBObject implementations. They are actually created by a generated container class. See InitialContextFactory interface that returns an InitialContext based on a properties hashtable.
2.Singleton: Ensure a class has only one instance, and provide a global point of access to it.
There are many such classes. One example is javax.naming.NamingManager
3. Abstract Factory: Provide an interface for creating families of relegated or dependent objects without specifying their concrete classes.
We have interfaces called InitialContext, InitialContextFactory. InitialContextFactory has methods to get InitialContext.
4.Builder: Separate the construction of a complex factory from its representation so that the same construction process can create different representations.
InitialContextFactoryBuilder can create a InitialContextFactory.
5. Adapter: Convert the interface of a class into another interface clients expect.
In the EJB implementation model, we implement an EJB in a class that extends SessionBean or a EntityBean. We don't directly implement the EJBObject/home interfaces. EJB container generates a class that adapts the EJBObject interface by forwarding the calls to the enterprise bean class and provides declarative transaction, persistence support.
6. Proxy: Provide a surrogate for other object to control access to it.
We have remote RMI-CORBA proxies ( stubs and skeletons) for the EJB's.
7. Memento: Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
No comments:
Post a Comment