原型模式

模式动机

原型模式使对象能复制自身,并且暴露到接口中,使客户端面向接口编程时,不知道接口实际对象的情况下生成新的对象。

原型模式配合原型管理器使用,使得客户端在不知道具体类的情况下,通过接口管理器得到新的实例,并且包含部分预设定配置。

Go语言实现

prototype.go

  1. package prototype
  2. //Cloneable 是原型对象需要实现的接口
  3. type Cloneable interface {
  4. Clone() Cloneable
  5. }
  6. type PrototypeManager struct {
  7. prototypes map[string]Cloneable
  8. }
  9. func NewPrototypeManager() *PrototypeManager {
  10. return &PrototypeManager{
  11. prototypes: make(map[string]Cloneable),
  12. }
  13. }
  14. func (p *PrototypeManager) Get(name string) Cloneable {
  15. return p.prototypes[name]
  16. }
  17. func (p *PrototypeManager) Set(name string, prototype Cloneable) {
  18. p.prototypes[name] = prototype
  19. }

prototype_test.go

  1. package prototype
  2. import "testing"
  3. var manager *PrototypeManager
  4. type Type1 struct {
  5. name string
  6. }
  7. func (t *Type1) Clone() Cloneable {
  8. tc := *t
  9. return &tc
  10. }
  11. type Type2 struct {
  12. name string
  13. }
  14. func (t *Type2) Clone() Cloneable {
  15. tc := *t
  16. return &tc
  17. }
  18. func TestClone(t *testing.T) {
  19. t1 := manager.Get("t1")
  20. t2 := t1.Clone()
  21. if t1 == t2 {
  22. t.Fatal("error! get clone not working")
  23. }
  24. }
  25. func TestCloneFromManager(t *testing.T) {
  26. c := manager.Get("t1").Clone()
  27. t1 := c.(*Type1)
  28. if t1.name != "type1" {
  29. t.Fatal("error")
  30. }
  31. }
  32. func init() {
  33. manager = NewPrototypeManager()
  34. t1 := &Type1{
  35. name: "type1",
  36. }
  37. manager.Set("t1", t1)
  38. }