Cov txheej txheem:

Yuav Ua Li Cas Ua Singleton Tus Qauv Hauv C ++: 9 Kauj Ruam
Yuav Ua Li Cas Ua Singleton Tus Qauv Hauv C ++: 9 Kauj Ruam

Video: Yuav Ua Li Cas Ua Singleton Tus Qauv Hauv C ++: 9 Kauj Ruam

Video: Yuav Ua Li Cas Ua Singleton Tus Qauv Hauv C ++: 9 Kauj Ruam
Video: P nyoom Y tsis yog ua si 4/24/2020 2024, Kaum ib hlis
Anonim
Yuav Ua Li Cas Ua Singleton Tus Qauv Hauv C ++
Yuav Ua Li Cas Ua Singleton Tus Qauv Hauv C ++

Taw qhia:

Lub hom phiaj ntawm phau ntawv qhia no yog qhia rau tus neeg siv yuav ua li cas txhawm rau siv tus qauv tsim ib leeg hauv lawv li C ++ program. Thaum ua li ntawd, cov lus qhia no tseem yuav piav qhia rau tus nyeem ntawv vim li cas cov ntsiab lus ntawm singleton yog lawv li cas thiab ua li cas cov cai tau ua. Paub txog qhov no, yuav pab yav tom ntej nrog kev debugging koj cov nkauj yav tom ntej. Singleton tus qauv tsim yog dab tsi? Singleton tus qauv tsim yog tus qauv tsim uas tus coder tsim cov chav kawm uas tsuas tuaj yeem ua tam sim ntawd ib zaug, cov chav kawm ua haujlwm rau pej xeem tuaj yeem nkag mus tau rau txhua qhov chaw, yog tias koj muaj #suav nrog cov ntaub ntawv header hauv lwm cov ntaub ntawv cuam tshuam nrog txoj haujlwm.

Singleton tus qauv tsim yog yuav tsum paub tus qauv tsim rau txhua tus phiaj xwm phiaj xwm phiaj xwm, software programmers, thiab game programmers. Singleton tus qauv tsim kuj tseem yog ib qho yooj yim coding tsim qauv tawm muaj. Kev kawm nws tuaj yeem pab koj kawm lwm yam, nyuaj dua, tsim qauv yav tom ntej. Nws kuj tseem tuaj yeem pab koj ua kom yooj yim rau koj li program cov cai hauv txoj kev uas koj tsis xav tias ua tau.

Thaum qhov nyuaj ntawm singleton tus qauv tsim qauv tau yooj yim thaum piv rau lwm cov qauv tsim, cov lus qhia no muaj qhov nyuaj nruab nrab. Qhov no txhais tau tias kom ua cov lus qhia no, peb pom zoo kom koj paub qhov yooj yim thiab ua ntej syntax xav tau ntawm C ++. Koj kuj tseem yuav tsum paub C ++ kev coj tus cwj pwm coj zoo (piv txwv li khaws cov chav kawm sib txawv ntiag tug, ib chav kawm ib daim ntawv header thiab lwm yam). Koj tseem yuav tsum paub yuav ua li cas kom tsis nco thiab cov neeg tsim khoom thiab cov neeg ua haujlwm puas tsuaj hauv C ++.

Phau ntawv qhia no yuav siv sij hawm nruab nrab ntawm 10-15 feeb.

Cov Khoom Yuav Tsum Tau:

-Cov khoos phis tawj (tuaj yeem yog PC lossis Mac) muaj peev xwm khiav Visual Studios (ib qho version)

-Cov phiaj xwm yooj yim, tsim hauv Visual Studios, uas koj tuaj yeem sim koj li singleton nrog

Nco tseg: Tus qauv tsim ib leeg tuaj yeem ua tiav ntawm lwm qhov C ++ txhawb nqa IDE lossis coding interface, tab sis rau cov lus qhia teeb tsa no, peb yuav siv Visual Studios Enterprise Edition.

Kauj Ruam 1: Tsim Koj Chav Kawm, Nrog Header File thiab CPP File

Tsim Koj Chav Kawm, Nrog Header File thiab CPP File
Tsim Koj Chav Kawm, Nrog Header File thiab CPP File
Tsim Koj Chav Kawm, Nrog Header File thiab CPP File
Tsim Koj Chav Kawm, Nrog Header File thiab CPP File

Txhawm rau tsim ob daim ntawv no thiab chav kawm ib zaug, qhib koj qhov project / program hauv Visual Studios, hla mus rau qhov kev daws teeb meem tshawb nrhiav, nyem rau sab xis, thiab lub npov yuav tsum tshwm nyob ze koj tus cursor nas, nrhiav qhov xaiv "Ntxiv", yuj rau hla nws, thiab lwm lub thawv yuav tsum tshwm rau sab xis. Hauv lub npov no, koj xav pom qhov kev xaiv "Yam Khoom Tshiab..", nyem rau nws thiab lub qhov rai, zoo ib yam li daim duab 1.1 duab hauv qab no, yuav tsum tshwm sim. Hauv lub qhov rai no koj xav xaiv "C ++ Class" thiab tom qab ntawd nyem "Ntxiv". Qhov no yuav qhib lwm lub qhov rai uas zoo li daim duab 1.2 duab. Hauv lub qhov rai no, koj ntaus lub npe ntawm koj chav kawm hauv "Class Name" teb thiab Visual Studios yuav cia li tso npe cov ntaub ntawv tseeb tom qab lub npe kawm. Rau qhov kev qhia no lub hom phiaj, peb yuav mus rau npe peb chav kawm "EngineDebugSingleton", tab sis nws tuaj yeem yog ib lub npe raws tus ntawv. Tam sim no koj tuaj yeem tsoo "OK" thiab mus rau kauj ruam 2.

Nco tseg: Cov tshuaj tshawb nrhiav thiab qhov twg khaws cov ntaub ntawv ntawm koj lub computer cais. Tsiv lossis tsim ib yam dab tsi hauv qhov kev tshawb fawb daws yuav tsis txav lossis teeb tsa cov ntaub ntawv hauv koj li OS file explorer. Ib txoj hauv kev nyab xeeb los teeb tsa koj cov ntaub ntawv ntawm cov ntawv tshawb fawb sab yuav raug tshem tawm, tab sis tsis tshem tawm cov ntaub ntawv tshwj xeeb los ntawm cov lus tshawb nrhiav, txav tib cov ntaub ntawv hauv cov ntawv tshawb fawb mus rau qhov chaw xav tau thiab tom qab ntawd rov qab mus rau kev daws teeb meem, nyem rau, nrhiav qhov kev xaiv "Ntxiv", tom qab ntawv nrhiav "Cov Khoom Uas Muaj" thiab nrhiav cov ntaub ntawv koj tau tsiv. Nco ntsoov tias koj txav ob lub header thiab cpp file.

Kauj Ruam 2: Teem Tus Kws Tsim Kho rau Ntiag Tug

Teem Tus Kws Tsim Kho rau Ntiag Tug
Teem Tus Kws Tsim Kho rau Ntiag Tug

Nrog koj cov ntaub ntawv CPP tshiab uas tau tsim thiab cov ntaub ntawv header, yog tias nws tsis qhib cia li thaum koj tsim nws, mus rau qhov kev tshawb fawb tshuaj thiab nyem thiab qhib "EngineDebugSingleton.h". Tom qab ntawd koj yuav tau txais tos nrog "EngineDebugSingleton ()", chav kawm tsis yog tus tsim thiab "~ EngineDebugSingleton ()" chav kawm puas tsuaj. Rau cov kauj ruam no, peb yuav xav teeb tsa tus tsim khoom rau tus kheej, qhov no txhais tau tias txoj haujlwm no tsuas yog muaj rau chav kawm thiab tsis muaj dab tsi ntxiv. Nrog rau qhov no, koj yuav tsis tuaj yeem hloov pauv lossis faib chav kawm rau kev nco sab nraud hauv chav kawm, tsuas yog hauv chav kawm 'header file thiab chav kawm' lwm txoj haujlwm. Muaj tus tsim tus kheej yog tus yuam sij rau tus qauv tsim thiab yuav ua li cas singletons ua haujlwm. Peb yuav pom hauv cov kauj ruam yav tom ntej yuav ua li cas singleton tau nrawm thiab nkag mus tau.

Tam sim no chav kawm yuav tsum zoo li qhov no tom qab hloov tus tsim mus rau tus kheej (Saib cov duab sib txuam)

