Suche…


Einführung

Das Prototypmuster ist ein kreatives Muster, das neue Objekte durch Klonen eines vorhandenen Prototypobjekts erstellt. Das Prototypmuster beschleunigt die Instantiierung von Klassen, wenn Objekte schneller kopiert werden.

Bemerkungen

Das Prototypmuster ist ein kreatives Designmuster. Sie wird verwendet, wenn der zu erstellende Objekttyp von einer prototypischen Instanz bestimmt wird, die zur Erzeugung neuer Objekte "geklont" wird .

Dieses Muster wird verwendet, wenn eine Klasse einen "polymorphen (Kopier-) Konstruktor" benötigt .

Prototypmuster (C ++)

class IPrototype  {
public:
    virtual ~IPrototype() = default;

    auto Clone() const { return std::unique_ptr<IPrototype>{DoClone()}; }
    auto Create() const { return std::unique_ptr<IPrototype>{DoCreate()}; }

private:
    virtual IPrototype* DoClone() const = 0;
    virtual IPrototype* DoCreate() const = 0;
};

class A : public IPrototype {
public:
    auto Clone() const { return std::unique_ptr<A>{DoClone()}; }
    auto Create() const { return std::unique_ptr<A>{DoCreate()}; }
private:
    // Use covariant return type :)
    A* DoClone() const override { return new A(*this); }
    A* DoCreate() const override { return new A; }
};

class B : public IPrototype {
public:
    auto Clone() const { return std::unique_ptr<B>{DoClone()}; }
    auto Create() const { return std::unique_ptr<B>{DoCreate()}; }
private:
    // Use covariant return type :)
    B* DoClone() const override { return new B(*this); }
    B* DoCreate() const override { return new B; }
};


class ChildA : public A {
public:
    auto Clone() const { return std::unique_ptr<ChildA>{DoClone()}; }
    auto Create() const { return std::unique_ptr<ChildA>{DoCreate()}; }

private:
    // Use covariant return type :)
    ChildA* DoClone() const override { return new ChildA(*this); }
    ChildA* DoCreate() const override { return new ChildA; }
};

Dadurch kann die abgeleitete Klasse aus einem Basisklassenzeiger erstellt werden:

ChildA childA;
A& a = childA;
IPrototype& prototype = a;

// Each of the following will create a copy of `ChildA`:
std::unique_ptr<ChildA> clone1 = childA.Clone();
std::unique_ptr<A> clone2 = a.Clone();
std::unique_ptr<IPrototype> clone3 = prototype.Clone();

// Each of the following will create a new default instance `ChildA`:
std::unique_ptr<ChildA> instance1 = childA.Create();
std::unique_ptr<A> instance2 = a.Create();
std::unique_ptr<IPrototype> instance3 = prototype.Create();

Prototypmuster (C #)

Das Prototypmuster kann mithilfe der ICloneable- Schnittstelle in .NET implementiert werden.

class Spoon {
}
class DessertSpoon : Spoon, ICloneable {
  ...
  public object Clone() {
    return this.MemberwiseClone();
  }
}
class SoupSpoon : Spoon, ICloneable {
  ...
  public object Clone() {
    return this.MemberwiseClone();
  }
}

Mustermuster (JavaScript)

In den klassischen Sprachen wie Java, C # oder C ++ erstellen wir zunächst eine Klasse und können dann neue Objekte aus der Klasse erstellen oder die Klasse erweitern.

In JavaScript erstellen wir zuerst ein Objekt, dann können wir das Objekt erweitern oder daraus neue Objekte erstellen. Ich denke, JavaScript demonstriert den tatsächlichen Prototyp als die klassische Sprache.

Beispiel:

var myApp = myApp || {};

myApp.Customer = function (){
    this.create = function () {
        return "customer added";
    }
};

myApp.Customer.prototype = {
    read: function (id) {
        return "this is the customer with id = " + id;
    },
    update: function () {
        return "customer updated";
    },
    remove: function () {
        return "customer removed";
    }
};

Hier erstellen wir ein Objekt mit dem Namen Customer . Ohne das Erstellen eines neuen Objekts haben wir das vorhandene Customer object mit dem Schlüsselwort prototype erweitert . Diese Technik wird als Prototypmuster bezeichnet .



Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow