ComputeLink<T> where T : ISeat3 3 { ComputeLink<T> Child { T Obj { ComputeLink(T obj, ComputeLink<T> parent, Action<T> method) 8 { 9 if (obj == null) ArgumentNullException(); 11 this.Obj = obj; 12 this.Obj.Method = x => method((T)x); 13 if (parent != null) 14 { 15 this.Parent = parent; 16 parent.Child = this; 17 parent.Obj.Child = this.Obj; 18 } 19 } ComputeLink<T> New(T obj, Action<T> method) 21 { ComputeLink<T>(obj, null, method); 23 } ComputeLink<T> Do(T obj, Action<T> method) 26 { ComputeLink<T>(obj, this, method); 28 } { { 33 if (null != this.Parent) .Parent.Head; ; 36 } 37 } Action() { 40 var head = this.Head; 41 head.Obj.Method(head.Obj); 42 } 43 } 44 interface ISeat3 45 { 46 ISeat3 Child { get; set; } 47 Action<ISeat3> Method { get; set; } 48 }
p.s.为什么第4版是ISeat3而不是ISeat4呢,因为我本不把第1版当作1个版本,因为太原始了,出现第2版后,我就把第1版给删除了。为了写这篇文章才重新去写第1版。于是原本我当作第3版的ISeat3自然地排到了第4版。
具体的"算法"就很简单了:
class Seat3 : ISeat3 { static Seat data = new Seat(); string Name { get; set; } public Seat3(string name) { this.Name = name; } 解耦的版本 Run() { var sql = ComputeLink<Seat3> .New(), m => m.Try()) .Do(), m => m.Try()) .Do(), m => m.Try()) .Do(), m => m.Try()) .Do(), m => m.Try2()) .Do(), m => m.Try2()) .Do(), m => m.Try2()) .Do(), m => m.Try2()) .Do(new Seat3(""), m => m.Print()); sql.Action(); } public Action<ISeat3> Method { get; set; } public ISeat3 Child { get; set; } public void Try() { for (int i = 0; i < 4; i++) { if (data.IsSelected(0, i)) continue; data.Selected(0, i, this.Name); if (this.Child != null) { this.Child.Method(this.Child); } data.UnSelected(0, i); } } public void Try2() { for (int i = 0; i < 5; i++) { if (i == 1) continue; if (data.IsSelected(1, i)) continue; if (data.IsSelected(0, i, this.Name)) continue; data.Selected(1, i, this.Name); if (this.Child != null) { this.Child.Method(this.Child); } data.UnSelected(1, i); } } public void Print() { data.Count++; Console.WriteLine(, data.Count, data.Current); } }
Seat3写起来简单,(Run方法内部)看起来舒服。通过链式写法达到嵌套循环的效果。对,这就是我要的!
它很像linq,所以我直接给变量命名为sql。