原型设计模式的重点是什么? [英] What's the point of the Prototype design pattern?

查看:89
本文介绍了原型设计模式的重点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在学校学习设计模式。今天,有人告诉我原型设计模式。



我一定想念一些东西,因为我看不到它的好处。我见过网上人说它比使用 new 更快,但这没有道理;在某个时候,无论如何创建新对象,都需要为其分配内存。



这种模式是否与鸡肉或鸡蛋问题处于同一圈子?由于原型模式本质上只是克隆对象,因此在某些时候必须创建原始对象(即不克隆)。这将意味着我需要为要克隆的每个对象都准备好要克隆的现有副本?



有人可以解释此模式的用途吗?

解决方案

原型模式是基于克隆预配置对象的创建模式。这个想法是,您选择一个配置为默认值或在某些特定用例范围内的对象,然后克隆该对象并根据您的实际需求进行配置。



<当需要进行繁琐的配置时,该模式可用于删除大量样板代码。我认为原型是一个预设对象,您可以在其中保存一堆状态作为新的起点。


So I'm learning about design patterns in school. Today I was told about the 'Prototype' design pattern.

I must be missing something, because I don't see the benefits from it. I've seen people online say it's faster than using new but this doesn't make sense; at some point, regardless of how the new object is created, memory needs to be allocated for it.

Doesn't this pattern run in the same circles as the 'chicken or egg' problem? Since the Prototype pattern essentially is just cloning objects, at some point the original object must be created itself (i.e. not cloned). This would mean that I need to have an existing copy of every object I want to clone already ready to clone?

Can anyone explain what the use of this pattern is?

解决方案

The Prototype pattern is a creation pattern based on cloning a pre-configured object. The idea is that you pick an object that is configured for either the default or in the ballpark of some specific use case and then you clone this object and configure to your exact needs.

The pattern is useful to remove a bunch of boilerplate code, when the configuration required would be onerous. I think of Prototypes as a preset object, where you save a bunch of state as a new starting point.

这篇关于原型设计模式的重点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