我有一些类是这样的:是加入这个分层级的反模式
+---+ +---+
| L | ---uses---> | D |
+---+ +---+
| +---+
inherit ----<---- | V |
| +---+ +---+
+---<--- | C |
+---+
假设;类L
是一个抽象类,并且V
和C
从它继承。并且L
有类D
的财产。 - 对不起,我的坏绘画和英语的也
现在,我需要添加一个新的类 - RA
- 有D
类和V
类的属性应该有RA
类的属性,但我也需要得到财产从类L
因此V
的对象。在我看来是这样的:
+---+ +---+ +----+
| L | --uses--> | D | <--uses-- | RA |
+---+ +---+ +----+
| +---+ |
inherit ----<----| V | --uses-->--+
| +---+ +---+
+---<---| C |
+---+
我不确定!但在我看来,像某种反固定模式或断路器的固体原理 - 不知何故!但我无法弄清楚。
我的新图是反模式吗?还是有更好的方法来设计这种情况?
请注意,像D
,V
,C
和RA
这样的类可以实例化。
我的意思是类D
的属性的使用现在分级在V
。
编辑:
想象一下,我使用的接口 - IGFP
- 通过使用D
财产L
所以在V
和C
返回string
值,现在在V
我需要通过RA
代替D
覆盖它。
在C#类是:
public abstract class L : VOBase<L>, IGFP {
public virtual D D { get; protected set; }
public virtual string Name { get; protected set; }
public virtual string GFP => $"{D.GFP}/{Name}"; // Here I use D property that I think breaks LSP
}
public class C : L {
public C (D d, string name) {
D = d;
Name = name;
}
}
public class V : L {
public V (RA ra, string name) {
RA = ra;
Name = name;
D = ra.D;
}
public RA RA { get; private set; }
public override string GFP => $"{RA.GFP}/{Name}"; // Here I should override GFP to use RA instead of D
}
public class D : VOBase<D>, IGFP {
public D (U u, string name) {
U = u;
Name = name;
}
public U U { get; private set; }
public string Name { get; private set; }
public string GFP => $"{U.GFP}/{Name}";
}
public class RA : VOBase<RA>, IGFP {
public RA (D d, string name) {
D = d;
Name = name;
}
public D D { get; private set; }
public string Name { get; private set; }
public string GFP => $"{D.GFP}/{Name}";
}
tnx为你的答案,我认为你的意思是没有任何反模式,我会在这里添加一些代码以使其更有意义;)。 –
我这样说:“你的图不表示任何反模式” – weston
我已经做了一些简化,代码仍然看起来错了吗? – weston