When we have to use static instance?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi folks,

Can u give me the real time scenario where we have to use satic instance only?


By
B.Balaji
 
That depends on the business problems you are trying to create a solution
for.

Assuming that you are talking about static methods in the .NET base class
library there are several examples. For example the String.Format() method,
you don't want to instantiate a new object each time you want to format a
string.

Gabriel Lozano-Morán
 
Hi,

Static objects are generally disregarded nowadays, it is recommended to use
the Singleton design pattern instead.
 
Hi ,

Thanks . But i need the rael time scenario where we can use singleton design
patterns?
 
Normally you would need to create a static instance when the secondary (
the primary ofcourse is the busines logic) need is\

1. Keeping an instance which holds the state of the application, while all
other objects around this one in the application can live and die at will
( eg. the Request/ Application objects in ASP.Net which are static
throughout the application.)
2. To remove the overhead of creating and collecting objects which are
more or less expected to be stateless ( consider the case of a remoting
server/webservice providing math functions, totally stateless, and hence the
service should mask a singleton/static instance that provides the services ,
thus avoiding the creation and collection of the objects created, one each
for each call.)
HTH,
r.
http://codebetter.com/blogs/ranjan.sakalley/
 
Static methods and the Singleton pattern address different needs.

The Singleton pattern

1) Adds a level of indirection. This allows the creation of more than
one
instance of the class at a later date without breaking client code.

2) Encapsulates data and methods into a separate namespace, the
singleton
class. 

3) Allows sub-classing. 

4) Provides access control to the single instance. 

The .NET framework is full of static methods. Just check out the methods
in
the Math class.

If you do not need the added level of indirection provided by the
Singleton
Pattern, it may be simpler to use static methods and fields. You can see
the
similarities between using the Singleton Pattern and using static
methods and
fields by comparing the IL generated by similar projects. 

Regards,
Jeff
Can u give me the real time scenario where we have to use satic instance
only?
 
Back
Top