Solved

Factory Design Pattern Template

Posted on 2012-04-02
3
286 Views
Last Modified: 2012-08-14
I came across the following code

public class MessageSupportFactory
{
    private static MessageSupportFactory instance = null;

    private MessageSupportFactory()
    {
     .....
    }

   static { 
         instance = new MessageSupportFactory(); 
         }

   public static MessageSupportFactory getInstance()
   {
      return instance;
   }

.....
}

Open in new window



The question is when should we use a template like this and what does it accomplish?
Thank you.
0
Comment
Question by:skiabox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 333 total points
ID: 37799813
The code(template) in MessageSupportFactory class is more about singleton pattern . Singleton pattern is where you want a single instance of the class . The constructor is made private here , so that clients cannot instantiate it .The only way to get an instance of this class is the getInstance() method.  Singletons are used mostly for resources like connectionfactories , applicationcontexts etc.
0
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 333 total points
ID: 37799861
If you are looking for the Factory design pattern -  As its name implies classes implementing this pattern act as factories that produce instances . Suppose you want to get a connection object to an underlying database . Instead of directly using the new operator and instantiating the connection object , you would approach the factory class (say , ConnectionFactory) to give you an instance of the connection object.

The advantage of using factory pattern . Tomorrow , if you move to a different database vendor , you need not change client code to implement connection logic for that specific vendor . Instead , the connectionFactory (which would have been coded to return specific instances for specific DB vendors) , would handle that

So , in simpler terms , this pattern  creates an abstraction over how classes(in our example , connections) are created
0
 
LVL 37

Accepted Solution

by:
zzynx earned 167 total points
ID: 37800281
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question