-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
tags: | ||
- software-design | ||
- software-architecture | ||
- graphical-notation | ||
title: "Going Through use cases of the prototype design pattern and it place among the creational patterns " | ||
date: 2024-05-15 | ||
description: "Prototype, one of the creational patterns, minimize efforts when recreating new from the exist object by cloning the 'prototype' of it." | ||
authors: | ||
- jim | ||
menu: memo | ||
type: memo | ||
hide_frontmatter: false | ||
pinned: false | ||
hide_title: false | ||
--- | ||
|
||
![](assets/prototype-design-pattern.pdf) | ||
|
||
|
||
### 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: | ||
- DB Access Object with credentials only provide in runtime. | ||
- A ledger object has transaction histories, which are protected through private fields, and exposes a Total() function to get the total balance. To replicate the whole ledger, we must recreate all its transaction histories. | ||
|
||
### 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: | ||
- Factories, abstract factories, and builders help create the original object. Prototype provides a clone of the object without the need to call the above patterns | ||
- Prototype behaves opposite to the singleton pattern. Singleton pattern focuses on having a single instance for operational flow. Prototype can Clone the object in the middle of its processing flow for other uses without changing the original object. | ||
|
||
The most common use cases for Prototype design patterns are: | ||
- Credential/Access objects for Database or external services | ||
- Ledger/Bank statement objects, we clone the object to the statistics and simulations. | ||
|
||
|
||
### Reference | ||
|
||
- https://refactoring.guru/design-patterns/prototype |