DP_Factory, C++, Memory Leak?
Hi guys,
After I tried factory pattern, I have a question that who is responsible for delete memory?
Just for C++. Any idea?
The following sample is from net.
#include <iostream>#include <string>using namespace std;// Abstract base classclass Mobile { public: virtual string Camera() = 0; virtual string KeyBoard() = 0; void PrintSpecs() { cout << Camera() << endl; cout << KeyBoard() << endl; }};// Concrete classesclass LowEndMobile : public Mobile { public: string Camera() { return "2 MegaPixel"; } string KeyBoard() { return "ITU-T"; }};// Concrete classesclass HighEndMobile : public Mobile { public: string Camera() { return "5 MegaPixel"; } string KeyBoard() { return "Qwerty"; }};// Abstract Factory returning a mobileclass MobileFactory { public: Mobile* GetMobile(string type);};Mobile* MobileFactory::GetMobile(string type) { if ( type == "Low-End" ) return new LowEndMobile(); if ( type == "High-End" ) return new HighEndMobile(); return NULL;}void main(){ MobileFactory* myFactory = new MobileFactory(); Mobile* myMobile1 = myFactory->GetMobile("Low-End"); myMobile1->PrintSpecs(); Mobile* myMobile2 = myFactory->GetMobile("High-End"); myMobile2->PrintSpecs();}OUTPUT:2 MegaPixelITU-T5 MegaPixelQwerty