想像你正在製作一個遊戲,遊戲中有許多相同種類的怪物。如果每次你需要一個新怪物時都從頭創建一個新的怪物物件,將需要很多的時間和資源。而如果你已經有了一個怪物物件,你可以用它作為原型,複製出新的怪物,這樣可以節省很多創建和初始化的時間和資源。
原型模式中的角色有以下幾種:

package prototype
type Prototype interface {
	GetName() string
	Clone(name string) Prototype
}
type ConcretePrototype struct {
	Name string
}
func (p *ConcretePrototype) GetName() string { return p.Name }
func (p *ConcretePrototype) Clone(name string) Prototype {
	return &ConcretePrototype{p.Name + " " + name}
}
package main
import (
	"fmt"
	prototype "prototype/Prototype"
)
func main() {
	proto := prototype.ConcretePrototype{Name: "Concrete Prototype"}
	clone := proto.Clone("Concrete Prototype Clone")
	fmt.Println("proto: ", proto.GetName())
	fmt.Println("clone: ", clone.GetName())
}