Sunday, December 26, 2004

Patterns 1 : Creational patterns

Some of this material is from the java patterns book by James cooper.*

Factory Pattern

Factory patterns helps localizing the generation of certain obects to one place in code. That way we have control over how and what object is created.

Factory Method

It provides a simple decision making class that returns one of several possible subclasses of an abstract base class depending on the data that are provided.

When to Use a Factory Pattern
You should consider using a Factory pattern when
· A class can’t anticipate which kind of class of objects it must create.
· A class uses its subclasses to specify which objects it creates.
· You want to localize the knowledge of which class gets created.

There are several similar variations on the factory pattern to recognize.
1. The base class is abstract and the pattern must return a complete working class.
2. The base class contains default methods and is only subclassed for cases where the default methods are insufficient.
3. Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different.

Abstract Factory Method
It provides an interface to create and return one of several families of related objects.

The Abstract Factory pattern is one level of abstraction higher than the factory pattern. You can use this pattern when you want to return one of several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories.

Consequences of Abstract Factory
One of the main purposes of the Abstract Factory is that it isolates the concrete classes that are generated. The actual class names of these classes are hidden in the factory and need not be known at the client level at all. Because of the isolation of classes, you can change or interchange these product class families freely. Further, since you generate only one kind of concrete class, this system keeps you for inadvertently using classes from different families of products. However, it is some effort to add new class families, since you need to define new, unambiguous conditions that cause such a new family of classes to be returned.

Builder Pattern
It separates the construction of a complex object from its representation, so that several different representations can be created depending on the needs of the program.

Prototype Pattern
It starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.
The Protoype pattern is used when creating an instance of a class is very time-consuming or complex in some way. Then, rather than creating more instances, you make copies of the original instance, modifying them as appropriate.

Noramlly in java you use clone method to shallwo copy an object.

MyObject mNew = (MyObject)mOld.clone();

But to do a deepcopy you do this. Remember that the Object tobeDeepCloned has to have all of its child objects as serializable.

public Object deepClone(Object tobeDeepCloned)
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(b);

ByteArrayInputStream bIn = new ByteArrayInputStream(b.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bIn);
return (oi.readObject());
catch (Exception e)
return null;

Singleton Pattern
It is a class of which there can be no more than one instance. It provides a single global point of access to that instance.


Post a Comment

<< Home