implementing the singleton design pattern
TRANSCRIPT
-
8/3/2019 Implementing the Singleton Design Pattern
1/12
Singleton Design Pattern
There Can Be Only One!
-
8/3/2019 Implementing the Singleton Design Pattern
2/12
OverviewObjective
Learn how to use the singleton design pattern.
Requirements Basics of PHP
Basics of ProdigyView
Estimated Time
7 minutes
www.prodigyview.com
http://www.prodigyview.com/http://www.prodigyview.com/ -
8/3/2019 Implementing the Singleton Design Pattern
3/12
Follow Along With Code Example1. Download a copy of the example code at
www.prodigyview.com/source.
2.Install the system in an environment you feelcomfortable testing in.
3.Proceed to examples/data/Singleton.php
http://www.prodigyview.com/sourcehttp://www.prodigyview.com/source -
8/3/2019 Implementing the Singleton Design Pattern
4/12
What is a Singleton DesignPattern?
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 only
use the same instance at various points in your code. Thiswill be accomplished using the singleton design pattern.
-
8/3/2019 Implementing the Singleton Design Pattern
5/12
Singleton Visual
SingleInstance
NewInstance
NewInstance
NewInstance
NewInstance
All new instances call a single instance
-
8/3/2019 Implementing the Singleton Design Pattern
6/12
Protect The ConstructorTo force a class to be used as a singleton, we cannot allow theclass to be instantiated with the key word new. To prevent thatmake the__constructor protected. Also extend PVObject orPVPatterns to the class.
DO NOT MAKE THE CONSTRUCTOR PRIVATE OR THISWILL NOT WORK.
Extend PVObject
ProtectedConstructor
-
8/3/2019 Implementing the Singleton Design Pattern
7/12
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/3/2019 Implementing the Singleton Design Pattern
8/12
The RidesSo we have our ticket class. Now lets make some classesthat act as the rides. These classes get the instance of the
ticket class with the getInstance() method.
Get the objects instance
Get the objects instance
Get the objects instance
Call the objects method
Call the objects method
Call the objects method
-
8/3/2019 Implementing the Singleton Design Pattern
9/12
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 theride.
-
8/3/2019 Implementing the Singleton Design Pattern
10/12
ResultsYour results may look something similar to this:
-
8/3/2019 Implementing the Singleton Design Pattern
11/12
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.
-
8/3/2019 Implementing the Singleton Design Pattern
12/12
API ReferenceFor a better understanding of the Collections and the
Iterator, check out the api at the two links below.
PVStaticPatterns
PVPatterns
www.prodigyview.com
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials
http://api.prodigyview.com/api/package/prodigyviewdata-package/pvstaticpatterns-classhttp://api.prodigyview.com/api/package/prodigyviewdata-package/pvpatterns-classhttp://www.prodigyview.com/http://www.prodigyview.com/tutorialshttp://www.prodigyview.com/tutorialshttp://www.prodigyview.com/tutorialshttp://www.prodigyview.com/tutorialshttp://www.prodigyview.com/http://api.prodigyview.com/api/package/prodigyviewdata-package/pvpatterns-classhttp://api.prodigyview.com/api/package/prodigyviewdata-package/pvstaticpatterns-class