覆盖现有记录的值但保留未覆盖的值的模式称为什么? [英] What is the pattern called where you override the values of an existing record but keep the values you didn't override?

查看:47
本文介绍了覆盖现有记录的值但保留未覆盖的值的模式称为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了大约六个地方:

I've seen this about six places:

  1. Maven 2 pom.xml 与您的项目 pom.xml 的配置本地项目pom会覆盖全局配置之一的值.
  2. vim 配置文件:在 vim 安装目录和用户配置文件中.
  3. Ruby on Rails(合理的默认值)
  4. Simphony 2(又名MICROS 9700的孙子),覆盖了来自企业(云)的所有记录.
  5. 在某种程度上,当子目录而不是继承其权限时,是在本地设置了父目录和子目录之间的NTFS ACL.
  6. 使用任何OOP语言扩展类的功能.
  1. Maven 2 Configuration of pom.xml vs. your projects pom.xml the local project pom overrides the value of the global configuration one.
  2. vim Configuration Files: in the vim install directory, and in your user profile.
  3. Ruby on Rails (Sensible Defaults)
  4. Simphony 2 (a.k.a. the great grandson of MICROS 9700) all the records that come down from the enterprise (cloud) get overridden.
  5. To some extent in NTFS ACLs between parent and child directories when the child directory instead of inheriting it's permissions, has it's down set locally.
  6. Extending a class in any OOP language in regards to functions.

推荐答案

我想您可以以多种方式查看此内容.正如 jaco0646所建议继承,或者也许更普遍地是子类型可能是一种查看方式.

I suppose that you can view this in more than one way. As jaco0646 suggests, inheritance, or, perhaps, more generally, subtyping could be one way of looking at it.

您也可以将其视为 mask ,例如位掩码:

You can also think of it as a mask, as in a bitmask:

    10010101   10100101
 OR 11110000   11110000
  = 11110101   11110101

您可以将其视为对二进制数组的二进制操作,如 x ||所示.y ,其中最左边的值 x 是默认值,而 y 包含您的替代值.

You can think of this as a binary operation on binary arrays, as in x || y, where the left-most value x is the default value, and y contains your overrides.

如果您将此想法概括化,则可以将其视为对类型的数据(例如配置数据)进行的二进制操作,该数据可以在多个级别上被部分覆盖.类似于 x<>y<z ,其中 x 是最通用的数据集(默认值),并且您获得的权限越靠右,覆盖的获取就越具体.

If you generalise this idea, you can think of it as a binary operation on a type of data (e.g. configuration data) that can be partwise overridden on multiple levels. Something like x <> y <> z, where x is the most general set of data (the defaults) and the further to the right you get, the more specific the overrides get.

如果您建立(明智的)规则来确定此操作是关联的,则您有一个 semigroup.一个半组称为 last ,该组总是选择最后一个(即最右边的)值.这样可以将您带到一半,但您只想选择最后一个值即可.然后,您可以将其与可选性"组合在一起,称为" Just "和" Nothing "(又称也许" ).以下是Haskell中的一些简单示例:

If you institute the (sensible) rule that this operation is associative, you have a semigroup. One semigroup is called last, which always picks the last (i.e. rightmost) value. That gets you halfway there, but you only want to pick the last value if it's there. You can combine it, then, with 'optionality', in the following called Just and Nothing (AKA Maybe). Here are some simple examples in Haskell:

Prelude Data.Semigroup> Just (Last 42) <> Just (Last 1337)
Just (Last {getLast = 1337})
Prelude Data.Semigroup> Just (Last 42) <> Nothing
Just (Last {getLast = 42})
Prelude Data.Semigroup> Just (Last 42) <> Nothing <> Just (Last 2112)
Just (Last {getLast = 2112})

如果默认值为中性"值,则您有一个 monoid .半群和 monoid组成.

If the default value is a 'neutral' value, you instead have a monoid. Both semigroups and monoids compose.

如果考虑类似可覆盖的配置值,则配置集通常是一组不同的值.例如,让我们考虑一个以毫秒为单位的超时值(一个整数)和一个默认名称(一个字符串).

If you consider something like overridable configuration values, a configuration set is typically a set of different values. As an example, let's consider a timeout value in milliseconds (an integer) and a default name (a string).

因此,您可以将此配置视为元组.(您可以为每个值创建一个带有字段名称的显式类型,但这对元组是同构的.)

Thus, you can consider this configuration as a tuple. (You can create an explicit type with field names for each value, but that would be isomorphic to a tuple.)

以下是默认值的示例:

Prelude Data.Semigroup> (Just (Last 1000), Just (Last "Foo"))
(Just (Last {getLast = 1000}),Just (Last {getLast = "Foo"}))

如果您只想覆盖 名称,则可以执行以下操作:

If you want to override only the name, you can do that:

Prelude Data.Semigroup> (Just (Last 1000), Just (Last "Foo")) <> (Nothing, Just (Last "Bar"))
(Just (Last {getLast = 1000}),Just (Last {getLast = "Bar"}))

后面的步骤也可以覆盖超时:

A later step can also override the timeout:

Prelude Data.Semigroup> (Just (Last 1000), Just (Last "Foo")) <> (Nothing, Just (Last "Bar"))
                            <> (Just (Last 2000), Nothing)
(Just (Last {getLast = 2000}),Just (Last {getLast = "Bar"}))

因此,从本质上讲,这个模式"只是半群的组成,其中 last 半群是内部半群,中间是 maybe/option ,由适当的元组作为最外面的半群组成.

So, in essence, this 'pattern' is just a composition of semigroups, with the last semigroup as the inner semigroup, maybe/option in the middle, and composed with an appropriate tuple as the outermost semigroup.

这篇关于覆盖现有记录的值但保留未覆盖的值的模式称为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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