, #, , java, ,
You can use singleletons with interfaces, like any other class. In C #, an interface is a contract, and objects that have an interface must satisfy all the requirements of that interface.
Singlets can be used with the interface
public interface ISiteInterface
{
};
class SiteStructure : ISiteInterface
{
}
class TestClass
{
public TestClass()
{
SiteStructure site = SiteStructure.Instance;
CustomMethod((ISiteInterface)site);
}
private void CustomMethod(ISiteInterface interfaceObject)
{
}
}
Here we can use singleton for any method that accepts an interface. We do not need to rewrite anything again and again. This is best practice for object-oriented programming. Below you can find more detailed examples on the type of interface in C #.
C # Singleton Pattern Versus Static Class
source
share