Docstoc

2009 Singleton

Document Sample
2009 Singleton Powered By Docstoc
					                            Singleton Pattern


            Singleton

               Problem: Exactly one instance of a certain object is
               required (this object is called a singleton). We must
               ensure a class has only one instance.

               Solution: use a static method of the class to return
               the singleton

               Factories and Facades are usually singletons.

               Textbook problem: how do we control the
               instantiation of the ServicesFactory?


Fall 2009                       ACS-3913   R McFadyen                  1
                           Singleton Pattern


Singleton

In Java, you would have code such as:

Public static synchronized ServicesFactory
getInstance()
{
If (instance == null)
   Instance := new ServicesFactory()
Return instance
}

The point of the above is that only one instance is ever
created. If one already exists, then the create is bypassed and
the reference to the existing object is returned.

  Fall 2009                    ACS-3913   R McFadyen              2
                           Singleton Pattern


Singleton

In Java, you would have code such as:

Public static synchronized ServicesFactory
getInstance()
{
If (instance == null)
   Instance := new ServicesFactory()
Return instance
}

The “synchronized” keyword ensures that only one “thread”
runs this code at a time. The text refers to a “critical section”.


  Fall 2009                    ACS-3913   R McFadyen                 3
                              Singleton Pattern

Suppose an instance of the ServicesFactory is created and,
later on, a message, getTaxCalculatorAdapter(), is passed to
this instance.



        :Register             ServicesFactory

              getInstance()
                                             new()
                                                          :ServicesFactory

              getTaxCalculatorAdapter()




 Fall 2009                        ACS-3913   R McFadyen                      4
                            Singleton Pattern

                            By applying the <<singleton>> stereotype to this object, they
                            are implying that the getInstance method was used to
                            instantiate it.



                             <<Singleton>>
       :Register            :ServicesFactory

            getAccountingAdapter()
                                            new()
                                                         :AccountingAdapter

                   post()




Fall 2009                        ACS-3913   R McFadyen                                5
                             Singleton Pattern

The text makes reference to Lazy Instantiation – The
ServicesFactory object was created when it was needed, and
not before

                              <<Singleton>>
        :Register            :ServicesFactory

             getAccountingAdapter()
                                            new()
                                                         :AccountingAdapter

                    post()




 Fall 2009                       ACS-3913   R McFadyen                        6
                                  Patterns

Quote from text
“To handle the problem of varying interfaces for external
services, let’s use Adapters generated from a Singleton
Factory”


                            <<Singleton>>
        :Register          :ServicesFactory

             getAccountingAdapter()
                                            new()
                                                         :AccountingAdapter

                    ...



 Fall 2009                       ACS-3913   R McFadyen                        7

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:3/30/2013
language:Unknown
pages:7