Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
126 views

Implementing The Singleton Design Pattern

This document discusses the singleton design pattern in PHP. The singleton pattern restricts the instantiation of a class to one instance. It explains how to implement a singleton by making the constructor protected and using a static getInstance() method to return the single instance. The document provides an example of a ticket class that uses the singleton pattern, with ride classes accessing the single ticket instance.

Uploaded by

ProdigyView
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Implementing The Singleton Design Pattern

This document discusses the singleton design pattern in PHP. The singleton pattern restricts the instantiation of a class to one instance. It explains how to implement a singleton by making the constructor protected and using a static getInstance() method to return the single instance. The document provides an example of a ticket class that uses the singleton pattern, with ride classes accessing the single ticket instance.

Uploaded by

ProdigyView
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Singleton Design Pattern

There Can Be Only One!

Overview
Objective

Learn how to use the singleton design pattern.


Requirements

Basics of PHP Basics of ProdigyView


Estimated Time 7 minutes

www.prodigyview.com

Follow Along of the example code atExample With Code 1. Download a copy
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 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 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 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 objects instance


Call the objects method

Get the objects instance Call the objects method

Get the objects instance Call the objects 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.

Results
Your results may look something similar to this:

An Explanation
Here 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 Reference
For a better understanding of the Collections and the Iterator, check out the api at the two links below. PVStaticPatterns PVPatterns

More Tutorials
For more tutorials, please visit: http://www.prodigyview.com/tutorials

www.prodigyview.com

You might also like