खोज…


परिचय

प्रोटोटाइप पैटर्न एक रचनात्मक पैटर्न है जो मौजूदा प्रोटोटाइप ऑब्जेक्ट को क्लोन करके नई ऑब्जेक्ट बनाता है। प्रोटोटाइप पैटर्न कक्षाओं की तात्कालिकता को गति देता है जब वस्तुओं को कॉपी करना तेज होता है।

टिप्पणियों

प्रोटोटाइप पैटर्न एक रचनात्मक डिजाइन पैटर्न है। इसका उपयोग तब किया जाता है जब बनाने के लिए वस्तुओं का प्रकार एक प्रोटोटाइप उदाहरण द्वारा निर्धारित किया जाता है, जो नई वस्तुओं का उत्पादन करने के लिए "क्लोन" होता है।

इस पैटर्न का उपयोग तब किया जाता है जब किसी वर्ग को "पॉलीमॉर्फिक (कॉपी) कंस्ट्रक्टर" की आवश्यकता होती है

प्रोटोटाइप पैटर्न (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; }
};

बेस क्लास पॉइंटर से व्युत्पन्न वर्ग के निर्माण की अनुमति देता है:

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

प्रोटोटाइप पैटर्न (C #)

प्रोटोटाइप पैटर्न को .NET में ICloneable इंटरफ़ेस का उपयोग करके लागू किया जा सकता है।

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

प्रोटोटाइप पैटर्न (जावास्क्रिप्ट)

जावा, सी # या सी ++ जैसी शास्त्रीय भाषाओं में हम एक वर्ग बनाकर शुरू करते हैं और फिर हम कक्षा से नई वस्तुएं बना सकते हैं या हम कक्षा का विस्तार कर सकते हैं।

जावास्क्रिप्ट में पहले हम एक ऑब्जेक्ट बनाते हैं, फिर हम ऑब्जेक्ट को बढ़ा सकते हैं या उससे नई ऑब्जेक्ट बना सकते हैं। इसलिए मुझे लगता है, जावास्क्रिप्ट शास्त्रीय भाषा की तुलना में वास्तविक प्रोटोटाइप प्रदर्शित करता है।

उदाहरण :

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

यहां, हम Customer नाम की एक वस्तु बनाते हैं, और फिर नई वस्तु बनाए बिना हमने प्रोटोटाइप कीवर्ड का उपयोग करके मौजूदा Customer object बढ़ाया है। इस तकनीक को प्रोटोटाइप पैटर्न के रूप में जाना जाता है।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow