Answer the question
In order to leave comments, you need to log in
Prototype, did I understand correctly that it should look something like this?
I started studying patterns, Prototype seemed to me the most versatile, its implementation is different everywhere, there is no consensus on how this pattern should look like. (I am studying 4 people from the book) Who fumbles, tell me pliz how the visual implementation of this miracle should look like.
I got it like this:
#include <iostream>
class Prototype {
public:
virtual Prototype* clone() = 0;
virtual ~Prototype() {};
};
class ConcretePrototype1 : public Prototype {
int data;
public:
ConcretePrototype1(int other_data): data(other_data) {}
ConcretePrototype1(const ConcretePrototype1 &prototype): data(prototype.data) {}
Prototype* clone() {return new ConcretePrototype1(* this);}
};
class ConcretePrototype2 : public Prototype {
int data;
public:
ConcretePrototype2(int other_data): data(other_data) {}
ConcretePrototype2(const ConcretePrototype2 &prototype): data(prototype.data) {}
Prototype* clone() {return new ConcretePrototype2(* this);}
};
Answer the question
In order to leave comments, you need to log in
There is no consensus on any pattern - as it should be, because they belong to OOP, and not to specific languages \u200b\u200bthat have their own ideoms.
In general - yes, the pattern is "built-in" into the language, because you can define a copy constructor, but you don't actually need the clone() member function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question