Design mode C # description - single case and multi-use mode

zhaozj2021-02-16  47

Design mode C # description - single case and multi-use mode

As the creation mode of the object, single case mode ensures that a certain class has only one instance, and it is instantiated and supplied to the entire system. This class is called a single case.

Single example mode has the following features:

Single examples can only have an instance.

Single cases must create their own unique instances yourself.

Single examples must provide this example to all other objects.

A typical single-size implementation is shown below: where the structure of the sub-privacy represents the subclass cannot be inherited.

Public Class Singleton

{

Private static singleleton m_instance = NULL;

Private singleton ()

{

}

Public Static Singleton getInstance ()

{

IF (m_instance == null)

{

m_instance = new singleleton ();

}

Return M_INSTANCE;

}

}

The so-called multi-model mode is actually a natural promotion of single example mode. Single-case class can only have an instance, but the single case class can also be extended to allow limited instance, this mode is multi-example mode. As the creation mode of the object, many models have the following features:

Multiple examples can have multiple instances.

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

Many classifications are classified into multiple categories of super-limiting and unpleassed.

A multimodal class with the upper limit has put the upper limit of the instance as part of the logic, and built into the interior of multiple categories. 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 the multiple categories does not require a limit, and the number of instances do not have the upper limit is called uncharacted multi-example mode. Since there is no upper limit, the number of instances is unlimited, so although this multi-model is the promotion of single case mode, this multi-class class is not necessarily returned to a single case. All instances of aggregation management are generally adopted.

转载请注明原文地址:https://www.9cbs.com/read-25079.html

New Post(0)