Last active
March 28, 2022 08:16
-
-
Save bunyamintamar/9b9c43ef3dffc01a650dda4ece315a08 to your computer and use it in GitHub Desktop.
Prototip Tasarım Kalıbı - Prototype Design Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Prototip Tasarım Kalıbı Örneği - Prototype Design Patern */ | |
| /* | |
| Var olan bir nesnenin birkaç özelliğini değiştirmek dışında kopyasına ihtiyacımız varsa | |
| bu yöntem tam da ihtiyacımız olan şey. | |
| Tamamen yenisini üretmek çok zaman alır. Kopyalayıp değiştirmek daha kısa sürer. | |
| (Bu kadar açıklama yeter. Hadi kodlayalım !) | |
| */ | |
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| /**************************************************************************/ | |
| class Belge // Soyut sınıf olmalı | |
| { | |
| protected: | |
| long id; | |
| string adi; | |
| string icerik; | |
| string Not; | |
| public: | |
| Belge() {} | |
| virtual Belge *clone() = 0; | |
| void setId(long id) { this->id = id; } | |
| void setAdi(string adi) { this->adi = adi; } | |
| void setIcerik(string icerik) { this->icerik = icerik; } | |
| void setNot(string Not) { this->Not = Not; } | |
| void yazdir() | |
| { | |
| string metin; | |
| metin.append("Belge\n{"); | |
| metin.append("\n\tid : ").append(to_string(id)); | |
| metin.append("\n\tadi : ").append(adi); | |
| metin.append("\n\ticerik : ").append(icerik); | |
| metin.append("\n\tNot : ").append(Not); | |
| metin.append("\n}\n"); | |
| cout << metin; | |
| } | |
| }; | |
| class KopyalanabilirBelge : public Belge | |
| { | |
| public: | |
| KopyalanabilirBelge() {} | |
| KopyalanabilirBelge(long id, string adi, string icerik, string Not) | |
| { | |
| this->id = id; | |
| this->adi = adi; | |
| this->icerik = icerik; | |
| this->Not = Not; | |
| } | |
| KopyalanabilirBelge *clone() | |
| { | |
| // Kopyalandığında özelliklerini yeni nesneye aktarıyor | |
| return new KopyalanabilirBelge(*this); | |
| } | |
| }; | |
| /**************************************************************************/ | |
| int main() | |
| { | |
| cout << "PROTOTİP TASARIM KALIBI ÖRNEĞİ" << endl << endl; | |
| Belge *belge1 = new KopyalanabilirBelge(1, "Belge1", "Belge1'in iceriği", "not yok"); | |
| belge1->yazdir(); | |
| cout << endl; | |
| Belge *belge2 = belge1->clone(); // Belge1'den kopyalanıp birkaç özelliği değiştirilecek | |
| belge2->setAdi("Belge2"); | |
| belge2->setNot("Belge1'in kopyasi olduğu için içeriği aynı"); | |
| belge2->yazdir(); | |
| cout << "Sadece adi ve not değişti" << endl << endl; | |
| Belge *belge3 = belge2->clone(); // Belge2'den kopyalanıp birkaç özelliği değiştirilecek | |
| belge3->setAdi("Belge3"); | |
| belge3->yazdir(); | |
| cout << "Sadece adi değişti. Belge2'nin bir kopyası aslında" << endl << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment