Solved

Factory Design Pattern Template

Posted on 2012-04-02
3
279 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now