Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Creational Design Patterns 
 

Creational design patterns provide the best way in which an object can be instantiated.

 

Creational Design Patterns

                         

Creational design patterns provide the best way in which an object can be instantiated. These describes the best way to CREATE object instances . Everyone knows the object instance in Java can be created using a new operator.

Employee emp = new emp ();

The new Operator creates the instance of an object, but this is complex-coding. Creating good application is difficult and so on, complex coding is the last thing one should do. At the present time the very nature of the object, which is created, can change according to the nature of the program. In such scenarios, we can use this pattern to give you more flexible approach.

There are five types of Creational Patterns.

  1. Factory Design Pattern
  2. Abstract Design Factory Pattern
  3. Builder Design Pattern
  4. Prototype Design Pattern
  5. Singleton Design Pattern

Factory Design Pattern :

Let us suppose we have a super class and a sub-class, based on data provided, we have to return the object of one of the sub-classes, we can use factory design pattern. Let's see an example to understand this pattern.

How a Factory Works

In this figure, Employee is a base class and classes Male and FeMale are derived from it. The Factory is a class that decides which of these subclasses to return depending on the arguments you give it. On the right, we define a getEmployee method to be one that passes in some value Name and Sex, and that returns some instance of the class Employee. Which one it returns doesn't matter to the programmer since they all have the same methods, but different implementations. How it decides which one to return is entirely up to the factory. It could be some very complex function but it is often quite simple

Example: Let's suppose an application asks for entering the name and sex of a Employee. If the sex is Male (M), it displays welcome message saying Hello Mr. <Name> and if the sex is Female (F), it displays message saying Hello Miss <Name>.

Sample Code:

Package com.javasree.rajnish;

class Employee {

          //** name string
          public String name;
          // **gender : Male or Female
          private String gender;
          public String getName()
         {
          return name;
          }
          public String getGender()
          {
          return gender;
          }
}

//** End of class

This is a simple class Employee having methods for name and gender. Now, we have two sub-classes, Male and Female, which will print the welcome message on the screen.

class Male extends Employee
{
       public Male(String fullName)
      {
      System.out.println("Hello Mr. "+fullName);
      }
}

//** End of class

Also, Second class is Female

class Female extends Employee {

      public Female(String fullNname)
     {
     System.out.println("Hello Miss. "+fullNname);
     }
}

//** End of class

Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided.

public class SalutationFactory
{

         public static void main(String args[])
        {
        SalutationFactory factory = new SalutationFactory();
        factory.getEmployee(args[0], args[1]);
        }
        public Employee getEmployee(String name, String gender)
        {
        if (gender.equals("Male"))
        return new Male(name);
        else if(gender.equals("Female"))
        return new Female(name);
        else
        return null;
        }

}

//** End of class

This class accepts two arguments from the system at runtime and prints the names.

Compile this Code:

Running this Code: with two argument Name: Rajnish and Sex: Male

Output: Hello Mr. Rajnish

Again Running this Code: with two argument Name: Bhaskar and Sex: FeMale

Output: Hello Miss. Bhaskar

When to use a Factory Pattern?

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.

Download Source Code

                         

» View all related tutorials
Related Tags: c class conversion interface io interfaces client get version ip pattern cli int this tab ie cte work expect to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.