Implementing The Singleton Design Pattern
Implementing The Singleton Design Pattern
Overview
Objective
www.prodigyview.com
Follow Along of the example code atExample With Code 1. Download a copy
www.prodigyview.com/source.
3. Proceed to examples/data/Singleton.php
Singleton Visual
Single Instance
New Instance
New Instance
New Instance
New Instance
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.
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