Your SlideShare is downloading. ×
0
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
How to implement the Singleton Design Pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

How to implement the Singleton Design Pattern

1,172

Published on

Learn how to implement the singleton design pattern.

Learn how to implement the singleton design pattern.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,172
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
30
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Singleton Design Pattern There Can Be Only One!
  • 2. OverviewObjectiveLearn how to use the singleton design pattern.Requirements Basics of PHP Basics of ProdigyViewEstimated Time7 minutes www.prodigyview.com
  • 3. 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
  • 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. Singleton Visual Single Instance New New New NewInstance 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 PVObjectProtectedConstructor
  • 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. 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. 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. ResultsYour results may look something similar to this:
  • 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. 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

×