Design Pattern: Prototype Pattern

Tram Ho

I. Introduction:


Prototype Pattern is a Design Pattern of the Creational group. It allows us to copy an object regardless of the object ‘s class . Prototype Pattern consists of the following main components:

  • Copyng Ptotocol : The protocol used to declare methods for copying objects .
  • Prototye : is a class adopting Copying Protocol to serve for duplicating its objects .

II. How it works:


To copy an object of the class , the Prototype Pattern will use an initialization function with the input params as an attribute of the object to be copied . The values ​​retrieved from the params passed into the constructor will be used to initialize a new object .

III. When is the prototype model used?


The prototype pattern is used when you want an object of the class to make a copy of itself.

IV. For example:


Declaring Copying Protocol

Implement Copying Ptotocol in the Prototype object.

Use

V. References:


Share the news now

Source : Viblo