- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个上下文对象,它有几个有用的属性和方法。其他对象在其构造函数中接受此上下文对象。但是,上下文对象可以将这些其他对象作为公共(public)属性。见下文:
public class Foo {
private IContext Context { get; private set; }
public Foo( IContext context ) {
Context = context;
}
}
public class SomeContext : IContext {
public Foo SomeProperty { get; set; }
/*
Other useful properties and methods as defined in IContext
*/
public SomeContext () {
SomeProperty = new Foo( this );
}
}
现在我可以在 Foo 的方法中做一些有用的事情了:
public void FooIt() {
IUseful bar = this.Context.GetUsefulInterface();
bar.DoUsefulThing();
}
但是,它可能会导致一些非常奇怪的事情。考虑 Foo 上的这些方法:
public void DoSomething() {
/* useful stuff */
}
public void DoSomethingElse() {
this.Context.SomeProperty.DoSomething(); // could just call this.DoSomething();
this.Context.SomeProperty.DoSomethingElse(); // stack overflow!
}
这是否被认为是糟糕的设计/代码味道?上下文对象的原因有些牵扯,我想将问题更多地指向循环引用。如果认为不好,有哪些方法可以打破这种循环关系?
最佳答案
如果没有领域知识,在尝试设计循环依赖时很难知道从哪里开始,所以我只是概括一下。
当您不表达层次关系时,循环引用是不好的,在这种关系中返回父级是必需的或有用的。这是因为它促进了循环中类型之间的强耦合,很难正确地构造/删除,并且在遍历时容易出现错误。
但是,当您具有层次关系时,并且需要/有用地遍历层次结构,那么循环引用就非常有意义了。
您可能想要避免的一件事是尝试一次对您的属性进行过深的调用。这会增加您的耦合度,并且很容易导致错误,例如您发现的堆栈溢出异常。
// Reaches too far. Makes this depend on the interface of SomeProperty
this.Context.SomeProperty.DoSomething();
// ...
// Not reaching too far. Only depends on Context.
// This might forward to SomeProperty.DoSomething()
this.Context.DoSomething();
根据您修复它的方式,这也可能有助于您解决堆栈溢出问题。
关于c# - 对象 A 引用对象 B,对象 B 将对象 A 作为公共(public)属性(property) - 糟糕的设计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395961/
我想在这里说的是我在从之前离开的相同状态重新启动我的应用程序时遇到的问题。我在这方面做了很多研发,并且已经解决了 stackoverflow 中发布的问题。所以请不要说它是重复的。 我试过设置这些选项
当我在我的类中实现 __cmp__ 函数时,python 是否会在内部重载“==”,我们在 C++ 中是如何做到的? 只是好奇。我是 python 的新手。 :) 最佳答案 ==的含义当您定义 __c
我在 Raspberry Pi2 上安装了 Gitlab,几个月来它运行良好。但自从关闭了RPi的电源后,它就不再起作用了。网页返回502错误。 502 Whoops, GitLab is takin
有人知道用户登陆带有Webfonts的页面时为什么Google Chrome浏览器崩溃吗 它并不会一直发生,而是经常发生 我刚得到一个蓝屏页面,却不知道为什么:该页面不是来自重定向时就很好了。 这是我
当我登录时,Skype始终会给出此错误。 糟糕,Skype存在问题。尝试注销然后重新登录。 STARTUP_LOAD_ERROR MACBOOK 最佳答案 Macbook 用户 退出Skype 回家
我正在尝试从 flutter 开始,首先我在 cmd 上运行 flutter doctor 它有效。在我安装了 ANDROID SDK 之后,同样的命令 flutter doctor 给了我异常:
从 android studio 终端运行 flutter attach 不工作。显示错误flutter 意外退出。 终端输出: flutter attach Checking for adverti
当使用 TinyMCE 4 测试所有浏览器时,Chrome 非常慢。 (我尝试从 TinyMCE 中删除所有插件,但没有任何区别。) Chrome 需要大约 20-25 秒在 TinyMCE 中呈现一
我试图让下面的脚本工作,以便从远程服务器(服务器 1)读取特定目录中的 CSV 文件列表,并将数据移植到另一台服务器的 PostgreSQL 数据库中。 我已经创建了一个 rsa SSH key 并将
在嵌入式 linux 环境中(在 PowerPC 上定制的 2.4.25)几个小时后我得到以下内核 panic : Oops: kernel access of bad area, sig: 11 N
在将现有 Node.js (Hapi.js) + RethinkDB 从 OVH VPS(最小 vps)迁移到 AWS Lambda( Node )+ DynamoDB 的过程中,我最近遇到了一个非常
我是一名优秀的程序员,十分优秀!