Dwarves
Memo
Type ESC to close search bar

Going Through use cases of the prototype design pattern and it place among the creational patterns

Problem statement

We want a copy of an object, but the config only initialized at runtime, its fields and methods were private, or its properties were manipulated through multiple processes, so it is difficult to recreate the object. For example:

Prototype design pattern

Request a Clone of the object(Prototype) without the need to look up its class and implementation.

The target object must turn into a ‘Prototype’ by having a Clone() function to mirror the object with sufficient properties for the user of the cloned object. For example:

//The following example is in Go. We are building an RPG with a Hero object, and a new Skill called 'Mimic' needs to create a copy of the Hero with the same level.
type Hero struct {       // level, experience, and killLogIDs are private to avoid editing the Hero object
 level         int64    
 experience    int64   
 killLogIDs    []int64 
}

type (h *Hero) KillConfirm(targetID int64) {
 h.killLogIDs = append(killLogIDs, targetID)
 h.experience += 10

 if h.experience >= 100 {
 h.level += 1
 h.experience -= 100
 }
}

// Creates a clone of the Hero without repeating the whole kill log 
type (h *Hero) Clone() Hero {
 return Hero {
 level: h.level,
 experience: h.experience,
 killLogIDs: h.killLogIDs,
 }
}

Case by case

When developing a system, the Prototype design pattern works in tandem with other creation patterns:

The most common use cases for Prototype design patterns are:

Reference