3/19/2010

Java Design Patterns

1 - Creational Design Patterns
a) Factory Pattern:

Definition:
The intent of the Factory pattern is to provide an interface for creating families of related or dependent objects without specifying their concrete classes. The application requests an object from the factory, and the factory keeps track of which
object is used. Since the application does not know which concrete classes are used, those classes can be changed at the factory level without impacting the rest of the application.

Why Factory Pattern?
All of the creational patterns deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged. In Java, of course, the simplest way to create an instance of an object is by using the new operator.Fred = new Fred(); //instance of Fred class
However, this really amounts to hard coding, depending on how you create the object within your program. In many cases, the exact nature of the object that is created could vary with the needs of the program and abstracting the creation process into a special “creator” class can make your program more flexible and general.


2. Behavioural Pattern
Behavioral patterns are those patterns that are most specifically concerned with communication between objects.
a. Command Patterns
Definition:
The Command pattern provides a simple way to separate execution of a
command from the interface environment that produced it.
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request. The Command pattern encapsulates a request as an object.The design abstracts the receiver of the Command from the invoker. The command is issued by the invoker and executed on the receiver.