Last active
March 27, 2022 12:05
-
-
Save bunyamintamar/75bcac1640ffeef3d8debf9c63d92692 to your computer and use it in GitHub Desktop.
Fabrika Metodu Tasarım Kalıbı - Factory Method 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
| /* Fabrika Metodu Tasarım Kalıbı Örneği - Factory Method Design Pattern */ | |
| /* | |
| Nesneleri sadece yaratıp kullanmak ve yaratılış süreçlerini gizlemek için bu yöntem kullanılır. | |
| Bu nedenle yaralış süreçlerine hakim olan başka bir nesneye ihtiyaç duyulur. | |
| Bu nesne bizim yerimize ihtiyaç duyduğumuz nesneyi yaratır. | |
| Mesela; | |
| Nissan isimli bir sınıftan Qashgai isimli bir nesne üretileceğini varsayalım. | |
| Kırmızı renkli Skypack detayına sahip olmasını istiyoruz. | |
| Bunun için Nissan sınıfından kendimiz bir Qashgai üretmek için setter metodlarını kullanmalıyız. | |
| Fakat, Qashgai'nin yaratılma sürecini gizlemek istiyorsak bunu bir fabrikaya söyleriz ve bizim | |
| yerimize bunu halleder. | |
| Hadi kodlayalım ! | |
| */ | |
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| /**************************************************************************/ | |
| class Nissan | |
| { | |
| private: | |
| string color; | |
| string package; | |
| protected: | |
| Nissan() { this->color = "beyaz"; this->package = "Tekna"; } // Gizlenmek istenen metod | |
| void setColor(string color) { this->color = color; } // Gizlenmek istenen metod | |
| void setPackage(string package) { this->package = package; } // Gizlenmek istenen metod | |
| public: | |
| void showMyNissan() // Sadece buna erişebilirim | |
| { | |
| cout << "Aracınızın rengi: " << color << endl; | |
| cout << "Aracınızı paketi: " << package << endl << endl; | |
| } | |
| friend class NissanFactory; // Nissan'ın metodlarına erişebilmeli | |
| }; | |
| class NissanFactory | |
| { | |
| public: | |
| Nissan *createNissan() | |
| { | |
| return new Nissan; | |
| } | |
| Nissan *createNissan(string color, string package) | |
| { | |
| Nissan *n = new Nissan(); | |
| n->setColor(color); | |
| n->setPackage(package); | |
| return n; | |
| } | |
| }; | |
| /**************************************************************************/ | |
| int main() | |
| { | |
| NissanFactory *factory = new NissanFactory(); | |
| Nissan *yourNissan = (Nissan*)factory->createNissan(); | |
| Nissan *myNissan = (Nissan*)factory->createNissan("kırmızı", "Skypack"); | |
| cout << "your Nissan" << endl; | |
| yourNissan->showMyNissan(); | |
| cout << "my Nissan" << endl; | |
| myNissan->showMyNissan(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment