K
K
Konstantin Knatarev2019-08-06 21:13:39
C++ / C#
Konstantin Knatarev, 2019-08-06 21:13:39

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);}
};

5d49c2ffacfa6829497216.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Bomberow, 2019-08-07
@majstar_Zubr

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 question

Ask a Question

731 491 924 answers to any question