Singleton Design Pattern




       There Can Be Only One!
Overview
Objective

Learn how to use the singleton design pattern.

Requirements

 Basics of PHP
 Basics of ProdigyView
Estimated Time

7 minutes


                     www.prodigyview.com
Follow Along of the example code atExample
  1. Download a copy
                     With Code
    www.prodigyview.com/source.

  2. Install the system in an environment you feel
    comfortable testing in.

  3. Proceed to examples/data/Singleton.php
What is a Singleton Design
            Pattern?
The singleton design pattern may be one of the most
common design patterns in PHP. The pattern can be
described as restricting the instances of an object.

In other words, the keyword ‘new’ may only be used once
on this object.

There may come situations when developing that you will
only want one instance of an object to be used and only
use the same instance at various points in your code. This
will be accomplished using the singleton design pattern.
Singleton Visual
                          Single
                         Instance




  New              New                    New           New
Instance         Instance               Instance      Instance



           All new instances call a single instance
Protect The Constructor
     To force a class to be used as a singleton, we cannot allow the
     class to be instantiated with the key word new. To prevent that
     make the __constructor protected. Also extend PVObject or
     PVPatterns to the class.

     DO NOT MAKE THE CONSTRUCTOR PRIVATE OR THIS
     WILL NOT WORK.

Extend PVObject




Protected
Constructor
Object::getInstance()
In our previous slide we extended PVObject class. Using
PVObject or PVPatterns class, they both have a method
called getInstance().

This method will create a single instance of the object and
store it. Anytime that getInstance() is called, it will
retrieve the same instance of the object.

The getInstance() is a replacement for the ‘new’
operator, meaning ‘new Object’ will never be used.
The Rides
     So we have our ticket class. Now lets make some classes
     that act as the rides. These classes get the instance of the
     ticket class with the getInstance() method.



Get the object’s instance
Call the object’s method




Get the object’s instance
Call the object’s method




Get the object’s instance
Call the object’s method
Using our Tickets
 Ticket Class that has tickets – check
 Rides that use the ticket class – check
 Instance of the ticket class – check
We are ready to start taking the tickets and enjoying the
ride.
Results
Your results may look something similar to this:
An Explanation
Here is what happened to make the result. When the instance
of the class was created, all the variables in that class
becomes usable.

Because only one instance is made, the variables inside the
object are always in the same state. Even though the instance
was being used inside different classes, it was still the same
instance and therefore the same variables being used.

In our example tickets, we are always subtracting from the
same ticket variable inside of the Tickets object because we
are only using one instance of ticket.
API Reference
For a better understanding of the Collections and the
Iterator, check out the api at the two links below.

PVStaticPatterns

PVPatterns


                 More Tutorials
For more tutorials, please visit:

http://www.prodigyview.com/tutorials


                       www.prodigyview.com

How to implement the Singleton Design Pattern

  • 1.
    Singleton Design Pattern There Can Be Only One!
  • 2.
    Overview Objective Learn how touse the singleton design pattern. Requirements  Basics of PHP  Basics of ProdigyView Estimated Time 7 minutes www.prodigyview.com
  • 3.
    Follow Along ofthe example code atExample 1. Download a copy With Code www.prodigyview.com/source. 2. Install the system in an environment you feel comfortable testing in. 3. Proceed to examples/data/Singleton.php
  • 4.
    What is aSingleton Design Pattern? The singleton design pattern may be one of the most common design patterns in PHP. The pattern can be described as restricting the instances of an object. In other words, the keyword ‘new’ may only be used once on this object. There may come situations when developing that you will only want one instance of an object to be used and only use the same instance at various points in your code. This will be accomplished using the singleton design pattern.
  • 5.
    Singleton Visual Single Instance New New New New Instance Instance Instance Instance All new instances call a single instance
  • 6.
    Protect The Constructor To force a class to be used as a singleton, we cannot allow the class to be instantiated with the key word new. To prevent that make the __constructor protected. Also extend PVObject or PVPatterns to the class. DO NOT MAKE THE CONSTRUCTOR PRIVATE OR THIS WILL NOT WORK. Extend PVObject Protected Constructor
  • 7.
    Object::getInstance() In our previousslide we extended PVObject class. Using PVObject or PVPatterns class, they both have a method called getInstance(). This method will create a single instance of the object and store it. Anytime that getInstance() is called, it will retrieve the same instance of the object. The getInstance() is a replacement for the ‘new’ operator, meaning ‘new Object’ will never be used.
  • 8.
    The Rides So we have our ticket class. Now lets make some classes that act as the rides. These classes get the instance of the ticket class with the getInstance() method. Get the object’s instance Call the object’s method Get the object’s instance Call the object’s method Get the object’s instance Call the object’s method
  • 9.
    Using our Tickets Ticket Class that has tickets – check  Rides that use the ticket class – check  Instance of the ticket class – check We are ready to start taking the tickets and enjoying the ride.
  • 10.
    Results Your results maylook something similar to this:
  • 11.
    An Explanation Here iswhat happened to make the result. When the instance of the class was created, all the variables in that class becomes usable. Because only one instance is made, the variables inside the object are always in the same state. Even though the instance was being used inside different classes, it was still the same instance and therefore the same variables being used. In our example tickets, we are always subtracting from the same ticket variable inside of the Tickets object because we are only using one instance of ticket.
  • 12.
    API Reference For abetter understanding of the Collections and the Iterator, check out the api at the two links below. PVStaticPatterns PVPatterns More Tutorials For more tutorials, please visit: http://www.prodigyview.com/tutorials www.prodigyview.com