Kauj Ruam 3: Teem Tus Destructor rau Tus Kheej

Teem Tus Destructor rau Tus Kheej
Teem Tus Destructor rau Tus Kheej

Zoo li peb tau ua nrog tus tsim hauv

kauj ruam 2, rau cov kauj ruam no, tam sim no peb yuav teeb tsa tus neeg rhuav tshem mus rau tus kheej. Zoo li nrog tus tsim, tsis muaj dab tsi, tsuas yog chav kawm nws tus kheej, tuaj yeem tshem tawm ib qho kev hloov pauv ntawm chav kawm los ntawm kev nco.

Cov chav kawm yuav tsum zoo li no tom qab ua tiav cov theem no. (Saib daim duab cuam tshuam)

Kauj Ruam 4: Tsim Kom Zoo Li Qub Pointer Variable hauv Singleton

Tsim kom muaj Qhov Hloov Pauv Hloov Pauv Zoo nyob hauv Singleton
Tsim kom muaj Qhov Hloov Pauv Hloov Pauv Zoo nyob hauv Singleton

Hauv cov kauj ruam no, peb yuav tsim a

zoo li tus pointer sib txawv ntawm hom "EngineDebugSingleton*". Qhov no yuav yog qhov sib txawv uas yuav siv los faib peb singleton rau lub cim xeeb thiab yuav taw tes rau nws rau tag nrho lub sijhawm peb singleton tau faib rau kev nco.

Nov yog qhov peb cov ntaub ntawv header yuav tsum zoo li tom qab tsim qhov sib txawv no

Kauj Ruam 5: Tsim Tus Txheej Txheem Ua Haujlwm

Tsim kom muaj nuj nqi
Tsim kom muaj nuj nqi

Tam sim no peb xav ua piv txwv

muaj nuj nqi Txoj haujlwm yuav tsum yog qhov ua haujlwm zoo li qub thiab yuav xav rov xa mus rau peb chav kawm ("EngineDebugSingleton &"). Peb hu peb lub luag haujlwm Instance (). Hauv txoj haujlwm nws tus kheej, peb yuav xav sim thawj zaug yog ptrInstance == nullptr (tuaj yeem luv rau! PtrInstance), yog tias nws yog nullptr, qhov no txhais tau hais tias singleton tsis tau muab faib thiab hauv qhov txwv ntawm yog cov lus, peb yuav xav faib los ntawm kev ua ptrInstance = tshiab EngineDebugSingleton (). Nov yog qhov uas koj tau faib lub singleton rau kev nco. Tom qab tawm ntawm qhov txwv ntawm cov lus yog, peb yuav rov qab dab tsi ptrInstance tau taw qhia rau, uas yog qhia los ntawm cov syntax "*ptrInstance". Peb yuav siv txoj haujlwm no hnyav thaum ua peb cov haujlwm pej xeem zoo li qub, yog li peb tuaj yeem tshawb xyuas seb puas tau tsim cov nkauj hu nkauj thiab muab faib rau lub cim xeeb. Hauv qhov tseeb, txoj haujlwm no ua rau koj kom koj tsuas muaj ib qho kev faib ntawm chav kawm thiab tsis muaj ntxiv lawm.

Nov yog qhov peb cov chav kawm yuav tsum zoo li tam sim no tom qab tsim qhov Instance () muaj nuj nqi. Raws li koj tuaj yeem pom, txhua yam uas peb tau ua tiav tau nyob hauv ntu ntiag tug ntawm chav kawm, qhov no yuav hloov pauv me ntsis hauv ob peb kauj ruam tom ntej.

Kauj Ruam 6: Tsim Kev Ua Haujlwm Zoo Li Qub

Tsim kom muaj nuj nqis rau pej xeem
Tsim kom muaj nuj nqis rau pej xeem
Tsim kom muaj nuj nqis rau pej xeem
Tsim kom muaj nuj nqis rau pej xeem
Tsim kom muaj nuj nqis rau pej xeem
Tsim kom muaj nuj nqis rau pej xeem

Tom qab koj tau ua txoj haujlwm los ntawm

