Onye ọ bụla nwere ahụmịhe na mmemme sọftụwia na-ekpe ikpe sọftụwia nke ndị ọzọ dere, na-eji usoro ikpe dabere na ụzọ ọrụ ha.
N'ime oge ọrụ m, amaara m ọtụtụ ndị mmepe, ahụkwala m ọtụtụ puku koodu na mgbe m kwesịrị inyocha nka onye nrụpụta, m na-eleba anya n'ihe abụọ:
N'ụzọ dị mma, enwere ụfọdụ isi ma ọ bụ ụkpụrụ ndị na-eme ka ọ dị mfe ịdị mma na nzuzo.
acronym SOLID na-anọchi anya:
S: ụkpụrụ nke otu ọrụ
O: ụkpụrụ mechiri emechi
L: Liskov nnọchi ụkpụrụ
I: Prinkpụrụ nke interface nkewa
D: Prinkpụrụ nke ntụgharị nke ịdabere
Ka anyị bido site n’ịbanye n’ime ụkpụrụ nke mbụ SOLID, ya bụ
Klas (ma ọ bụ modul) kwesịrị inwe naanị otu ihe kpatara ọ ga-eji gbanwee, ka ọ gbanwee.
Ebumnuche ahụ n'onwe ya dị mfe, mana iji nweta ịdị mfe a ụzọ ntinye nwere ike ịdị mgbagwoju anya. Otu klas kwesịrị inwe naanị otu ihe mere ọ ga-eji gbanwee.
Mana gịnị mere?
Gịnị mere o ji dị mkpa inwe naanị otu ihe mere ị ga-eji gbanwee?
Dịka ọmụmaatụ, ọ bụrụ na e nwere ihe abụọ dị iche iche maka ịgbanwe, ọ ga-eche na otu abụọ dị iche iche nwere ike ịrụ ọrụ na otu koodu maka ihe abụọ dị iche iche. Onye ọ bụla ga-etinye usoro nke aka ha, nke gbasara asụsụ a chịkọtara (dịka C ++, C # ma ọ bụ Java), nwere ike ibute modulu ndị na-ekwekọghị na ndị ọzọ ma ọ bụ akụkụ ndị ọzọ nke ngwa ahụ.
Ihe atụ ọzọ, ọ bụrụ n ’ị na-eji asụsụ a sụgharịrị asụgharị, ị nwere ike ịnwale otu klaasị ma ọ bụ modulu ahụ maka ebumnuche dị iche. Nke a pụtara ọtụtụ ọrụ, oge na mbọ maka njikwa mma.
Chọpụta naanị otu klaasị ma ọ bụ modulu kwesịrị ịnwe siri ike karịa naanị ịlele ndepụta nyocha iji gbaa ule.
Mana ka anyi gbalịa iche site n'echiche dị obere, ya bụ, ka anyị nwalee inyocha onye ọrụ klaasị anyị ma ọ bụ modulu anyị, ya bụ onye ga-eji ya. Akụkụ bụ isi nke anyị kwesịrị iburu n'uche mgbe niile, bụ eziokwu na ndị ọrụ nke ngwa ma ọ bụ sistemụ anyị mepụtara bụ ndị otu modulu ga-arụ ga-abụ ndị na-arịọ ka emezigharị ya. Ndị ọrụ ahụ ga-ajụ ka ha gbanwee klaasị ma ọ bụ modul ahụ.
Ihe omuma atu nke modulu na ojiji ha:
Ya mere ọ bụrụ na ihe mbụ ị ga-eme bụ ịchọ ndị na-eme ihe nkiri ma ọ bụ onye na-eme ihe nkiri nwere ọrụ nke onye na-eme mkparịta ụka na modulu ahụ, ijikọ ndị mmadụ n'otu n'otu na ọrụ niile nwere ike isi ike. Na obere ụlọ ọrụ, otu onye nwere ike ịrụ ọtụtụ ọrụ ebe nnukwu ụlọ ọrụ nwere ike ịnwe ọtụtụ mmadụ nwere otu ọrụ.
O yiri ihe ezi uche dị na ya ịchọpụta ọrụ, karịa ndị mmadụ ma ọ bụ ndị ọrụ.
Ya mere:
Ka anyị lee ụfọdụ ihe atụ
Ka e were ya na anyị nwere klaasị Akwụkwọ na - ejikọpụta echiche akwụkwọ na etu esi arụ ọrụ.
akwukwo klas {
ọrụ getTitle () {
laghachi “Akwụkwọ Dị Ukwuu”;
}
ọrụ getAuthor () {
laghachi “Alessandro Baricco”;
}
ọrụ nextpage () {
// peeji na-esote
}
Ọrụ Biputere Akwukwo ()
ikwughachi "ọdịnaya nke ibe dị ugbu a";
}
}
Nke a bụ klas dị oke mma. Anyị nwere akwụkwọ, klaasị nwere ike inye anyị aha, ha nwere ike inye anyị onye edemede, ha nwekwara ike ịga n’ihu. N'ikpeazụ, ọ nwekwara ike ibipụta ibe dị ugbu a na ihuenyo ahụ.
Otú ọ dị, enwere obere nsogbu.
Iche echiche banyere ndị na-eme ihe nkiri na njikwa nke ihe Book, onye ha nwere ike ịbụ?
Anyị nwere ike iche echiche dị mfe banyere ndị omee abụọ dị iche iche ebe a: Njikwa akwukwo (dika onye akwukwo) bu Usoro ntinye data (dị ka otu anyị chọrọ iji nyefee onye ọrụ ọdịnaya: na ihuenyo, interface njirimara eserese, interface naanị onye ọrụ, ikekwe bipụta).
Ya mere, anyị nwere ndị omekome abụọ dị iche iche na-emekọ ihe na klaasị.
Na nkenke, klas a na-eme ka mix dị n'etiti:
nke a nwere ike ịbụ nsogbu n'ihi na ọ na-emebi otu iwu iwu (SRP).
Kedụ ka anyị nwere ike isi gbanwee, kedu ka anyị ga - esi meziwanye koodu a iji kwanyere ụkpụrụ nke ibu ọrụ aka?
Lelee koodu ndị a:
akwukwo klas {
ọrụ getTitle () {
laghachi "Oceano Mare";
}
ọrụ getAuthor () {
laghachi “Alessandro Baricco”;
}
ọrụ ihu peeji () {
// peeji na-esote
}
ọrụ getCurrentPage () {
ikwughachi "ọdịnaya nke ibe dị ugbu a";
}
}
onye nbipute interface {
ọrụ Mbipụta ($ peeji);
}
klaasị StampaLibro na-eweta ngwa nbipute {
ọrụ arụ ọrụ ($ peeji) {
ikwughachi $ peeji;
}
}
klas HtmlPrinter mejuputa Printer {
ọrụ arụ ọrụ ($ peeji) {
nkuzi mgbamejije ' '. $ peeji. '' ';
}
}
Ihe omuma a di nfe na egosi uzo esi ewepu ihe ngosi site na uche ahia, na irube aka na SRP o n’enye otutu ihe ngbanwe nke oru ayi.
Ka anyị leba anya n’ihe atụ ọzọ:
Ihe omuma atu dika nke di n'elu bu mgbe ihe nwere ike ichekwa ma weghachite onwe ya site na ngosi.
akwukwo klas {
ọrụ getTitle () {
laghachi "Oceano Mare";
}
ọrụ getAuthor () {
laghachi “Alessandro Baricco”;
}
ọrụ ihu peeji () {
// peeji na-esote
}
ọrụ getCurrentPage () {
laghachi "ọdịnaya nke ibe dị ugbu a";
}
ọrụ nchekwa () {
$ filename = '/ akwụkwọ /'. $ this-> nwetaTitolo (). '-'. $ this-> nwetaAuthor ();
file_put_contents ($ filename, serialize ($ nke a));
}
}
Dị ka ọ dị na mbụ, anyị nwekwara ike ịchọpụta ndị na-eme ihe nkiri dị iche iche Njikwa akwukwo (dika onye akwukwo) bu nnọgidesi ike. Mgbe obula anyi choro igbanwe uzo anyi si aga na peeji ozo, anyi kwesiri igbanari klaasi a. Anyị nwere ike inwe ọtụtụ ihe kpatara mgbanwe.
akwukwo klas {
ọrụ getTitle () {
laghachi "Oceano Mare";
}
ọrụ getAuthor () {
laghachi “Alessandro Baricco”;
}
ọrụ ihu peeji () {
// peeji na-esote
}
ọrụ getCurrentPage () {
laghachi "ọdịnaya nke ibe dị ugbu a";
}
}
klaasị SimpleFilePersistence {
ọrụ nchekwa (Akwụkwọ $ akwụkwọ) {
$ filename = '/ akwụkwọ /'. $ book-> getTitle (). '-'. $ book-> getAuthor ();
file_put_contents ($ filename, serialize ($ akwụkwọ));
}
}
Vingkwaga ọrụ ịnọgidesi ike na klaasị ọzọ ga-ekewapụ ibu ọrụ n'ụzọ doro anya ma anyị ga-enwere onwe anyị ịgbanwee usoro ịnọgidesi ike na-enweghị emetụta klaasị Akwụkwọ anyị. Iji maa atụ, itinye n'ọrụ klaasị DatabasePersistence ga-abụ obere ihe, mgbagha azụmahịa anyị arụnyere n'ọrụ akwụkwọ agaghị agbanwe.
Gaa n'ihu site n'ịgụ ụkpụrụ nke abụọ Mepee / Emechie ->
Ercole Palmeri
A rụrụ ọrụ ophthalmoplasty site na iji ihe nlele azụmahịa Apple Vision Pro na Catania Polyclinic…
Ịzụlite ọmarịcha nka ụgbọ ala site na ịcha agba na-akwado ụmụaka maka nka dị mgbagwoju anya dị ka ide ihe. Ka agba…
Ngalaba ndị agha mmiri bụ ezigbo ike akụ na ụba ụwa, nke gorola n'ahịa ahịa ijeri 150 ...
Mọnde gara aga, Financial Times kwupụtara nkwekọrịta ya na OpenAI. FT na-enye ikike maka akwụkwọ akụkọ ụwa…