Tuesday, November 23, 2010

Monday, January 5, 2009

Test Blog

 

Regards,

Gopi



http://www.mindtree.com/email/disclaimer.html

Factory design pattern

FACTORY DESIGN PATTERN
The Factory Method pattern deals with situations where at runtime one of several similar classes must be created.
In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
Let’s take an example to understand this pattern.

Example: Let’s suppose an application asks for entering the name and sex of a person. 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 HelloMs <Name>.

The skeleton of the code can be given here.

public class Person {

 

// name string
public String name;
// gender : M or F
private String gender;

public String getName() {
return name;
}

public String getGender() {
return gender;
}

}// End of class

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

public class Male extends Person {

 

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

}// End of class

 

Also, the class Female

public class Female extends Person {

 

public Female(String fullNname) {
System.out.println("Hello Ms. "+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.getPerson(args[0], args[1]);
}

public Person getPerson(String name, String gender) {
if (gender.equals("M"))
return new Male(name);
else if(gender.equals("F"))
return new Female(name);
else
return null;
}

}// End of class

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

Running the program:

After compiling and running the code on my computer with the arguments Prashant and M:

java Prashant M

The result returned is: “Hello Mr. Prashant”.

 

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.

 

Regards

Manju K



http://www.mindtree.com/email/disclaimer.html

Hide Your Data

Hide Your Data

---------------------------------------------

This is an admonition to hide the internal details of your classes, and only expose functionality through methods and/or constants (public static finals). Basically the thing you must do is make all your member fields private.

 

The admonition is to make all member variables private (except class constants), and write methods for getting and setting them (called getters and setters, respectively).

 

What does this get you? One word: control. Later, after you’ve written thousands of lines of code that uses your getter and setter methods, you can play tricks. The getters and setters provide a place where you can wedge in little bits of code around accesses to those fields. And not only can you do this in the original class, you can do it in subclasses, which is even better.

 

Regards,

Gopi



http://www.mindtree.com/email/disclaimer.html

Blog Introduction

Hello Reader,
Thanks for reading our blog. We created this blog because we wanted to discuss our technology related learning’s and share with each other.
The Group member consist of following members…
  • Satya Prakash
  • Sowmya Devdas
  • Guru Gowdar
  • Gopinath
  • Manju Kannan &
  • Rajesh
    So this is called: S2G2MR Tech Discussion