kauj ruam 5, koj tuaj yeem pib ua haujlwm zoo li qub rau pej xeem. Txhua qhov haujlwm pej xeem yuav tsum muaj txoj haujlwm ntiag tug kom ua nrog nws, lub npe ntawm txoj haujlwm no tsis tuaj yeem zoo ib yam. Vim li cas thiaj ua kom muaj nuj nqi Zoo li qub? Peb tab tom ua cov haujlwm pej xeem zoo li qub kom lawv tuaj yeem nkag mus yam tsis muaj qhov khoom tiag. Yog li tsis txhob ua qee yam xws li "EngineDebugSingleObj-> SomeFunction ()", peb ua "EngineDebugSingleton:: Qee Qhov Ua Haujlwm ()". Qhov no ua rau nws muaj peev xwm nkag mus rau ib leeg hauv ib qhov twg hauv cov cai, yog tias koj tau #suav nrog cov ntaub ntawv header hauv cov ntawv tshwj xeeb uas koj tab tom ua haujlwm nrog. Nrog rau qhov no, koj tseem tuaj yeem tsim cov nkauj ib leeg los ntawm ib qho ntawm nws cov haujlwm pej xeem.

Rau peb lub hom phiaj hauv cov kauj ruam no peb tsim ob qhov kev ua haujlwm pej xeem zoo li qub, "ntxiv ()" thiab "rho tawm ()". Hauv ntu ntiag tug, peb ob txoj haujlwm ntxiv, "PrivAdd ()" thiab "PrivSubtract ()". Peb kuj ntxiv qhov sib txawv hu ua "NumberOfThings". Lub ntsiab lus rau cov haujlwm no yuav nkag mus rau hauv peb chav kawm ntawv cov ntaub ntawv CPP. Txhawm rau ua kom yooj yim ua rau lub luag haujlwm nkag mus rau hauv CPP cov ntawv, koj hais meej, nrog koj tus cursor, txoj haujlwm, uas yuav tsum muaj kab ntsuab nyob hauv qab nws, thiab ntaus "Left ALT + ENTER", nws yuav muab rau koj xaiv los tsim cov lus txhais hauv chav kawm cuam tshuam nrog CPP cov ntaub ntawv. Saib Duab 6.1 kom pom cov ntaub ntawv header yuav tsum zoo li cas thiab tom qab koj tau tsim tag nrho cov lus txhais ua haujlwm, koj li CPP yuav tsum zoo li Duab 6.2 tshwj tsis yog tias koj lub ntsiab lus txhais yuav tsis muaj tus lej hauv lawv.

Tam sim no koj yuav xav ntxiv cov lej ib yam li hauv Daim Duab 6.2 rau hauv koj cov lus txhais ua haujlwm. Raws li tau hais ua ntej, peb cov haujlwm pej xeem yuav tau siv ntawm Instance () muaj nuj nqi, uas yuav rov qab dab tsi ptrInstance tau taw tes rau. Qhov no tso cai rau peb nkag mus rau cov haujlwm ntiag tug ntawm peb chav kawm. Nrog rau ib tus singleton cov haujlwm pej xeem, koj tsuas yog yuav tsum tau hu qhov Instance muaj nuj nqi. Tsuas yog qhov tshwj xeeb rau qhov no yog peb Txoj Haujlwm Ua Haujlwm.

Nco tseg: Qhov tseeb ntawm pej xeem thiab ntiag tug kev ua haujlwm tau qhia hauv qib no tsis tsim nyog, koj tuaj yeem muaj lub luag haujlwm sib txawv lub npe thiab kev ua haujlwm hauv tus kheej txoj haujlwm, tab sis rau txhua hom kev ua haujlwm rau pej xeem, koj yuav tsum muaj txoj haujlwm ntiag tug kom nrog nws thiab kev ua haujlwm rau pej xeem yuav tsum siv tas li, hauv peb qhov xwm txheej, Kev Ua Haujlwm () muaj nuj nqi.

Kauj Ruam 7: Tsim Kev Txiav Txim Ua Haujlwm

Tsim kom muaj Kev Txiav Txim Siab
Tsim kom muaj Kev Txiav Txim Siab
Tsim Kev Txiav Txim Ua Haujlwm
Tsim Kev Txiav Txim Ua Haujlwm

