SOLID ndi mfundo ziti 5 za mapulogalamu okonda zinthu

Zithunzi zolimba zolimba
maphunziro
1

SOLID ndichidule, potengera mfundo zisanu za kapangidwe kazinthu (OOD kapena OOP). Awa ndi malangizo omwe opanga mapulogalamu angagwiritse ntchito popanga mapulogalamu osavuta kuwongolera, kuwongolera ndikuwonjezera. Kumvetsetsa mfundo izi kukupangitsani kukhala wopanga mapulogalamu abwino ndikuthandizani kupewa mavuto oyang'anira mapulogalamu. Kodi zimatanthauza chiyani kukhala wolemba mapulogalamu wabwino?

Aliyense amene akudziwa zambiri pulogalamu yamapulogalamu amaweruza mapulogalamu olembedwa ndi ena, pogwiritsa ntchito ziweruzo potengera ntchito yawo.

Pa nthawi yonse yomwe ndimagwira ntchito yaukadaulo, ndadziwa akatswiri ambiri, ndipo ndaona mizere yambirimbiri ndipo ndikafunika kuyesa luso la wopanga zinthu ndimangoyang'ana zinthu ziwiri:

  • Kuphweka powerenga code;
  • Momwe zilili kuti code yawo igwire ntchito ndikusintha pakapita nthawi.

Mwamwayi, pali zina zoyambira kapena mfundo zomwe zimapangitsa kuti zikhale zosavuta kuzilemba.

mawu akuti SOLID amayimira:
S: mfundo yokhala ndiudindo umodzi
O: mfundo yotseguka
L: Liskov m'malo mfundo
I: Mfundo yosiyanitsa mawonekedwe
D: Mfundo Yosinthira Kutengera

Tiyeni tiyambe ndikupenda mfundo yoyamba YOLIMBIKIRA, yomwe ndi

Mfundo Yodalirika Yokha

Kalasi (kapena gawo) liyenera kukhala ndi chifukwa chimodzi chokha chosinthira, kusintha.

Lingaliro lokha ndilosavuta, koma kuti akwaniritse kuphweka kumeneku njira yakukhazikitsa ikhoza kukhala yovuta kwambiri. Gulu liyenera kukhala ndi chifukwa chimodzi chokha chosinthira.

Koma chifukwa chiyani? 

Chifukwa chiyani kuli kofunika kukhala ndi chifukwa chimodzi chokha chosinthira?

