Prototype Pattern You Should Know in Golang(Design Patterns 04)

Elegant Object Cloning in Go: The Prototype Pattern Explained

Contact Author@:Medium,LinkedIn,Twitter

golang golang-prototype-pattern(from github.com/MariaLetta/free-gophers-pack)|300

Hello, here is Wesley, Today’s article is about prototype pattern in Go. Without further ado, let’s get started.💪

The Prototype pattern is also a type of creation pattern, which can help us elegantly create copies of objects. In this design pattern, the responsibility of cloning an object is delegated to the object itself that needs to be cloned. In Go, the Prototype pattern can be implemented by implementing the Clone method to copy objects.

Applicable Scenarios

  • High Object Creation Cost

    1. When creating a new object operation is resource-intensive (e.g., database operations) or costly. It’s more feasible to clone an object rather than creating a new one from scratch.
    2. For example, when the cloning process involves deep copying or hierarchical copying; or when the object being cloned has private members that cannot be directly accessed.
  • Dynamic Object Creation
    The type of object is determined at runtime. By using the cloning operation, you can generate objects of the required type without needing to determine the specific type at compile time.

Key Elements

  • Prototype Interface (Prototype): defines the interface for the cloning method, typically containing a Clone() method used to copy itself.
  • Concrete Prototype: implements the prototype interface and has the ability to copy itself.
  • Clone Laboratory (CloneLab): manages and stores prototype instances to facilitate cloning.

Code Implementation

Assuming we are developing a simple animal cloning laboratory that can clone different animals, each of which implements the Clone() interface.

1. Define Interface and Concrete Type

2. Creating a Clone Laboratory and Example

1
2
3
4
5
// output
Original Sheep: Name=Dolly, Weight=10, Info:[age: 1 gender: female]
Cloned Sheep: Name=Dolly, Weight=12, Info:[age: 1 gender: female]
Original Cow: Name=Bessie, Gender=female, Info:[age: 5 weight: 150]
Cloned Cow: Name=Bessie, Gender=female, Info:[age: 6 weight: 150]

As is well known, in Golang, only value copying is available, and for pointer references, deep copying is required to copy the contents, rather than just copying the pointer’s value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func (c *Cow) Clone() Cloneable {
newCow := &Cow{Name: c.Name, Gender: c.Gender}
if c.Info == nil {
return newCow
}
newCow.Info = make([]string, len(c.Info))
copy(newCow.Info, c.Info)
return newCow
}

// wrong case
func (c *Cow) Clone() Cloneable {
newCow := *c
return &newCow
}

And deep copying can be implemented in the following ways:

  1. Manually performing deep copying
  2. Serialization and deserialization
  3. Using some out-of-the-box deep copy libraries

Summary

  1. The Prototype pattern can be easily implemented in Go by using interfaces and deep copying logic.
  2. By cloning existing objects to create new ones, it provides a flexible and efficient way of object creation. It is very useful in scenarios where you need to create many similar objects, and can improve system performance and development efficiency.
  3. By implementing the Clone() method, various concrete types can easily generate their own copies, simplifying the process of managing and creating objects.

More

Recent Articles:

Random Article:


More Series Articles about You Should Know In Golang:

https://wesley-wei.medium.com/list/you-should-know-in-golang-e9491363cd9a

And I’m Wesley, delighted to share knowledge from the world of programming. 

Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. it would be a great help to me.

Give me some free applauds, highlights, or replies, and I’ll pay attention to those reactions, which will determine whether or not I continue to post this type of article.

See you in the next article. 👋

中文文章: https://programmerscareer.com/zh-cn/golang-prototype-pattern/
Author: Medium,LinkedIn,Twitter
Note: Originally written at https://programmerscareer.com/golang-prototype-pattern/ at 2024-12-01 18:56.
Copyright: BY-NC-ND 3.0

Object Pool Pattern You Should Know in Golang(Design Patterns 05) Singleton Pattern You Should Know in Golang(Design Patterns 03)

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×