我正在寻找实现这一点的最佳方法,本质上是一种适合的模式,给定一个与兄弟姐妹和孩子有关系的对象。
我们有一套复杂的规则来设置每个对象的状态
例如
- n个“类型”A的兄弟对象,那么每个对象的状态都是x
- n 个兄弟对象“type”A 和“type”B 然后每个的状态是 y
这可能还有 3 或 4 个变体
对于每个兄弟对象,其子对象的组成将确定它的“类型”
希望这足够清楚,如果您认为我可以添加更多说明,请发表评论?
编辑:
添加了一些伪代码
状态将与 Foo 对象(而不是 FooBar 或 Bar)一起保留,并且状态在用户驱动的事件上更新(用户可以修改 Foos 和 Bars 的组合,然后生成事件以重新调查,设置状态并持久化到数据库)
希望这可以帮助
void Main()
{
var foobar = GrabAFooBar();
//Each Foo will have its state set based on the rules in the question
//eg
//foobar has 2 Foos both of which only contain BarType1 then both foos have a State of StateOne
//foobar has 2 foos, one has a BarType1 one has a BarType2 both foos have a State of StateTwo
//foobar has 2 foos, one has a BarType1 and BarType3 one has a BarType2 both foos have a State of StateThree
//effectivaly there are 5 States (currently) and a well defined set of combinations
//All foos will have the same state at the end of the process (there will never be a mix)
}
FooBar GrabAFooBar()
{
//return a FooBar from data
}
// Define other methods and classes here
class FooBar
{
List<Foo> Foos {get;set;}
}
class Foo
{
public List<Bar> Item {get;set;}
public State state {get;set;}
}
abstract class Bar
{
}
class BarType1 : Bar
{
}
class BarType2 : Bar
{
}
class BarType3 : Bar
{
}
enum State
{
StateOne,
StateTwo,
StateThree
}