Refactoring to remove Singleton

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.