History Monuments Details

Read every monuments of country in detail, Check out how they created, about the history and hidden things against each monuments.

Latest News about the world

Be active and pro to have latest news, here you can find latest news with out breaks.

Get the latest events happening in world

Avail the service to be updated about the unseen/unavailable events happening in the world.

Lets hit the new Technologies

Be Pro by Learning new technologies free with latest blogs written by us at techytreat.blogspot.in

Learn new things about the country

Here you can learn new and unique things about country.

Showing posts with label Creational Pattern. Show all posts
Showing posts with label Creational Pattern. Show all posts

Tuesday, 3 July 2012

What is Factory Design Patterns?


Creational Patterns
     -Factory Pattern

Factory of classes. In simple words, if we have a super class and nth 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.

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  Welcome to application,  Ms <Name>.

The skeleton of the code can be given here.

public class Person {
// name stringpublic String name;// gender : M or Fprivate 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(" Welcome to application, Mr. "+fullName);}
}// End of class

Also, the class Female

public class Female extends Person {
public Female(String fullNname) {System.out.println("Welcome to application, 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);elsereturn 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 Bhanu and M:
java Bhanu M
The result returned is: “Hello Mr. Bhanu”.

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.

Friday, 4 May 2012

What is Singleton Design Pattern?

Singleton Pattern is one of the well known and ask most of the times in interview questions in design patterns, if I am not wrong then 90  percent of the cases singleton pattern is asked, but I don't understand why this is so famous may be due to its criticality and  frequently occur problem in software coding, WHY, WHY WHY...

Is any one has the answer, Think about Performance!!!.

Performance is one of the target for any application and singleton pattern plays a vital role in improving performance, Imagine the application with lots of objects created and none of the object is destroyed by their own and there is  mechanism to destroy of these objects, Now I don't think anyone will agree for such design or application. Yes here is Singleton pattern come in picture. Create a single instance of a class and use it through out the application.

One scenario I am missing here what will happen, when no of component of application uses single instance of a class, Doesn't this hamper the application performance. Do we need to create every class as singleton.

I think No No No. So question is Where to use Singleton pattern?

Singleton pattern is generally used to service instance independent or static data where multiple threads can access data at same time. example can be logging class.

Singletons often control access to resources such as database connections or sockets. For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time. If you add database connections or use a JDBC driver that allows multithreading, the Singleton can be easily adjusted to allow more connections.


How to follow or implement Singleton Design Pattern?

Follow the below suedo to implement singleton pattern in class:
  1. Create a private static variable of a class
  2. Create private construtor so that constructor can be called from outside.
  3. Create a public method which will return the instance of class created at step 1.

public class MySingleton { 
 private static MySingleton _instance; 

 private MySingleton() { 
 } 

 public static synchronized MySingleton getInstance() { 
  if (_instance==null) { 
   _instance = new MySingleton(); 
  } 
  return _instance; 
 } 

Thanks for the reading please do comment to facilitate you better.

Site Search