I am learning Prototype design pattern and I am little bit confused about main idea of this pattern and when to use it. Can you please help me to make clear some points for me?
1) If I am get right from this discussion, the main idea of Prototype pattern is saving cost of creating new object (NOTE this not mean memory allocating). Sometimes to create your object you need to request data from somewhere (for example DB request) or some big calculations and it might be time consuming so rather than to create new object it is more efficient to clone it. So the main idea of Prototype pattern is NOT saving efforts on memory allocation but on creating your object as it may be data driven or result of a calculation. Please correct me if I am wrong.
2) Is this code good example of prototype design pattern c++ implementation?
// Prototype
class Prototype
{
public:
virtual ~Prototype() { }
virtual Prototype* clone() const = 0;
};
// Concrete prototype
class ConcretePrototype : public Prototype
{
private:
int m_x;
public:
ConcretePrototype(int x) : m_x(x) { }
ConcretePrototype(const ConcretePrototype& p) : m_x(p.m_x) { }
virtual Prototype* clone() const
{
return new ConcretePrototype(*this);
}
void setX(int x) { m_x = x; }
int getX() const { return m_x; }
void printX() const { std::cout << "Value :" << m_x << std::endl; }
};
// Client code
void prototype_test()
{
Prototype* prototype = new ConcretePrototype(1000);
for (int i = 1; i < 10; i++) {
ConcretePrototype* tempotype =
dynamic_cast<ConcretePrototype*>(prototype->clone());
tempotype->setX(tempotype->getX() * i);
tempotype->printX();
delete tempotype;
}
delete prototype;
}
3) On a wikipedia usage of prototype pattern described as this
- avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.
in this sentence I do not understand the context of this part (e.g., using the 'new' keyword) How you can create an object without new?
4) There is a article in SourceMaking (Prototype in C++: Before and after) and code snippet
class Stooge
{
public:
virtual void slap_stick() = 0;
virtual Stooge* clone() = 0;
};
class Larry : public Stooge
{
public:
void slap_stick()
{
cout << "Larry: poke eyes\n";
}
Stooge* clone() { return new Larry; }
};
class Moe : public Stooge
{
public:
void slap_stick()
{
cout << "Moe: slap head\n";
}
Stooge* clone() { return new Moe; }
};
class Curly : public Stooge
{
public:
void slap_stick()
{
cout << "Curly: suffer abuse\n";
}
Stooge* clone() { return new Curly; }
};
class Factory
{
public:
static Stooge* make_stooge( int choice );
private:
static Stooge* s_prototypes[4];
};
Stooge* Factory::s_prototypes[] = {0, new Larry, new Moe, new Curly};
Stooge* Factory::make_stooge( int choice )
{
return s_prototypes[choice]->clone();
}
int main()
{
vector roles;
int choice;
while (true)
{
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back(Factory::make_stooge( choice ) );
}
for (int i=0; i < roles.size(); ++i)
roles[i]->slap_stick();
for (int i=0; i < roles.size(); ++i)
delete roles[i];
}
Based on my understanding of Prototype pattern this code snippet is not much related to prototype pattern and more about decoupling client code from concrete storage class by introducing Factory class and encapsulation creation of concrete classes. Please correct me if I am wrong and help me to understand how this example introduce prototype pattern.
Thanks in advance for your time and effort.
Aucun commentaire:
Enregistrer un commentaire