Last active
March 28, 2022 14:55
-
-
Save bunyamintamar/47faac1835b99f9b94ee1b48749979d8 to your computer and use it in GitHub Desktop.
Soyut Fabrika Tasarım Kalıbı - Abstract Factory 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
| /* Soyut Fabrika Tasarım Kalıbı Örneği - Abstract Factory Design Pattern */ | |
| /* | |
| Farklı nesnelerin bazı ortak özelliklere sahip olduğunu ve | |
| bu nesneleri bir metoda argüman olarak girmek istediğinizi düşünün. | |
| Bunu nasıl yaparsınız? | |
| Mesela; | |
| Kedi, köpek ve kuş sınıflarından türemiş olan nesnelerimiz var. | |
| Bunları "hayvanlariBesle(..)" isimli bir metoda argüman olarak girmek istiyoruz. | |
| Bu metodun prototipi nasıl olmalıdır? | |
| void hayvanlariBesle(Kopek *kopek) <--- Sadece köpekleri besliyor | |
| void hayvanlariBesle(Kedi *kedi) <--- Sadece kedileri besliyor | |
| void hayvanlariBesle(Kus *kus) <--- Sadece kuşları besliyor | |
| Gördüğünüz gibi üç tane farklı hayvanlariBesle(..) isimli metodumuz var. | |
| Bu listenin daha da uzadığını hayal edin! | |
| Korkmayın! Soyut fabrika tasarım kalıbı ayağınıza geldi | |
| Hadi kodlayalım! | |
| */ | |
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| /**************************************************************************/ | |
| class Hayvan | |
| { | |
| public: | |
| virtual void besle() = 0; | |
| }; | |
| class Kopek : public Hayvan | |
| { | |
| public: | |
| Kopek() { cout << "Kopek" << endl; } | |
| void besle() { cout << "Kopeğe et veriliyor" << endl; } | |
| }; | |
| class Kedi : public Hayvan | |
| { | |
| public: | |
| Kedi() { cout << "Kedi" << endl; } | |
| void besle() { cout << "Kediye süt veriliyor" << endl; } | |
| }; | |
| class Kus : public Hayvan | |
| { | |
| public: | |
| Kus() { cout << "Kus" << endl; } | |
| void besle() { cout << "Kuşa yem veriliyor" << endl; } | |
| }; | |
| /**************************************************************************/ | |
| void hayvanlariBesle(Hayvan *hayvan) | |
| { | |
| hayvan->besle(); | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| Hayvan *hayvan1 = new Kopek(); | |
| Hayvan *hayvan2 = new Kedi(); | |
| Hayvan *hayvan3 = new Kus(); | |
| cout << endl; | |
| hayvanlariBesle(hayvan1); | |
| hayvanlariBesle(hayvan2); | |
| hayvanlariBesle(hayvan3); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment