|
Creational Design Pattern
|
|
Design pattern includes creational, structural, and
behavioral patterns. But here we are going to cover few
of the creational patterns.
All the creational design patterns define the best possible way
in which an object can be instantiated. These describes the
best way to CREATE object instances. Creational design
pattern tries to reduce the creational complexities (of an
object) at its maximum level. It’s not only important to understand
how to use, but also equally important when to use
patterns.
I. Factory Pattern:
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.
A class may need it’s subclasses to specify the objects to be
created or delegate responsibility to one of several helper
subclasses so that knowledge can be localized to specific
helper subclasses. Even Sometimes, an Application (or
framework) at runtime, is not able to judge properly the class
of an 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.
Creational design pattern, more specifically the Factory
Pattern tries to resolve out such issues. Factory Method is a
creational pattern. This pattern helps to model an interface for
creating an object which at creation time can let its subclasses
to decide which class to instantiate. We call this a
Factory Pattern since it is responsible for “Manufacturing” an
Object. It helps to instantiate the appropriate subclass by
creating the right object from a group of related classes.
The Factory Pattern promotes loose coupling by eliminating
the need to bind application-specific classes into
the code. It is an object oriented design pattern that returns
an instance of one of the several possible classes based on
the data passed to it. Generally all the classes that it returns
have common methods and also have a common parent class.
It should be noted that all the subclasses performs the different
task and is optimized for different kind of data.
A factory is not needed to make an object. A simple call to
new will do it for you. However, the use of factories gives the
programmer the opportunity to abstract the specific attributes
of an object into specific subclasses which create them. The
Factory Pattern is all about “Define an interface for creating an
object, but let the subclasses decide which class to instantiate
.
The Factory method lets a class defer instantiation to
subclasses” |
|
Benefits: Factory pattern allows the subclasses to choose the
type of objects to create. One of the other benefit of factory
pattern is that many of the methods created using the factory
method technique do not depend on subclasses, this means
that it is beneficial to define the methods using the factory
method technique that are used to create objects to gain the
other benefits. These methods are known as static methods.
Usage: Classes that are parallel in hierarchies usually require
objects of one hierarchy for creating appropriate objects from
another. Factory methods are mostly used in frameworks and
toolkits where library codes required to creating the objects of
specific types that may be subclassed by applications with
the help of framework.
The Factory patterns can be used in following cases:
1 When a class does not know which class of objects it
must create.
2 A class specifies its sub-classes to specify which
objects to create.
3 In programmer’s language (very raw form), you can use
factory pattern where you have to create an object of
any one of sub-classes depending on the data provided.
Let’s try to understand this pattern with an example.
Example: Let’s take an application that ask to enter the name
and the Id of a person. If the person is the administrator then it
displays welcome message saying Hello Mr. <Name> You are
the administrator of the organization otherwise it displays a
message saying Hello Mr. <Name> you are not the administrator
of the organization.
The skeleton of the code can be given here.
public class Employee {
public String name;
private String Id = “123542”;
public String getName() {
return name;
}
public String getId() {
return Id;
}}
This is a simple Employee class that contains the methods for
name and Id. Now, we take two sub-classes, Administrator
and NotAnAdministrator which will print a message on the
screen accordingly.
public class Administrator extends Employee {
public Employee(String fullName) {
System.out.println(“Hello Mr. “+ fullName + “You are the
administrator of this organization”);
}} |
|
August 2007 | Java Jazz Up |25 |
|
|
|
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Download PDF |
|
|
|
|
|
|
|
|