什么是用C#一个良好的设计模式,它需要引用其他类课程? [英] What is a good design pattern in C# for classes that need to reference other classes?

查看:221
本文介绍了什么是用C#一个良好的设计模式,它需要引用其他类课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在C#.NET中的业务问题。我有两个类,名为C和W,将独立在不同的时间被实例化。

I am working on a business problem in C#.NET. I have two classes, named C and W that will be instantiated independently at different times.

C类的一个对象需要包含对0 ... n个等级的W的对象,即C的对象可以包含最多至n W对象

An object of class C needs to contain references to 0 ... n objects of class W, i.e. a C object can contain up to n W objects.

每个W均对象需要包含引用C类恰好1对象,即在W对象包含在一个C目标。

Each W object needs to contain a reference to exactly 1 object of class C, i.e. a W object is contained in one C object.

C类的目的通常是实例化的第一。在稍后的点,它的钨含量被发现,并且实例化。在这后一点上,我需要交叉引用C和W对象给对方。

An object of class C is usually instantiated first. At a later point, its W contents are discovered, and instantiated. At this later point, I need to cross reference the C and W objects to each other.

什么是好的设计模式呢?其实,我在那里我有三个或四个类别涉及案件,但我们可以谈论的是两大类,以保持它的简单。

What is a good design pattern for this? I actually have cases where I have three or four classes involved but we can talk about two classes to keep it simple.

我想的一些东西:

class C
{
   public List<W> contentsW;

}

class W
{
  public C containerC;

}

这将工作的时刻,但我可以预见无需编写code相当数量,以保持所有的引用和其有效性的轨道。我想实现code在路上做的只是容器和所有引用类的深刷新浅刷新。还有没有其他的办法和他们有什么好处呢?

This will work for the moment but I can foresee having to write a fair amount of code to keep track of all the references and their validity. I'd like to implement code down the road to do shallow refreshes of just the container and deep refreshes of all referenced classes. Are there any other approaches and what are their advantages?

编辑在11/3: 感谢所有的好答案和良好的讨论。我最终选择了JOP的答案,因为这最接近于我想做的事,但对方的回答也有帮助。再次感谢!

Edit on 11/3: Thanks to all for the good answers and good discussion. I finally chose jop's answer because that came closest to what I wanted to do, but the other answers also helped. Thanks again!

推荐答案

如果您有Martin Fowler的重构书,只要按照更改单向协会双向重构。

If you have the Martin Fowler's Refactoring book, just follow the "Change Unidirectional Association to Bidirectional" refactoring.

如果你没有的话,这里是如何你的类将类似于重构后:

In case you don't have it, here's how your classes will look like after the refactoring:

class C
{
  // Don't to expose this publicly so that 
  // no one can get behind your back and change 
  // anything
  private List<W> contentsW; 

  public void Add(W theW)
  {
    theW.Container = this;
  }

  public void Remove(W theW)
  {
    theW.Container = null;
  }

  #region Only to be used by W
  internal void RemoveW(W theW)
  {
    // do nothing if C does not contain W
    if (!contentsW.Contains(theW))
       return; // or throw an exception if you consider this illegal
    contentsW.Remove(theW);
  }

  internal void AddW(W theW)
  {
    if (!contentW.Contains(theW))
      contentsW.Add(theW);
  }
  #endregion
}

class W
{
  private C containerC;

  public Container Container
  {
    get { return containerC; }
    set 
    { 
      if (containerC != null)
        containerC.RemoveW(this);
      containerC = value; 
      if (containerC != null)
        containerC.AddW(this);
    }
  }
}

请注意,我已经做了名单,其中,W&GT; 私人。揭露直接曝光名单Ws内容的列表,通过一个枚举来代替。

Take note that I've made the List<W> private. Expose the list of Ws via an enumerator instead of exposing the list directly.

例如。公开名单GetWs(){返回this.ContentW.ToList(); }

e.g. public List GetWs() { return this.ContentW.ToList(); }

在code以上处理所有权转移正常。假设你有一些C两个实例 - C1和C2 - 和W的实例 - W1和W2

The code above handles transfer of ownership properly. Say you have two instances of C -- C1 and C2 - and the instances of W -- W1 and W2.

W1.Container = C1;
W2.Container = C2;

在code以上,C1包含W1和C2包含W2。如果重新分配W2为C1

In the code above, C1 contains W1 and C2 contains W2. If you reassign W2 to C1

W2.Container = C1;

然后C2将具有零项和C1将有两个项目 - W1和W2。你可以有一个浮动的是W

Then C2 will have zero items and C1 will have two items - W1 and W2. You can have a floating W

W2.Container = null;

在这种情况下,W2将被从C 1的列表中删除,并且将没有容器。您还可以使用添加和删除从C方法来操作W酒店的容器 - 所以C1.Add(W2)将自动删除W2从它原来的容器中,并将其添加到新的

In this case, W2 will be removed from C1's list and it will have no container. You can also use the Add and Remove methods from C to manipulate W's containers - so C1.Add(W2) will automatically remove W2 from it's original container and add it to the new one.

这篇关于什么是用C#一个良好的设计模式,它需要引用其他类课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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