Design Patterns in 1 minute: Prototype / Clone #8
Classification: Creational
Prototype (also known as Clone) is the design pattern that allows cloning — 🥸 — an object. In other words, it enables creating a new object in memory based on the information of an existing object.
The Problem
Imagine you are building an application that manages tasks on a board (similar to Jira), and one of your system's features is allowing users to duplicate one or more tasks.
Duplicating a task means not only copying its content but also copying the content of all subtasks, attachments, links, etc.
How can we solve this problem?
The Solution
Although the problem may seem complex, the solution is quite simple:
The idea is to create an interface called Prototype with a clone method (you can name the interface and its method as you prefer).
With the interface created, simply implement it in the classes that need to be cloned and define the copying logic for each object.