Txij li peb tsuas tuaj yeem cais peb cov nkauj ib leeg los ntawm kev nco hauv peb chav kawm, peb yuav tsum tsim kom muaj kev ua haujlwm zoo li qub. Lub luag haujlwm no yuav hu rau rho tawm ntawm ptrInstance, uas hu rau chav ua kom puas thiab tom qab ntawd peb yuav xav teeb ptrInstance rov qab rau nullptr yog li nws tuaj yeem faib dua yog tias koj qhov program tsis xaus. Koj tseem yuav xav txiav koj li Singletons los ntxuav ib qho kev nco uas koj faib rau hauv Singleton tus kheej qhov sib txawv.

Kauj Ruam 8: Teeb PtrInstance rau Nullptr

Teeb PtrInstance rau Nullptr
Teeb PtrInstance rau Nullptr

Txhawm rau ua kom tiav koj li singleton, koj xav mus rau EngineDebugSingleton. CPP cov ntaub ntawv thiab nyob rau sab saum toj ntawm CPP cov ntaub ntawv, hauv peb qhov piv txwv, ntaus "EngineDebugSingleton* EngineDebugSingleton:: ptrInstance = nullptr."

Ua qhov no yuav pib teeb tsa ptrInstance rau nullptr, yog li thaum koj xub mus dhau qhov piv txwv ua haujlwm thawj zaug, peb chav kawm yuav raug tso cai rau faib rau kev nco. Tsis muaj nws, koj yuav feem ntau yuav tau txais qhov yuam kev vim tias koj yuav tau sim nkag mus rau lub cim xeeb uas tsis muaj dab tsi faib rau nws.

Kauj Ruam 9: Xeem thiab Xaus

Xeem thiab Xaus
Xeem thiab Xaus

Tam sim no peb yuav xav sim qhov peb singleton kom ntseeg tau tias nws ua haujlwm, qhov no yuav koom nrog peb hu rau pej xeem lub luag haujlwm zoo li tau piav qhia hauv kauj ruam 6 thiab peb pom zoo kom koj teeb tsa cov lus sib faib kom nqis los ntawm koj cov cai thiab pom tias singleton ua haujlwm li nws yuav tsum yog. Peb qhov pib yuav yog nyob hauv peb qhov project main.cpp thiab peb qhov main.cpp tam sim no zoo li cov duab hauv qab no.

Nrog koj zoo siab! Koj nyuam qhuav ua tiav koj thawj qhov kev siv ntawm Singleton Design Pattern. Nrog tus qauv tsim no, tam sim no koj tuaj yeem hloov kho koj cov cai hauv ntau txoj hauv kev. Piv txwv li, tam sim no koj tuaj yeem ua tus tswj hwm cov haujlwm uas ua haujlwm thoob plaws koj lub sijhawm ua haujlwm, uas tuaj yeem nkag mus tau los ntawm kev ua haujlwm zoo li qub nyob txhua qhov chaw uas koj tau suav nrog chav kawm.

Koj cov ntawv kawg header yuav tsum zoo li daim duab 7.1. Koj singleton qhov cuam tshuam nrog CPP cov ntaub ntawv yuav tsum zoo li Duab 6.2 nrog rau ntxiv rau, nyob rau sab saum toj ntawm cov ntawv, ntawm cov cai tau qhia hauv qib 8. Cov lus qhia no tau muab rau koj nrog cov qauv yooj yim ntawm Singleton Tsim Qauv.

Cov lus qhia daws teeb meem:

Tau txais qhov yuam kev cuam tshuam txog kev nco?

Nco ntsoov tias koj hais txog qib 7 thiab qib 8 kom paub tseeb tias koj tau teeb tsa ptrInstance rau nullptr.

Infinite loop tshwm sim?

Ua kom ntseeg tau rau cov haujlwm pej xeem, hauv lawv cov lus txhais, koj hu rau tus kheej, tsis yog tib txoj haujlwm pej xeem.

Cov khoom faib rau hauv singleton ua rau lub cim xeeb xau?

Nco ntsoov tias koj hu koj lub singleton qhov kev ua haujlwm thaum tsim nyog hauv koj li kev cai lij choj, thiab hauv kev rhuav tshem koj lub singleton, xyuas kom tseeb tias koj tsis faib cov khoom uas tau faib rau kev nco hauv thaj tsam ntawm singleton code.