Singleton Design Pattern There Can Be Only One!
OverviewObjectiveLearn how to use the singleton design pattern.Requirements Basics of PHP Basics of ProdigyViewEstimated Time7 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 mostcommon design patterns in PHP. The pattern can bedescribed as restricting the instances of an object.In other words, the keyword ‘new’ may only be used onceon this object.There may come situations when developing that you willonly want one instance of an object to be used and onlyuse the same instance at various points in your code. Thiswill be accomplished using the singleton design pattern.
Singleton Visual Single Instance New New New NewInstance 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 PVObjectProtectedConstructor
Object::getInstance()In our previous slide we extended PVObject class. UsingPVObject or PVPatterns class, they both have a methodcalled getInstance().This method will create a single instance of the object andstore it. Anytime that getInstance() is called, it willretrieve 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 instanceCall the object’s methodGet the object’s instanceCall the object’s methodGet the object’s instanceCall 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 – checkWe are ready to start taking the tickets and enjoying theride.
ResultsYour results may look something similar to this:
An ExplanationHere is what happened to make the result. When the instanceof the class was created, all the variables in that classbecomes usable.Because only one instance is made, the variables inside theobject are always in the same state. Even though the instancewas being used inside different classes, it was still the sameinstance and therefore the same variables being used.In our example tickets, we are always subtracting from thesame ticket variable inside of the Tickets object because weare only using one instance of ticket.
API ReferenceFor a better understanding of the Collections and theIterator, check out the api at the two links below.PVStaticPatternsPVPatterns More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com