Singleton Pattern


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Singleton Pattern

  1. #1
    Join Date
    Aug 2007
    Posts
    1

    Singleton Pattern

    Hi All,

    I was going through the Design Patterns. I read about the Singleton Pattern which says only one Instance will be created and further Request will return the same Object. Every thing is fine.

    My Question is If we implement the same thing in the Static Class what will happen.

    For Example: I have seen an example which will read values from the XML and store it in the Static List inside the Class and further request the same instacne is returned.

    Instead if we do the same thing with the Static Class and read the values from the XML and store it in the Static List and for subsiquent request we can send the same list.

    So what is the difference? I know the object is created once. Why we need a object when we can get the same List with Static Method.

    Thanks in advance

    Anandraj.A.

  2. #2
    Join Date
    Aug 2007
    Posts
    1
    1. Design Patterns are beyond particular languages, e.g. in Java or in .Net 1 there are no static classes.
    2. Sometimes you'll need an instance, e.g. implementation of specific interface.
    3. One of main intent of Design Patterns is to make software easier modifiable, extendable, etc. E.g. in your example perhaps there are no need to use "pure" Singleton and static class is the best for you. But using Singleton (i.e. returning object instance in your case) you can decide later return instance of other class - very easy and customizable change in behavior.
    4. Static class doesn't eliminate Singletin: static property in static class can be implementation of Singleton in fact.

  3. #3
    Join Date
    Jan 2007
    Posts
    145
    Singleton pattern is more generic in nature.
    Say if you have to create maximum ten instances for a class, now can you code it using static class concept?

  4. #4
    Join Date
    Sep 2007
    Posts
    2
    Hi,

    Lets understand the difference between static class and static method.
    1. Static method is part of the class which could be accessed without instance creation and as well used to achieve the singleton pattern.
    The benefit of the Singleton pattern is that it prevents you from filling up the global namespace. You can keep the variables and functions locked up inside a class and restrict access to it by implementing a single point of access, which prevents you from creating multiple instances of the class.


    2.Singleton class can be inherited,Where as static class cannot be inherited.

    3.By making the whole class as static you only avoid creating object of the class by allowing the user to call all the interfaces.Thus making the class as utility class where in you provide a set of utility functions to access without creating the instance.


    So the purpose of having both the features is totally different and can't be used as alternative for each other.

    Another notable difference is that all static member class cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, you really have to make it a singleton

    Hope that helps you,
    -Shilpa

Similar Threads

  1. Correct Design Pattern for Order Form
    By Need2CSharp in forum Architecture and Design
    Replies: 1
    Last Post: 07-28-2007, 08:50 AM
  2. Singleton Pattern doubt
    By LuxCoder in forum .NET
    Replies: 0
    Last Post: 07-27-2007, 11:31 AM
  3. Java Singleton Pattern Tutorial
    By hemanthjava in forum Java
    Replies: 2
    Last Post: 11-14-2006, 10:57 PM
  4. how to list resources in a package?
    By stoughto in forum Java
    Replies: 2
    Last Post: 06-11-2006, 09:56 AM
  5. problem with singleton as library
    By kronos in forum C++
    Replies: 3
    Last Post: 04-07-2005, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center