Design Pattern C # Description--single case and multiple case pattern

Source: Internet
Author: User
Design design Pattern C # Description--single case and multiple case pattern

As an object's creation pattern, the singleton pattern ensures that a class has only one instance, and instantiates it and supplies the instance to the entire system. This class is called a single instance class.



The single example pattern has the following characteristics:

A singleton class can have only one instance.

A singleton class must create its own unique instance.

A singleton class must provide this instance to all other objects.

A typical implementation of a single instance class is as follows: The construction child private represents a subclass that cannot be inherited.



public class Singleton

{

private static Singleton m_instance = null;



Private Singleton ()

{

}

public static Singleton getinstance ()

{

if (m_instance==null)

{

M_instance=new Singleton ();

}

return m_instance;

}

}



The so-called multiple-case pattern, in fact, is the natural extension of a single case, a singleton class can only have one instance in general, but a singleton class can also be extended to allow a finite number of instances, this pattern is a multiple-case pattern. As an object creation pattern, several examples of patterns have the following characteristics:

Multiple instances of a class can have more than one instance.

Multiple classes must create, manage, and provide their own instances to the outside world.

Many cases are divided into upper-bound multiple cases and no upper-class.

A class with a higher limit has already taken the upper bound of the instance as part of the logic and built it into the interior of multiple classes. As follows:



public class Multiton

{

private static Multiton Instance1=null;

private static Multiton Instance2=null;



Private Multiton ()

{

}



public static Multiton getinstance (int whichone)

{

if (whichone==1)

{

if (instance1==null)

{

Instance1=new Multiton ();

}

return Instance1;

}

Else

{

if (instance2==null)

{

Instance2=new Multiton ();

}

return instance2;



}

}

}

The number of instances of multiple classes does not need to be capped, and the number of instances with no upper bounds is called no upper-case mode. Since there is no limit to the number of instances for multiple classes, this multiple-case pattern is a generalization of a singleton pattern, but it is not necessarily possible to return to a singleton class. Aggregate management is generally used for all instances.


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.