Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Factory Design Pattern Template

Posted on 2012-04-02
3
Medium Priority
?
290 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
  • 2
3 Comments
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 1332 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 1332 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 668 total points
ID: 37800281
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

963 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