To add to my growing library of Design Patterns in PHP 5 I have
written what I think is a good example of the Singleton Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
In the classic singleton pattern an object will distribute one and
only one instance of itself. This can be useful for the sharing of
resources such as a single db or network connection.
A variation, sometimes called a multiton, would distribute a limited
number of instances of itself. Useful, say if you had a limited
number of db connections to share. 9 2965
FluffyCat said the following on 14/01/2006 00:20: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
Unfortunately, this is not a good example of a singleton system.
You have a class (BookSingleton) with a public constructor, so there's
nothing to stop someone creating multiple BookSingleton objects with
multiple "new BookSingleton() " calls.
To solve this problem, you need to make the constructor private, and the
borrowBook() method static.
P.S. Another thing to make your code more readable would be to syntax
highlight it...
--
Oli
On 2006-01-14, Oli Filth <ca***@olifilth .co.uk> wrote: FluffyCat said the following on 14/01/2006 00:20: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
Unfortunately, this is not a good example of a singleton system.
You have a class (BookSingleton) with a public constructor, so there's nothing to stop someone creating multiple BookSingleton objects with multiple "new BookSingleton() " calls.
To solve this problem, you need to make the constructor private, and the borrowBook() method static.
And make sure to throw an error when clone is called.. Because a singleton
shouldn't be cloned :)
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be >
> Unfortunately, this is not a good example of a singleton system.
I agree. Singletons are often used where, in procedural programming, one
would use a global variable or function. Also, the root of a library or
package can be a singleton. Examples:
$connection = Database->OpenConnection (...);
The Database class is here the root of the database package, and acts as
the root of (or the gate to) the entire database functionality. There is
no need to instantiate Database as such, because it is stateless and not
really an instance. It is more the "switchboar d" that tells you where to
find the database functionality. You might even argue that a singleton
is something like a namespace.
An example of a "global variable" is, for instance, Visual Basic's Err
object, that is used for error handling. You could do that with a singleton:
ErrorLog->AddMessage(... );
The actual instance of the log object then remains "hidden" within the
ErrorLog singleton. Naturally, you could also use the namespace-like
structure:
ErrorHandling->Log()->AddMessage(... );
Where the log instance is visible, but should only be instantiated by
the ErrorHandling singleton.
Best regards
>To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
In the classic singleton pattern an object will distribute one and only one instance of itself. This can be useful for the sharing of resources such as a single db or network connection.
Why would one assume that there is only one instance of a database
connection or only one network connection? Isn't that almost as
bad as assuming that *THE ONLY* user presses *THE ONLY* key on
*THE ONLY* keyboard with *THE ONLY* finger?
Gordon L. Burditt
Gordon Burditt wrote: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
In the classic singleton pattern an object will distribute one and only one instance of itself. This can be useful for the sharing of resources such as a single db or network connection.
Why would one assume that there is only one instance of a database connection or only one network connection? Isn't that almost as bad as assuming that *THE ONLY* user presses *THE ONLY* key on *THE ONLY* keyboard with *THE ONLY* finger?
Gordon L. Burditt
Oh you're gonna hate yourself in the morning... ;-)
The classic database connection singleton is to a connection pool not a
single connection.
-david-
On Sat, 14 Jan 2006 16:30:49 -0500, David Haynes
<da***********@ sympatico.ca> wrote: Gordon Burditt wrote: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
In the classic singleton pattern an object will distribute one and only one instance of itself. This can be useful for the sharing of resources such as a single db or network connection.
Why would one assume that there is only one instance of a database connection or only one network connection? Isn't that almost as bad as assuming that *THE ONLY* user presses *THE ONLY* key on *THE ONLY* keyboard with *THE ONLY* finger?
Gordon L. Burditt
Oh you're gonna hate yourself in the morning... ;-)
The classic database connection singleton is to a connection pool not a single connection.
-david-
Quite right, foolish of me to go out on a limb and get so darn and
unnecessarily specific.
The singleton pattern is (as described in GoF) "Ensure a class has
only one instance, and provide a global point of access to it".
Why you would or would not want such a thing I'll leave up to you.
-Larry Truett
On Sat, 14 Jan 2006 03:07:54 GMT, Oli Filth <ca***@olifilth .co.uk>
wrote: FluffyCat said the following on 14/01/2006 00:20: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
Unfortunatel y, this is not a good example of a singleton system.
You have a class (BookSingleton) with a public constructor, so there's nothing to stop someone creating multiple BookSingleton objects with multiple "new BookSingleton() " calls.
To solve this problem, you need to make the constructor private, and the borrowBook() method static.
P.S. Another thing to make your code more readable would be to syntax highlight it...
Good call on the private constructor and static borrowBook() method,
that really tightens the class up.
Thanks!
On Sat, 14 Jan 2006 04:36:34 +0000 (UTC), Tim Van Wassenhove
<ti***@users.so urceforge.net> wrote: On 2006-01-14, Oli Filth <ca***@olifilth .co.uk> wrote: FluffyCat said the following on 14/01/2006 00:20: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
Unfortunately, this is not a good example of a singleton system.
You have a class (BookSingleton) with a public constructor, so there's nothing to stop someone creating multiple BookSingleton objects with multiple "new BookSingleton() " calls.
To solve this problem, you need to make the constructor private, and the borrowBook() method static.
And make sure to throw an error when clone is called.. Because a singleton shouldn't be cloned :)
That's an excellent point thanks!
I will add a note about that.
FluffyCat said the following on 15/01/2006 04:03: On Sat, 14 Jan 2006 03:07:54 GMT, Oli Filth <ca***@olifilth .co.uk> wrote:
FluffyCat said the following on 14/01/2006 00:20: To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern.
http://www.fluffycat.com/PHP-Design-Patterns/Singleton/ Unfortunately, this is not a good example of a singleton system.
You have a class (BookSingleton) with a public constructor, so there's nothing to stop someone creating multiple BookSingleton objects with multiple "new BookSingleton() " calls.
To solve this problem, you need to make the constructor private, and the borrowBook() method static.
Good call on the private constructor and static borrowBook() method, that really tightens the class up.
Another point to make is that the returnBook() method is currently
pointless. Not only should it be static, but even if it were static, it
would serve no purpose.
Imagine the following code:
$book = BookSingleton:: borrowBook();
BookSingleton:: returnBook();
// umm, $book still refers to the BookSingleton instance...
To utilise a "returning" feature correctly, you would actually need to
hook onto the object destructor, rather than calling a method
explicitly. However, this is also impossible, as you maintain a static
reference to the object, so it will never get destructed.
Therefore, any attempt at "returning" the singleton object is futile.
--
Oli This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alicia Roberts |
last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?
Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?
If someone knows about the...
|
by: Neil Zanella |
last post by:
Hello,
I would be very interested in knowing how the following C++ multi-instance
singleton (AKA Borg) design pattern based code snippet can be neatly coded
in Python. While there may be somewhat unusual places where multi-instance
singleton is more useful than plain singleton, it seems to me that the
former leads to less coding, so unless I can somehow package the
singleton pattern in a superclass (so I don't have to code it...
|
by: Richard A. DeVenezia |
last post by:
foo() generates elements with event handlers that invoke foo function
properties.
Is this an abhorrent or misthought pattern ?
It allows just the one occurence of identifier /foo/ to be changed to
/whatever/ when need arises and everything should still work.
function foo () {
var callee = arguments.callee
|
by: Alicia Roberts |
last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?
Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?
If someone knows about the...
|
by: Daniel Billingsley |
last post by:
Let's say I'm writing a business app and I want there to be only one
instance of the Customer object for each particular customer (representing a
database record) being edited.
Would it be possible to extend the Singleton pattern to handle this?
Assuming that my Customer class follows the Singleton pattern (particularly
Skeet's 4th version) I'm thinking if I add
private static SomeCollectionType customers;
| |
by: Sharon |
last post by:
I wish to build a framework for our developers that will include a singleton
pattern.
But it can not be a base class because it has a private constructor and
therefore can be inherit.
I thought maybe a Template can be use for that, but C# does not support
Templates (will be C# generics in mid 2005).
Does anyone have a solution on how the singleton pattern can be written, in
C#, as a framework/ infrastructure class, so users can use this...
|
by: Robert W. |
last post by:
At the beginning of my C# days (about 6 months ago) I learned about the
Singleton pattern and implemented for Reference data, such as the kind that
appears in an Options dialog box. My Singleton code looks like this:
public sealed class Reference
{
private static readonly Reference instance = new Reference();
// Make the default constructor private, so that nothing can directly
create it.
|
by: Kevin Newman |
last post by:
I have been playing around with a couple of ways to add inheritance to a
JavaScript singleton pattern. As far as I'm aware, using an anonymous
constructor to create a singleton does not allow any kind of inheritance:
singletonObj = new function() {
this.prop = true;
}
Here are two ways to create a singleton with inheritance:
|
by: dischdennis |
last post by:
Hello List,
I would like to make a singleton class in python 2.4.3, I found this
pattern in the web:
class Singleton:
__single = None
def __init__( self ):
if Singleton.__single:
raise Singleton.__single
|
by: weaknessforcats |
last post by:
Design Pattern: The Singleton
Overview
Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it.
The global point of access is the object's Instance() method.
Individual users need to be prevented from creating their own instances of the Singleton.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |