Design mode entry (1) ------ Singleton

zhaozj2021-02-11  177

Design mode entry (1) ------------ Singleton

The author's words:

Everyone says that C is difficult, it is difficult to be in his object-oriented OO part. And OO's design, it is the difficult point of OO

The design pattern describes a certain type of fixed problem in the object-oriented domain, and the fixed solution is compared. So

You can reuse this solution once again without having to repeat labor. But classic books << Design Pattern

>> Comparison, think about myself, and spend a lot of time to try to figure out the meaning of the book. Now master the master of design patterns

Many, but they are very busy. I think, I can spend some rest time to do some C fans to do some

Meaningful things. My level is not high, there must be some mistakes in the text, welcome to correct the masters.

I am a C mystery. I often go www.cuj.com, in my article, you will also find Jim Hyslop and Herb Su

Tter's shadow.

I hope to introduce the pattern to everyone in a way that is easy to understand. If you think this article is quite right to your appetite, or for you

There is a little help, Hahaha, ITS My Pleasure. Also come to me! Everyone encourages me to write

Li! (^ - ^)

-------------------------------------------------- -------------------------

"Hey, what are you doing?", This guy has turned his pen to turn over the morning and turned around.

I am upset. "You can't be quiet!", I said.

"I have a problem that I can't solve." Grant said, "I designed a clock class, he needs a single clock to

Other other timers in the program are coordinated. I don't want to generate the second clock! "," So how do you do it?

"What about it is interested in it." I want to use global variables, but I have no way to let me have the damn clock class only.

One example. "And, he went to say" this program is too big, not me alone. If you just alone

I can remember that I only instantiate the clock class once, but I still have a lot of partner, this is not a good way, I

Want to guarantee it from design! "

Think about it just after reading the design mode last night, a single piece of method for ensuring only the only instance of a class (Singleton)

), I can't help but be happy, hahaha, I can sell it! ".

"You can define constructor into non-public, you can guarantee that you will not generate instances." I said with a smile.

"My god, no, non-public constructor, I have never thought ........." So how do I generate me?

The only clock? If it is a non-public constructor, then I don't seem to instite the clock class. .........

"

"You can define a STATIC type for class operations, instantiated in this function!"

"I still don't quite understand!", Grant blinked.

I wrote the following example:

Class Singleton

{

PUBLIC:

Static Singleton * instance ();

protected:

Singleton ();

Private:

Static Singleton * _INSTANCE;

}

Singleton * Singleton :: _ instance = 0;

Singleton * singleleton :: instance ()

{

IF (_Instance == 0)

{

_INSTANCE = New Singleton;

}

Return_INSTANCE;

}

"Here, the instance function is a Static type function, he can operate the class, and _instance is a Static type

Variables, it is only related to class. Instance uses inert initialization, it creates unique Singletion instance until the first time is called. And constructor you can define as a private type, which is defined as a protecty, which can make you

Reute it in the class function, so that if someone wants to use Singleton to instantiate the object, it will get a compilation error.

He can only ask the only object through the instance function, understand? "

"You are a genius, this idea is great, if I am MM, I will marry you!"

"Marry me, still count! Go to your program."

Looking at the back of the GRANT, I spit out the tongue, ha, genius, exactly the genius, only I know!

-------------------------------------------------- ------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Let's take a look at the topic, design patterns, remember, this is just getting started! It is very shallow here. If you want to mention

High words, or watch << Design Pattern >>. (Hahaha, if you look carefully, you will find the author plagiarism.

many! )

Author: hyqryq

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

New Post(0)