Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. singleton pattern example

singleton pattern example

Scheduled Pinned Locked Moved Design and Architecture
designregexarchitecturetutorial
23 Posts 9 Posters 17 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L led mike

    Urs Enzler wrote:

    but with managing functionality

    "managing" would be structural.

    Urs Enzler wrote:

    Depending on configuration and other things it will return me a singleton or a new instance.

    It is difficult to understand that with any certainty. Perhaps you are using both Factory and Flyweight[^] Patterns. Or perhaps you are referring to a higher level abstraction of both into a Composite or Facade. I can't tell.

    U Offline
    U Offline
    Urs Enzler
    wrote on last edited by
    #21

    Hmm, I'd say it is as you say a higher level of abstraction and containing several individual design patterns. I think I should write an article about that, that would make the discussion easier ;)

    -^-^-^-^-^- no risk no funk ................... please vote ------>

    1 Reply Last reply
    0
    • V Vinay Dornala

      hi everybody, Iam new to design pattern concept , I want to know in which scenario we are using singleton pattern in our appl. I need exact scenario because i had some examples and gone the theory in books i want applicable knowledge in application development. Thanks in advance for replay,

      E Offline
      E Offline
      ElSpinos
      wrote on last edited by
      #22

      Greetings Shakeela, The Singleton design pattern serves to describe an implementation that restricts instantiation of a class to one object, in other words no copies or additional references. I've created a Singleton instantiate for you here to experiment with and get an idea of my description above. the singleton design pattern also belongs to the Creational Patterns group which consist of the following additional design patterns: o Abstract factory pattern: centralize decision of what factory to instantiate o Factory method pattern: centralize creation of an object of a specific type choosing one of several implementations o Builder pattern: separate the construction of a complex object from its representation so that the same construction process can create different representations o Lazy initialization pattern: tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed o Object pool: avoid expensive acquisition and release of resources by recycling objects that are no longer in use o Prototype pattern: used when the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) is prohibitively expensive for a given application The singleton class you can experiment with: /// Any type with a public and parameterless constructor. public sealed class SingletonCreator where Type : class, new() { #region Private declarations /// /// The instance to retain after creation /// private static Type instance; #endregion #region Properties /// /// A lazy instance instantiator property. It will test for the existence of an object and return that reference otherwise it will create a new object. /// /// An singleton instance of type "Type". public static Type GetInstance() { lock (typeof(Type)) { if (instance == null) { instance = new Type(); } return instance; } } #endregion } Usage: // This will always create an instance it it doesn’t exist or return the // existin reference if it was previously called. MyClass myClass = SingletonCreator<MyClass>.GetInstance(); I hope this helps you in your quest to learn design patterns ;) Have an awesome weekend...

      Fernando Mendes Senior .NET Developer, Architect

      1 Reply Last reply
      0
      • V Vinay Dornala

        hi everybody, Iam new to design pattern concept , I want to know in which scenario we are using singleton pattern in our appl. I need exact scenario because i had some examples and gone the theory in books i want applicable knowledge in application development. Thanks in advance for replay,

        E Offline
        E Offline
        etkid84
        wrote on last edited by
        #23

        singletons are used when it only makes sense to have exactly one and only one of them... i am not sure the impact singletons have on multi-threaded apps either, you might want to do some research in that area as well. design patterns are an interesting concept... i always stick to the kiss principle.. and avoid over-engineering my code. sometimes you can get into trouble with singletons, because sometimes they are implemented with private a constructor and destructor, and some example implementations you find on the web have nearly undetectable flaws ... there are variations to this theme between java and c++, and i am not sure about python (or whether you even need to do it in python). be cautious and judicious in your decision making, kind regards,

        David

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups