Upload
prodigyview
View
1.008
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Learn how to implement the singleton design pattern.
Citation preview
Singleton Design Pattern
There Can Be Only One!
OverviewObjective
Learn how to use the singleton design pattern.
Requirements
Basics of PHP
Basics of ProdigyView
Estimated Time
7 minutes
www.prodigyview.com
Follow Along With Code Example
1. Download a copy of the example code at 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 most common design patterns in PHP. The pattern can be described as restricting the instances of an object.
In other words, the keyword ‘new’ may only be used once on this object.
There may come situations when developing that you will only want one instance of an object to be used and only use the same instance at various points in your code. This will be accomplished using the singleton design pattern.
Singleton Visual
Single Instance
New Instance
New Instance
New Instance
New Instance
All new instances call a single instance
Protect The ConstructorTo 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 PVObject
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 RidesSo 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 instance
Get the object’s instance
Get the object’s instance
Call the object’s method
Call the object’s method
Call 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 – check
We are ready to start taking the tickets and enjoying the ride.
ResultsYour results may look something similar to this:
An ExplanationHere 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 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