Solved

Factory Design Pattern Template

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 84
maven project error 5 55
Why does the Android Studio logcat show this error when the user presses the button? 2 46
egit plugin on eclipse 8 63
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

770 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