Java Design Pattern
Java Design Pattern
Factory Pattern
Abstract Factory Pattern
Builder Pattern
Prototype Pattern
Singleton Pattern
Factory Pattern
The Problem: One of the goals of object-oriented
design is to delegate responsibility among different
objects. This kind of partitioning is good since it
encourages Encapsulation and Delegation.
• Sometimes, an Application (or framework) at runtime,
cannot anticipate the class of object that it must
create. The Application (or framework) may know that
it has to instantiate classes, but it may only know
about abstract classes (or interfaces), which it cannot
instantiate. Thus the Application class may only know
when it has to instantiate a new Object of a class, not
what kind of subclass to create.
• a class may want it's subclasses to specify the objects
to be created.
• a class may delegate responsibility to one of several
helper subclasses so that knowledge can be localized
Factory Pattern (cont…)
Cloning In Java:
Prototype Pattern (cont…)
Singleton Pattern
“Ensure a class only has one instance, and provide a
global point of access to it.”
class PrintSpooler
{
static boolean instance_flag = false; //true if 1 instance
public PrintSpooler() throws Singleton Exception
{
if (instance_flag)
throw new SingletonException("Only one spooler
allowed");
else
instance_flag = true; //set flag for 1 instance
System.out.println("spooler opened");
}
public void finalize()
{
instance_flag = false; //clear if destroyed
}
Singleton Pattern (cont…)
class PrintSpooler
{
static PrintSpooler printSpooler;
private PrintSpooler()
{
// ----------
}
public PrintSpooler getSpoolerInstance()
{
if(printSpooler == null)
{
printSpooler = new PrintSpooler();
}
Structural Pattern
Structural patterns describe how classes and
objects can be combined to form larger
structures. The difference between class patterns
and object patterns is that class patterns describe
how inheritance can be used to provide more
useful program interfaces. Object patterns, on the
other hand, describe how objects can be
composed into larger structures using object
composition, or the inclusion of objects within
other objects.
Type of Structural Patterns
There are seven type of Structural
patterns
Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Facade Pattern
Flyweight Pattern
Proxy Pattern
Adapter Pattern
“This pattern establishes a relationship between the
two unrelated interfaces such that they work together. This
is similar to the conversion of one interface of one class to
the interface expected by the client.”
A facade can:
– make a software library easier to use and understand, since
the facade has convenient methods for common tasks;
– make code that uses the library more readable, for the same
reason;
– reduce dependencies of outside code on the inner workings of
a library, since most code uses the facade, thus allowing more
flexibility in developing the system
– wrap a poorly-designed collection of APIs with a single well-
designed API.
An Adapter is used when the wrapper must respect a
particular interface and must support a polymorphic
behavior. On the other hand a facade is used when one
Facade Pattern
Flyweight Pattern
• There are cases in programming where it seems that you
need to generate a very large number of small class
instances to represent data. Sometimes you can greatly
reduce the number of different classes that you need to
instantiate if you can recognize that the instances are
fundamentally the same except for a few parameters. If you
can move those variables outside the class instance and
pass them in as part of a method call, the number of
separate instances can be greatly reduced.
Proxy Pattern