- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
出于各种原因,我想开始在设计中使用更多不可变类型。目前,我正在处理一个现有类如下的项目:
public class IssueRecord
{
// The real class has more readable names :)
public string Foo { get; set; }
public string Bar { get; set; }
public int Baz { get; set; }
public string Prop { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
public string Prop5 { get; set; }
public string Prop6 { get; set; }
public string Prop7 { get; set; }
public string Prop8 { get; set; }
public string Prop9 { get; set; }
public string PropA { get; set; }
}
这个类代表了一些确实有这么多属性的磁盘格式,所以在这一点上将它重构为更小的位几乎是不可能的。
这是否意味着此类的构造函数在不可变设计中真的需要有 13 个参数?如果不是,如果我要使这个设计不可变,我可以采取什么步骤来减少构造函数中接受的参数数量?
最佳答案
要减少参数的数量,您可以将它们分组到合理的集合中,但要拥有真正不可变的对象,您必须在构造函数/工厂方法中对其进行初始化。
有些变体是创建“构建器”类,您可以使用流畅的界面进行配置,而不是请求最终对象。如果您实际上计划在代码的不同位置创建许多此类对象,那么这将是有意义的,否则在一个地方使用多个参数可能是可以接受的权衡。
var immutable = new MyImmutableObjectBuilder()
.SetProp1(1)
.SetProp2(2)
.Build();
关于c# - 不可变设计 : Dealing with Constructor Insanity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12291621/
我正在以 XML 格式解析和存储从服务器获取的数据。该应用程序是一个瘦客户端,它非常频繁地请求这样的服务器端数据。一些变量将是触发更多网络调用的 uris。数据的形式是: value
我更喜欢编码标准是合乎逻辑的。这是我为什么不遵循以下一组标准的观点。 我需要知道以下两件事之一:(1)为什么我错了,或者(2)如何说服我的团队更改它们。 camelCase:函数,类名,方法和变量必须
在我的一个 Java 项目中,由于 Java 处理 (not) 的方式,代码重复困扰着我。原语。必须手动将相同的更改复制到四个不同的位置(int、long、float、double)再次,对于第三次,
出于各种原因,我想开始在设计中使用更多不可变类型。目前,我正在处理一个现有类如下的项目: public class IssueRecord { // The real class has mo
我们有一个 3 主机的 Kafka 集群。我们有 136 个主题,每个主题有 100 个分区,复制因子为 3。这使得我们的集群中有 13,600 个分区。 这是我们主题的合理配置吗? 最佳答案 太多了
这绝对让我感到震惊,!&&%&$ 疯了......它无视我能想到的一切。 这个角色就在这里... " " 在这些引号之间......打开谷歌浏览器并检查。你会看到一个 ……正常吧?现在右
这样做的最佳方法是什么?这是一个 250GB 的文本文件,每行 1 个单词 输入: 123 123 123 456 456 874 875 875 8923 8932 8923 想要的输出: 123
memberA 定义在 ClassA 的头部。 memberB 定义在 ClassB 的头部。 B类是A类的子类 在 ClassB 的实例中,通过简单赋值设置 memberA: memberA = 0
这个问题在这里已经有了答案: Check legal characters by regular expression but with unexpected result (2 个答案) 关闭 7
我正在阅读关于 if consteval (§3.2) 的 C++ 论文,并看到一段显示 constexpr strlen 实现的代码: constexpr size_t strlen(char co
This is the game 我已经成功地生成了随机混合的 4 种颜色和 4 个立方体的问题,并遵循链接中建议的配色方案。 因此,目标是使用 clpfd 生成问题的可能解决方案。主要原则是基本的,
#include union NumericType { float value; int intvalue; }Values; int main() { Values.va
献给所有喜欢低级 Window Server(CoreGraphicsPrivate.h 等)、Mac 上的 X11、SIMBL 和其他疯狂东西的人:) Mac 上有一个简单的 X11 模拟应用程序(
我在一个 pygame 项目上工作,其中程序保存了大量的对象(100 000 + 对象)。该脚本基于每组对象之间的大距离。为简单起见,该计划是一张无边界的 map ,上面有一个在上面移动的相机。对象密
我不知道发生了什么,但我正在试验 GSUB 然后决定我不想再使用它所以将我的 link_to 参数改回: 现在我不断收到这个错误:nil:NilClass 的未定义方法 `gsub' 当我从我的表演
来自this线程(Control.Monad.Cont fun,2005),Tomasz Zielonka 引入了一个函数(Thomas Jäger 以清晰而漂亮的方式进行了评论)。 Tomasz 获
我是一名优秀的程序员,十分优秀!