首页 \ 问答 \ 是否使用原型模式(Use prototype pattern or not)

是否使用原型模式(Use prototype pattern or not)

我有个问题。 我需要创建一些与产品有关的东西。 现在我可以说7种不同类型的产品。 有些是其他的亚型,例如

Cars
 - Vans
   - petrol
   - diesel
 - City
 - Hatchback
 - Saloon
 - Estate
   - petrol
   - diesel

现在,为了争论,我所有的城市,两厢车和轿车都是混合动力/汽油/什么,我不打算出售汽油和柴油。 然而,未来某个时候我可能会有汽油和柴油轿车,但这并不是说我将拥有20多种类型的产品。 如果它会上升,我可能会有2-3种类型。

根据我的理解Prototype Pattern在这里可能是好的,因为我将能够避免在庄园 - >汽油和范->汽油之间的重复......但是再次,范汽车将具有与城市汽车不同的特征,例如最大装载尺寸。

我一直在阅读有关设计模式的广泛内容,我记得一件事就是当你不需要它时不要使用模式。 现在的问题是 - 我需要它吗?

谢谢!


I have a question. I need to create a little thing to do with products. Now I can have say 7 different types of products. Some are subtypes of others e.g.

Cars
 - Vans
   - petrol
   - diesel
 - City
 - Hatchback
 - Saloon
 - Estate
   - petrol
   - diesel

Now, for the sake of the argument all my City, Hatchback and Saloon cars are hybrid/gas/whatever and I do not plan to sell petrol and diesel ones. However there is a possibility that I may have petrol and diesel saloon cars sometime in the future, but it's not like I am going to have 20+ types of products. If it is going to go up I will have probably 2-3 more types.

From what I understand Prototype Pattern may be good one here for I will be able to avoid duplication between estate->petrol and van->petrol ... but then again Van cars will have different characteristics than say city car e.g. maximum loading dimensions.

I have been reading extensively about design patterns and one thing I remember for certain is not to use pattern when you don't need it. Now the question is - do I need it?

Thanks!


原文:https://stackoverflow.com/questions/7925943
更新时间:2024-01-22 12:01

最满意答案

是。 || 被称为短路运算符,意味着如果它知道足够返回true(它的第一个操作数为真)那么它将不会计算第二个操作数并立即返回true。

&&是相似的 - 如果它的第一个操作数返回false,它会立即返回false而不检查第二个操作数。


Yes. || is known as a short circuiting operator, meaning if it knows enough to return true (that its first operand is true) then it will not evaluate the second operand and immediately return true.

&& is similar - if its first operand returns false, it immediately returns false without checking the second.

相关问答

更多
  • 是的,C ++中的&&操作符使用短路评估,因此如果bool1评估为false ,则不会打扰评估bool2 。 “短路评估”是您希望Google在索引中寻找的一个奇特术语。 Yes, the && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn't bother evaluating bool2. "Short-circuit evaluation" is the fancy ...
  • 是的,JavaScript有“短路”评估。 if (true == true || foo.foo){ // Passes, no errors because foo isn't defined. } 现场演示 if (false && foo.foo){ // Passes, no errors because foo isn't defined. } 现场演示 Yes, JavaScript has "short-circuit" evaluation. if (true == t ...
  • 这是一个棘手的问题。 b是sc_and方法的输入参数,因此将始终被评估。 在其他单词中, sc_and(a(), b())将调用a()并调用b() (不保证顺序),然后调用sc_and并将其结果传递给a?b:0 a(), b() a?b:0 。 它与三元运算符本身无关,这将绝对短路。 UPDATE 关于为什么我称之为“诡计问题”:这是因为缺乏明确的上下文来考虑“短路”(至少由OP转载)。 许多人在给出功能定义时, 假定问题的上下文是询问功能的正文 ; 它们通常不将功能视为表达自身。 这是问题的“诀窍” 为了 ...
  • &&运算符使用懒惰评估 。 如果&&运算符的任何一边都是false ,那么整个表达式就是false 。 C检查操作符左侧的真值,在你的情况下为0 。 由于c中的0为假,因此操作的右侧表达式(a = b = 777)永远不会被评估。 第二种情况是类似的,除了|| 如果左侧表达式返回true则返回true 。 还要记住,在c中,任何不是0东西都被认为是true 。 希望这可以帮助。 The && operator uses lazy evaluation. If either side of the && op ...
  • 我会这样说 if ((x > 0) && (bar[x] == foo)) 编码风格也不错 。 它甚至可能都很好 。 我肯定更喜欢它像你描述的嵌套if结构。 顺便说一句,我会减少你使用的括号数量。 这些都是同样正确的,至少在C#和大多数其他C语言中是这样的: if (x > 0 && bar[x] == foo) if (x > 0 && x < bar.Length && bar[x] == foo) 知道该语言的读者(你必须在某种程度上假设这一点)将能够很容易地理解上述短路表达式。 那些读者可能会反 ...
  • 是。 || 被称为短路运算符,意味着如果它知道足够返回true(它的第一个操作数为真)那么它将不会计算第二个操作数并立即返回true。 &&是相似的 - 如果它的第一个操作数返回false,它会立即返回false而不检查第二个操作数。 Yes. || is known as a short circuiting operator, meaning if it knows enough to return true (that its first operand is true) then it will n ...
  • 一组简单的测试案例表明短路工作: PS C:\> 1 -eq 0 -or $(Write-Host 'foo') foo False PS C:\> 1 -eq 1 -or $(Write-Host 'foo') True PS C:\> 1 -eq 1 -and $(Write-Host 'foo') foo False PS C:\> 1 -eq 0 -and $(Write-Host 'foo') False A simple set of test cases show that short-c ...
  • 你问过几个不同的问题,所以我会尽力解决这些问题。 错过分支预测:除非您使用C或汇编编码,否则不要担心。 在PHP中,你离硬件太远了,考虑分支预测不会对你有所帮助。 无论哪种方式,这将是一个非常微观的优化,尤其是在开始广泛的字符串解析的函数中。 有没有其他理由不使用它? 维基百科似乎这么认为,但只是参考C.我知道PHP是用C语言编写的,所以这绝对是相关的信息。 PHP可能会将其解析为不同的执行结构。 除非你计划运行这个功能数百万次,否则你知道这是一个瓶颈,我不担心。 在2012年,我发现使用or短路不太可能导 ...
  • 这里的问题只是对实际发生的一种误解。 折叠表达式中如何进行短路评估 ? 它可用折叠表达式。 (args && ... )遵循与(a && b && c && d)完全相同的规则。 也就是说,只有在a , b和c都评估为真的情况下才会评估d。 这不是你们两种情况之间的实际差异。 false && (*pb = true); // ok at runtime. AndL(false, (*pb = true)); // error at runtime! 虽然折叠表达式与非折叠表达式完全相同, ...
  • 是。 在Ruby标准的最终草案中, all? 定义如下: 在接收器上调用each方法 对于每个方法的each元素X: 如果给出了块,则以X为参数调用块。 如果此调用返回falseish对象,则返回 false 。 如果没有给出块,并且X是虚假对象,则返回 false 。 返回true 。 请注意步骤2中的单词return 。这保证了短路评估。 any? 被类似地定义。 然而,标准仍然是一个草案,我不知道哪些Ruby实现(如果有的话)的目标是符合标准。 Yes. In the final draft of t ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。