Refactoring is not only a get mode, but also a remove mode. because we know that the mode will bring complexity to the system to a certain extent: (------- this problem does exist. especially if the system is to be maintained by a person who does not understand the mode in the future, the problem will be even greater! So be careful when using the mode!
Inline Singleton refactoring is to remove unsuitable Singleton.
We know that Singleton provides a global access point to ensure that the system has a unique instance of the class! From another perspective, it is just an unchangeable global variable! Are you afraid?-d
Therefore, the refactoring and pattern book reminds us that Singleton is unnecessary when they can be designed or re-designed to avoid using them.
In this case, do not hesitate to inline it! This will make your class relationship more obvious. UnnecessaryCodeThe unique instance (0) is implemented ).
Specific steps:
/// <Summary>
/// Summary description for class1.
/// </Summary>
Public class consolesingleton
{
Private Static connobj OBJ = new connobj ();
Private consolesingleton ()
{
}
Public static connobj getconnn ()
{
Return OBJ;
}
}
Public class connobj
{
}
Public class Adapter
{
Private connobj OBJ;
Public void dowork ()
{
Console. writeline (obj. tostring ());
}
}
Public class client
{
Public static void main ()
{
OBJ = lelesingletion. getconnn ();
Console. Readline ();
}
}
Consolesingleton is a singleton object. However, this object does not play much role. So remove this Singleton
Public class connobj
{
}
Public class Adapter
{
Private connobj OBJ;
Public adapter (connobj _ OBJ)
{
OBJ = _ OBJ;
}
Public void dowork ()
{
Console. writeline (obj. tostring ());
}
}
Public class client
{
Public static void main ()
{
New adapter (New connobj (). dowork ();
Console. Readline ();
}
}