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
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.
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?
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,
By Need2CSharp in forum Architecture and Design
Last Post: 07-28-2007, 09:50 AM
By LuxCoder in forum .NET
Last Post: 07-27-2007, 12:31 PM
By hemanthjava in forum Java
Last Post: 11-14-2006, 11:57 PM
By stoughto in forum Java
Last Post: 06-11-2006, 10:56 AM
Last Post: 04-07-2005, 04:08 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center