
Originally Posted by
Tanus
I find myself using monostate when there are several methods that logically relate to each other, but don't share any class properties, and a singleton when I have an object that stores properties, but only ever want one instance of it floating around. They both achieve that global access, but are subtley different in how it happens. The important thing about them as opposed to using global $variables; all over the place is that you can control how those variables are accessed and manipulated.
For use or properties, you can use either and they work the same way. I can't see any difference in using these two below. In both situations, the properties you access will always be from one instance. Do you have an example where one would make a difference over the other? If it is where you have a group of methods without properties, why not just make a regular class and instantiate it?
Regards,
Eli
PHP Code:
class Singleton
{
private string var1 = "var1";;
private string var2 = "var2";
private Singleton singleton
public static Singleton GetInstance()
{
return singleton;
}
public string GetVar1()
{
return var1;
}
public string GetVar2()
{
return var2;
}
}
Singleton s = Singleton.GetInstance();
print s.GetVar1();
PHP Code:
class MonoState
{
private static string var1 = "var1";;
private static string var2 = "var2";
public static string GetVar1()
{
return var1;
}
public static string GetVar2()
{
return var2;
}
}
print MonoState.GetVar1();
Bookmarks