池模式(Pool Pattern)和原型模式(Prototype Pattern)之间有什么区别?

huangapple go评论83阅读模式
英文:

what is the difference between pool and prototype patterns?

问题

我刚刚阅读了《golang设计模式》一书中关于创建型模式的章节。这些模式包括:

  • 工厂模式(factory)
  • 抽象工厂模式(abstract factory)
  • 原型模式(prototype)
  • 单例模式(singleton)
  • 建造者模式(builder)

我在GitHub上创建了一个仓库来列出它们。我还搜索了其他类似的仓库:

是否存在一个官方的go语言创建型模式列表?了解这个列表重要吗?还是更重要的是知道可能的模式,并能够在正确的上下文中使用正确的代码?是否需要了解世界上所有的模式?

另外,原型模式和对象池模式有什么区别?它们在我看来非常相似。

英文:

I've just read the chapter on Creational patterns in the book Design Patterns in golang. These are:

  • factory
  • abstract factory
  • prototype
  • singleton
  • builder

I've created a repository on github to list all of them. I've looked for other repositories like mine:

Some list object-pool as a pattern, others simple-factory and factory-method. Does there exist an official list of creational patterns in go? Is it important to know the list, or is it more important to know what is possible and be able to use right code in right context? With or without know all the patterns in the world?

And, ... what is the difference between prototype and object pool patterns? They look very similar to me.

答案1

得分: 6

一个对象池有助于限制新对象的分配数量;原型是指定如何创建新对象的一种方式。

对象池主要关注缓存已创建的对象实例,以便以后可以访问。Golang有一个实现(https://golang.org/pkg/sync/#Pool),主要用于减轻垃圾收集器的压力。使用对象池的常规方式如下:

  1. 向对象池请求一个实例
  2. 对象池要么返回一个未使用的对象,要么分配一个新对象,并将新实例返回给你
  3. 使用对象进行所需操作
  4. 将对象返回给对象池,以便另一个“客户端”可以使用它。

Pool的实现中,你会注意到可以传递一个“创建”函数来指定对象池在第2步中如何创建实例。

New func() interface{}

这个函数可以简单地分配一个空结构体,或者如果创建逻辑复杂,可以使用原型模式来克隆新实例。

原型模式更关注如何从原型对象创建许多“副本”对象。当你想尽量避免使用new关键字时,通常会使用这种模式。

英文:

A Pool helps limit the number of new objects allocated; a Prototype is a way of specifying how a new object is created.

Object pools are concerned with caching created instances of objects that can be accessed later. Golang has an implementation (https://golang.org/pkg/sync/#Pool) which is mainly used for relieving pressure on the garbage collector. The usual way of using a pool follows something like:

  1. ask the pool for an instance
  2. The pool will either give you back an unused object, or it will allocate a new object, and give the new instance back to you
  3. You use the object to do whatever you need
  4. You give the object back to the pool so that another "client" can use it.

In the Pool implementation, you'll notice that you can pass a "creation" function to specify how the pool should create an instance in Step 2.

New func() interface{}

This function could simply allocate an empty struct, or it could use a Prototype pattern to clone new instances if the creation logic was complicated.

The Prototype pattern is more focused on abstracting how you can create many "copies" of an object from a prototype object. You mainly use this pattern when you want to try and avoid the use of the new keyword.

答案2

得分: 5

设计模式并不孤立存在,它们之间存在着相互关系。例如,对象池可能使用工厂或原型来初始化池或处理耗尽的情况。该池中的对象可能符合命令模式,或者是建造者模式的示例。

在实践中,你不可能知道所有模式的所有细节,最重要的是了解它们的意图,即模式所要实现的目标。在使用模式时,剩下的细节可以从模式目录中获取,并且应该从中获取。随着时间的推移,你会全面了解一些模式,并参考其他模式。

模式目录是一组完整记录的设计模式。《GoF设计模式》书籍可能是最著名的模式目录,但你提到的书籍和GitHub存储库也可以达到同样的目的。

英文:

Design Patterns do not exist in isolation, they have relationships of their own. So for example, an object pool might use a factory or prototype to initially fill the pool or deal with exhaustion. The objects in that pool might conform to the command pattern, or be examples of the builder pattern.

In practice you cannot possibly know all the details of all patterns, the most important thing to know is the intent. What the pattern achieves. When using the pattern the remaining details can and should be taken from a pattern catalogue, over time you will learn some patterns comprehensively and reference others.

A Pattern Catalogue is the a collection of fully documented Design Patterns. The GoF Design Patterns book is probably the most famous Pattern Catalogue, however the book you mention and even github repositories can fulfil the same purpose.

池模式(Pool Pattern)和原型模式(Prototype Pattern)之间有什么区别?

huangapple
  • 本文由 发表于 2017年9月13日 14:00:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/46189914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定