Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Factory Design Pattern Template

Posted on 2012-04-02
3
Medium Priority
?
287 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …
Suggested Courses

688 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