是否使用原型模式(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
最满意答案
是。
||
被称为短路运算符,意味着如果它知道足够返回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 ...
-
为什么这个版本的逻辑AND在C中没有显示短路行为?(Why is this version of logical AND in C not showing short-circuit behavior?)[2024-02-12]
这是一个棘手的问题。 b是sc_and方法的输入参数,因此将始终被评估。 在其他单词中, sc_and(a(), b())将调用a()并调用b() (不保证顺序),然后调用sc_and并将其结果传递给a?b:0 a(), b() a?b:0 。 它与三元运算符本身无关,这将绝对短路。 UPDATE 关于为什么我称之为“诡计问题”:这是因为缺乏明确的上下文来考虑“短路”(至少由OP转载)。 许多人在给出功能定义时, 假定问题的上下文是询问功能的正文 ; 它们通常不将功能视为表达自身。 这是问题的“诀窍” 为了 ... -
对C的短路评估(Short-circuit evaluation on C)[2022-08-10]
&&运算符使用懒惰评估 。 如果&&运算符的任何一边都是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 ...
-
如何在Windows PowerShell 4.0中执行短路评估?(How to perform short-circuit evaluation in Windows PowerShell 4.0?)[2023-03-16]
一组简单的测试案例表明短路工作: 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 ...