帮助我在两种设计之间进行选择
// 愚蠢的标题,但我想不出有什么更聪明的东西,
我有一个代码(见下文,抱歉代码很长,但是它非常非常简单):
namespace Option1
{
class AuxClass1
{
string _field1;
public string Field1
{
get
{
return _field1;
}
set
{
_field1 = value;
}
}
// another fields. maybe many fields maybe several properties
public void Method1()
{
// some action
}
public void Method2()
{
// some action 2
}
}
class MainClass
{
AuxClass1 _auxClass;
public AuxClass1 AuxClass
{
get
{
return _auxClass;
}
set
{
_auxClass = value;
}
}
public MainClass()
{
_auxClass = new AuxClass1();
}
}
}
namespace Option2
{
class AuxClass1
{
string _field1;
public string Field1
{
get
{
return _field1;
}
set
{
_field1 = value;
}
}
// another fields. maybe many fields maybe several properties
public void Method1()
{
// some action
}
public void Method2()
{
// some action 2
}
}
class MainClass
{
AuxClass1 _auxClass;
public string Field1
{
get
{
return _auxClass.Field1;
}
set
{
_auxClass.Field1 = value;
}
}
public void Method1()
{
_auxClass.Method1();
}
public void Method2()
{
_auxClass.Method2();
}
public MainClass()
{
_auxClass = new AuxClass1();
}
}
}
class Program
{
static void Main(string[] args)
{
// Option1
Option1.MainClass mainClass1 = new Option1.MainClass();
mainClass1.AuxClass.Field1 = "string1";
mainClass1.AuxClass.Method1();
mainClass1.AuxClass.Method2();
// Option2
Option2.MainClass mainClass2 = new Option2.MainClass();
mainClass2.Field1 = "string2";
mainClass2.Method1();
mainClass2.Method2();
Console.ReadKey();
}
}
您喜欢什么选项(选项1或选项2)?在什么情况下我应该使用 option1 或 option2 ? option1 或 option2 (组合、聚合)有什么特殊名称吗?
// stupid title, but I could not think anything smarter
I have a code (see below, sorry for long code but it's very-very simple):
namespace Option1
{
class AuxClass1
{
string _field1;
public string Field1
{
get
{
return _field1;
}
set
{
_field1 = value;
}
}
// another fields. maybe many fields maybe several properties
public void Method1()
{
// some action
}
public void Method2()
{
// some action 2
}
}
class MainClass
{
AuxClass1 _auxClass;
public AuxClass1 AuxClass
{
get
{
return _auxClass;
}
set
{
_auxClass = value;
}
}
public MainClass()
{
_auxClass = new AuxClass1();
}
}
}
namespace Option2
{
class AuxClass1
{
string _field1;
public string Field1
{
get
{
return _field1;
}
set
{
_field1 = value;
}
}
// another fields. maybe many fields maybe several properties
public void Method1()
{
// some action
}
public void Method2()
{
// some action 2
}
}
class MainClass
{
AuxClass1 _auxClass;
public string Field1
{
get
{
return _auxClass.Field1;
}
set
{
_auxClass.Field1 = value;
}
}
public void Method1()
{
_auxClass.Method1();
}
public void Method2()
{
_auxClass.Method2();
}
public MainClass()
{
_auxClass = new AuxClass1();
}
}
}
class Program
{
static void Main(string[] args)
{
// Option1
Option1.MainClass mainClass1 = new Option1.MainClass();
mainClass1.AuxClass.Field1 = "string1";
mainClass1.AuxClass.Method1();
mainClass1.AuxClass.Method2();
// Option2
Option2.MainClass mainClass2 = new Option2.MainClass();
mainClass2.Field1 = "string2";
mainClass2.Method1();
mainClass2.Method2();
Console.ReadKey();
}
}
What option (option1 or option2) do you prefer ? In which cases should I use option1 or option2 ? Is there any special name for option1 or option2 (composition, aggregation) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据德米特定律,选项2。这样你就可以自由地改变MainClass的实现,你不必担心调用依赖于AuxClass1细节的代码,并且如果需要的话确实可以完全删除它。
According to Law of Demeter, Option2. That way you can freely change the implementation of MainClass, You don't have to worry about calling code relying on details of AuxClass1, and indeed can remove it entirely if needed.
编辑
原始
我认为 MainClass 应该从 AuxClass 派生..
EDIT
Original
I tihnk MainClass should derive from AuxClass..
我将从实现 C# 的一个很好的功能(称为“自动属性”)开始。 而不是编写
您可以编写
,编译器将生成完全相同的 IL。除此之外,您可以添加一些选项,例如将 setter 设置为私有:
在您的情况下,我会选择选项 3:
这样,您可以在设置后锁定 AuxClass 引用(如选项 2 所示),而不锁定请自行了解 AuxClass 接口的更改(如选项 1)。
I would start with implementing a nice feature of C# called "automatic properties". Instead of writing
you can write
and the compiler will generate the exact same IL. On top of this, you can add some options, for example making the setter private:
In your case, I would go for option 3:
This way, you lock the AuxClass reference once it's set (like in Option 2) while not locking up yourself for changes in the AuxClass interface (like in Option 1).
选择设计的决定基于不同的因素,
Decision of choosing design is based on different factors,