1
Software Design
Lecture : 28
2
3
4
Categories of Design Patterns
Creational
Structural
Behavioral
5
Creational Design Patterns
Deal with one of the most commonly performed tasks in an OO
application, the creation of objects.
Support a uniform, simple, and controlled mechanism to create
objects.
Allow the encapsulation of the details about what classes are
instantiated and how these instances are created.
Encourage the use of interfaces, which reduces coupling.
6
Factory Pattern
Problem Statement:
When a client object does not know which class to instantiate, it can
make use of the factory method to create an instance of an
appropriate class from a class hierarchy or a family of related
classes. The factory method may be designed as part of the client
itself or in a separate class. The class that contains the factory
method or any of its subclasses decides on which class to select and
how to instantiate it.
7
Factory Pattern Description
If there exist class hierarchies ie super / sub classes then client object
usually know which class /sub class to instantiate but at times client object
know that it needs t instantiate the object but of which class it does not
know ; it may be due to many factors
The state of the running application
Application configuration settings
Expansion of requirements or enhancements
In such cases, an application object needs to implement the class selection
criteria to instantiate an appropriate class from the hierarchy to access its
services
8
Disadvantage of this Approach
Because every application object that intends to use the services
offered by the class hierarchy needs to implement the class selection
criteria, it results in a high degree of coupling between an
application object and the service provider class hierarchy.
Whenever the class selection criteria change, every application object
that uses the class hierarchy must undergo a corresponding change.
Shown in the next picture
9
10
Solution :Factory Pattern
“Factory Pattern defines an interface for creating the object but let
the subclass decide which class to instantiate. Factory pattern let the
class defer instantiation to the sub class “
It works on the principle of delegation
11
Solution Description
Encapsulating the functionality required, to select and instantiate an
appropriate class separately.
Selects an appropriate class from a class hierarchy based on the
application context and other influencing factor
Instantiates the selected class and returns it as an instance of the
parent class type
This approach will decouple the client from object creation
12
Factory Method
Application objects can make use of the factory method to get access to
the appropriate class instance. This eliminates the need for an
application object to deal with the varying class selection criteria.
Besides the class selection criteria, the factory method also implements
any special mechanisms required to instantiate the selected class. This
is applicable if different classes in the hierarchy need to be instantiated
in different ways. The factory method hides these details from
application objects and eliminates the need for them to deal with these
intricacies.
13
Possible Class Diagram of Factory
Pattern
14
Simple Example with Java Code
• Problem Statement:
We want the user to enter the name in either “first name last name
or last name, first name” format.
We have made the assumption that there will always be a comma
between last name and first name and space between first name last
name
15
Class Diagram
NameFactory
Namer
Firstfirst
Lastfist
Application
Object
Passes
Parameter
Handles
Creation
of
Subclass object and return
relevant instance of subclass
16
Sample Code in Java
17
Namer Class
class Namer
{
protected String last; //store last name here
protected String first; //store first name here
public String getFirst()
{
return first; //return first name
}
public String getLast() {
return last; //return last name
}}
18
class FirstFirst extends Namer
{ //split first last
public FirstFirst(String s)
{
int i = s.lastIndexOf(" "); //find sep space
if (i > 0)
{
//left is first name
first = s.substring(0, i).trim();
//right is last name
last =s.substring(i+1).trim();
}
else
{
first = “”; // put all in last name
last = s; // if no space
}}}
19
class LastFirst extends Namer
{ //split last, first
public LastFirst(String s)
{
int i = s.indexOf(","); //find comma
if (i > 0)
{
//left is last name
last = s.substring(0, i).trim();
//right is first name
first = s.substring(i + 1).trim();
}
else {
last = s; // put all in last name
first = ""; // if no comma
}}}
20
Building the Factory
21
class NameFactory
{
//returns an instance of LastFirst or FirstFirst
//depending on whether a comma is found
public Namer getNamer(String entry) {
int i = entry.indexOf(","); //comma determines name
order
if (i>0)
return new LastFirst(entry); //return one class
else
return new FirstFirst(entry); //or the other
}}
22
Testing the Factory
23
public class testFactory
{
public static void main(String args[])
{
NameFactory nfactory = new NameFactory();
String name=“Ali khan”;
Namer namer = nfactory.getNamer(name); Delegation
//compute the first and last names
//using the returned class
System.out.println(namer.getFirst());
System.out.println(namer.getLast());
}}