doulie0178 2017-09-13 06:00
浏览 68
已采纳

池模式和原型模式有什么区别?

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.

  • 写回答

2条回答 默认 最新

  • douxuan3095 2017-09-13 06:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