Mwachitsanzo, ngati pali zifukwa ziwiri zosinthira, ndizotheka kuti magulu awiri osiyana atha kugwira ntchito yofanana pazifukwa ziwiri zosiyana. Aliyense akuyenera kukhazikitsa yankho lake, lomwe likakhala kuti chilankhulo (monga C ++, C # kapena Java), chitha kubweretsa ma module omwe sakugwirizana ndi magulu ena kapena magawo ena a pulogalamuyi.

Chitsanzo china, ngati mukugwiritsa ntchito chilankhulo, mutha kuyesanso kalasi limodzi kapena gawo lomwelo pazifukwa zosiyanasiyana. Izi zikutanthauza ntchito yambiri, nthawi ndi khama pakuwongolera zabwino.

Kuzindikira gawo limodzi lomwe kalasi kapena gawo liyenera kukhala ndizovuta kwambiri kuposa kungoyang'ana mndandanda kuti muyese mayeso. 

Koma tiyeni tiyesere kulingalira kuchokera pamalingaliro ocheperako, ndiye kuti, tiyeni tiyesere kusanthula wogwiritsa ntchito kalasi yathu kapena gawo lathu, ndiye amene adzagwiritse ntchito. Chofunikira kwambiri chomwe tiyenera kukumbukira nthawi zonse, ndichakuti ogwiritsa ntchito kapena makina omwe timapanga omwe amatumizidwa ndi gawo linalake ndi omwe adzapemphe zosintha. Omwe atumizidwa adzafunsa kuti asinthe kalasi kapena module. 

Zitsanzo zina za ma module ndi momwe amagwiritsira ntchito:

  • Gawo lokonzanso: wogwiritsa ntchito amapangidwa ndi oyang'anira nkhokwe ndi opanga mapulogalamu.
  • Kufotokozera gawo: wogwiritsa ntchito amapangidwa ndi ogwira ntchito kumaofesi, owerengera ndalama komanso zopanga.
  • Gawo lowerengera ndalama pamayeso oyang'anira malipiro: ogwiritsa ntchito atha kuphatikizira maloya, oyang'anira ndi owerengera ndalama.
  • Kusaka kwamalemba gawo la kasamalidwe ka laibulale: ogwiritsa ntchito amatha kuyimilidwa ndi woyang'anira laibulale kapena ndi alendo komanso makasitomala a laibulale yomwe.

Chifukwa chake ngati gawo loyamba ndikufufuza ochita zisudzo kapena wochita seweroli yemwe ali ndi gawo lokambirana ndi gawoli, kuphatikizira anthu omwe ali ndi maudindo onse kumakhala kovuta. Pakampani yaying'ono, munthu m'modzi amatha kuchita maudindo angapo pakampani yayikulu pakhoza kukhala anthu angapo omwe ali ndi gawo limodzi. 

Zikuwoneka zomveka kuzindikira maudindo, osati anthu kapena ogwiritsa ntchito.

Chifukwa chake:

  • wogwiritsa ntchito pulogalamuyo amafotokoza zifukwa zosinthira;
  • udindo ndi banja la ntchito zomwe zimakwaniritsa zosowa za wochita sewero, ndiye kuti, wogwiritsa ntchito makinawo;
  • ochita sewerowo, wogwiritsa ntchitoyo amakhala gwero la kusintha kwa mabanja azinthu zomwe ziyenera kukwaniritsa zosowa za wogwiritsa ntchito;
  • kusinthika kwa zosowa za ogwiritsa ntchito, kumatsogolera kusinthika kwa magwiridwe antchito;

Mfundo zolimba

Tiyeni tiwone viyerezgero vinyake

Tiyerekeze kuti tili ndi Kalasi ya Buku yomwe imaphatikiza lingaliro la buku ndi magwiridwe ake.

kalasi Book {

    ntchito GetTitle () {

        bwererani "Bukhu Lalikulu";

    }

    gwiritsani ntchito GetAuthor () {

        bwererani "Alessandro Baricco";

    }

    gwiritsani tsamba lotsatira () {

        // tsamba lotsatira

    }

    gwiritsani ntchito printCurrentPage () {

        lembani "zomwe zili patsamba lino";

    }

}

Ili ndi gulu labwinobwino. Tili ndi buku, ndipo ophunzirawo atha kutipatsa mutuwo, atha kutipatsa wolemba, ndipo amatha kupitiliza. Pomaliza, imathanso kusindikiza tsamba lomwe lilipoli pazenera. 

Komabe, pali vuto laling'ono. 

Kuganizira za omwe akuchita nawo kasamalidwe ka Book, angakhale ndani? 

Titha kuganiza mosavuta za osewera awiri apa: Kuwongolera Mabuku (monga owerenga mabuku) ndi Makina operekera deta (monga momwe tikufuna kuperekera zinthu kwa wogwiritsa ntchito: pazenera, mawonekedwe owonetsera, mawonekedwe owerenga okha, mwina kusindikiza). 

Tili ndi ochita sewero awiri osiyana omwe amalumikizana ndi gulu.

Mwachidule gulu ili limapanga kuphatikiza pakati:

  • Malingaliro abizinesi ndi 
  • nkhani 

ili likhoza kukhala vuto chifukwa limaphwanya mfundo imodzi yokhazikika (SRP). 

Kodi tingasinthe bwanji, tingawongolere bwanji malamulowa kuti tilemekeze mfundo yoti aliyense akhale ndi udindo?

Onani nambala yotsatirayi:

kalasi Book {

    ntchito GetTitle () {

        bwererani "Oceano Mare";

    }

    gwiritsani ntchito GetAuthor () {

        bwererani "Alessandro Baricco";

    }

    tsamba lotembenuzira ntchito () {

        // tsamba lotsatira

    }

    gwiritsani ntchito GetCurrentPage () {

        lembani "zomwe zili patsamba lino";

    }

}

mawonekedwe Printer {

    ntchito printPage ($ tsamba);

}

Class StampaLibro imagwiritsa ntchito Printer {

    gwiritsani ntchito masamba osindikiza ($ tsamba) {

        pezani tsamba la $;

    }

}

 

HtmlPrinter imagwiritsa ntchito Printer {

    gwiritsani ntchito masamba osindikiza ($ tsamba) {

        echo ' '. $ tsamba. ' ';

    }

}

Chitsanzo chophwekachi chikuwonetsa momwe tingalekanitsire zowonetserako ndi malingaliro abizinesi, ndipo kutsatira SRP kumapereka mwayi waukulu pakusintha kwa ntchito yathu.

Tiyeni tione chitsanzo china:

Chitsanzo chofanana ndi chomwe chili pamwambapa ndi pamene chinthu chimatha kudzipulumutsa ndikudziwonetsera pachokha.

kalasi Book {

    ntchito GetTitle () {

        bwererani "Oceano Mare";

    }

    gwiritsani ntchito GetAuthor () {

        bwererani "Alessandro Baricco";

    }

    tsamba lotembenuzira ntchito () {

        // tsamba lotsatira

    }

    gwiritsani ntchito GetCurrentPage () {

        bweretsani "zomwe zili patsamba lino";

    }

    ntchito sungani () {

        $ filename = '/ zolemba /'. $ izi-> getTitolo (). '-'. ' $ izi-> getAuthor ();

        file_put_contents ($ filename, serialize ($ iyi));

    }

}

Monga kale, panonso titha kuzindikira osewera osiyanasiyana ngati Kuwongolera Mabuku (monga owerenga mabuku) ndi Khama. Nthawi zonse tikamafuna kusintha momwe timayendera kuchokera patsamba kupita kutsamba, tifunika kusintha kalasi iyi. Titha kukhala ndi zifukwa zingapo zosinthira.

kalasi Book {

    ntchito GetTitle () {

        bwererani "Oceano Mare";

    }

    gwiritsani ntchito GetAuthor () {

        bwererani "Alessandro Baricco";

    }

    tsamba lotembenuzira ntchito () {

        // tsamba lotsatira

    }

    gwiritsani ntchito GetCurrentPage () {

        bweretsani "zomwe zili patsamba lino";

    }

}

kalasi SimpleFilePersistence {

    ntchito sungani (Buku $ book) {

        $ filename = '/ zolemba /'. $ buku-> getTitle (). '-'. ' $ buku-> getAuthor ();

        fayilo_put_contents ($ filename, serialize ($ book));

    }

}

Kusunthira ntchito yolimbikira kupita mgulu lina kudzasiyanitsa bwino maudindo ndipo tidzakhala omasuka kusinthanitsa njira zolimbikira popanda kukhudza gulu lathu la Buku. Mwachitsanzo, kugwiritsa ntchito DatabasePersistence kalasi sikungakhale kopepuka, ndipo malingaliro athu amabizinesi omangidwa mozungulira ntchito zamabuku sangasinthe.

Pitirizani powerenga mfundo yachiwiri Open / Closed ->

Ndemanga ya 1

Siyani ndemanga

Imelo yanu siidzafalitsidwa. Masamba ovomerezeka amadziwika *

mfundo ya liskov
maphunziro
Mfundo ya Liskov Substitution, mfundo yachitatu ya SOLID

Maphunziro a ana sayenera kusintha kapena kusintha matanthauzidwe amtundu wamakolo. Lingaliro la mfundoyi lidayambitsidwa ndi a Barbara Liskov m'mawu apadera pamsonkhano wa 1987 ndipo kenako adasindikiza mu nkhani limodzi ndi Jannette Wing mu 1994. Tanthauzo lawo loyambirira…

zochitika zotsatsa pa google
maphunziro
Momwe mungagwiritsire ntchito Google Trends kutsatsa kwakanthawi

Limodzi mwamavuto akulu omwe amakampani amakumana nawo mu 2020 ndikumvetsetsa komwe magawo azinthu amasinthira bizinesi yawo: makamaka magawo ambiri amakampani adakumana ndi zovuta zomwe zidapangitsa kuti makampani asalowemo, makamaka ngati wosewera watsopano. Magawo ochepa opanga ...

njira zanzeru zamabizinesi
njira
Njira zopangira Business Intelligence

Kupanga njira yopambana ya Business Intelligence yanu kumayamba ndikuwona bwino zolinga. Tikuwona pansipa mfundo zofunikira. Kuwona momwe zinthu ziliri Kungakhale kulakwitsa kwakukulu kunyalanyaza izi. Kuwunika momwe zinthu ziliri pano kumatanthauza kusanthula njira, kapangidwe kake ...