Jump to content

The Singleton Pattern

- - - - -

  • Please log in to reply
2 replies to this topic

#1
alrazy1

alrazy1

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
as i understand , singleton pattern insure that only one instance of an object reside in the server memory , and all visitors of the website will use the same instance , so its purpose is to save memory in the server, for example we have 10 visitors in our website only the first visitor will create an object and the rest 9 visitors will use the same object , please help me understand what is singleton pattern is for exactly ?!!

or its purpose to give only 1 instance of an object for each visitor ? or for each session ?

please somebody explain how this work , i mean the logic not the code itself ?

thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It can have a few purposes.
1) global variable. This would be an instance where you don't want to duplicate data throughout the system.
2) if you have a text editor, you could use the singleton pattern to only store one instance of a character's font representation, so you don't have thousands of copies of the data for "e", for instance.

In general, it's used when you have many "instances" of large identical data, and want to save memory by having them all point to a single real representation of it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
I often see it used used for database connections. Typically, you only need to connect to a single database and there no sense in doing it for every page load.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users