Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Tutorials
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Factory Pattern

                         

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 create 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");
}
}

Also, the Not an Administrator

public class NotAnAdministrator extends Person {
public NotanAdministrator(String fullNname) {
System.out.println("Hello Mr. " + fullNname + "you are not the administrator of this organization");
}
}

Now, we will create the Verification class which will return a message depending on the data provided.

public class Verification {
public static void main(String args[]) {
Verification verify = new Verification();
verify.getEmployee(args[0], args[1]);
}
public Employee getEmployee(String name, String Id) {
if ((name.equals("Zulfiqar")&&(Id.equals("123542")))
return new Administrator(name);
else
return new NotanAdministrator(name);
}
}

This class takes two arguments from the system at runtime and prints the names and a message accordingly.

Running the program:
After compilation, run the code with the arguments Zulfiqar and 123542:

java Zulfiqar 123542

The result returned is: “Hello Mr. Zulfiqar You are the administrator of the organization”.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.