So your core code won't break at all.
Singleton Pattern
One of the most used design patterns is Singleton. This pattern solves a very
significant problem in object oriented programming and saves the lives of millions of
programmers in practical programming.
The main purpose of the Singleton pattern is to deliver a single instance of object no
matter how many times you instantiate it. That is, if an object is instantiated once,
using the Singleton pattern you can deliver only that instance when you require
it again in your code. This saves memory consumption by preventing the creation
of multiple instances of an object. Thus Singleton pattern is used to improve the
performance of your application.
GoogleDoc
Adapter Writely
Doc Manager Context
Design Patterns
[ 80 ]
Let's take the MySQLManager class, which we created in the previous example. Now
we are adding a single instance feature using Singleton pattern.
class MySQLManager
{
private static $instance;
public function __construct()
{
if (!self::$instance)
{
self::$instance = $this;
echo "New Instance\n";
return self::$instance;
}
else
{
echo "Old Instance\n";
return self::$instance;
}
}
//keep other methods same
}
?>
Now let us see how it actually works.
Pages:
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99