How to implement the Singleton Design Pattern


Published on

Learn how to implement the singleton design pattern.

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

How to implement the Singleton Design Pattern

  1. 1. Singleton Design Pattern There Can Be Only One!
  2. 2. OverviewObjectiveLearn how to use the singleton design pattern.Requirements Basics of PHP Basics of ProdigyViewEstimated Time7 minutes
  3. 3. Follow Along of the example code atExample 1. Download a copy With Code 2. Install the system in an environment you feel comfortable testing in. 3. Proceed to examples/data/Singleton.php
  4. 4. 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.
  5. 5. Singleton Visual Single Instance New New New NewInstance Instance Instance Instance All new instances call a single instance
  6. 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 PVObjectProtectedConstructor
  7. 7. 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.
  8. 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 instanceCall the object’s methodGet the object’s instanceCall the object’s methodGet the object’s instanceCall the object’s method
  9. 9. 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.
  10. 10. ResultsYour results may look something similar to this:
  11. 11. 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.
  12. 12. 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: