Chain Of Responsibility 避免把一个请求的发送者和请求者的接收者进行耦合,这个模式要求多个对象都有机会处理这个请求...

ContractedBlock.gif ExpandedBlockStart.gif 客户代码
1None.gif            Console.WriteLine( root.Responsible.Name );
2None.gif            Console.WriteLine( leafoneleft.Responsible.Name );
3None.gif            Console.WriteLine( leafoneright.Responsible.Name );
4None.gif            Console.WriteLine( leaftwoleft.Responsible.Name );
5None.gif            Console.WriteLine( leaftworight.Responsible.Name );
6None.gif            Console.ReadLine();
ContractedBlock.gif ExpandedBlockStart.gif MachineComponent
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.ChaninOfResponsibility
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public abstract class MachineComponent
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public MachineComponent(MachineComponent parent)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            _parent = parent;            
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        public MachineComponent(MachineComponent parent,Engineer responsible):this(parent)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            _responsible = responsible;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif        public virtual MachineComponent Parent
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            get
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                return _parent;
20ExpandedSubBlockEnd.gif            }

21ExpandedSubBlockEnd.gif        }
private MachineComponent _parent;
22InBlock.gif        public virtual Engineer Responsible
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            get
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                if( _responsible != null)
27ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
28InBlock.gif                    return _responsible;
29ExpandedSubBlockEnd.gif                }

30InBlock.gif                return Parent.Responsible;
31ExpandedSubBlockEnd.gif            }
 
32ExpandedSubBlockEnd.gif        }
private Engineer _responsible;
33ExpandedSubBlockEnd.gif    }

34ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif MachineComposite
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.ChaninOfResponsibility
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class MachineComposite:MachineComponent
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public MachineComposite(MachineComponent parent):base(parent)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
 9InBlock.gif        public MachineComposite(MachineComponent parent,Engineer responsible):base(parent,responsible)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
11ExpandedSubBlockEnd.gif    }

12ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Machine
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.ChaninOfResponsibility
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class Machine:MachineComponent
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public Machine(MachineComponent parent):base(parent)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{ }
 9InBlock.gif        public Machine(MachineComponent parent,Engineer responsible):base(parent,responsible)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{ }
11ExpandedSubBlockEnd.gif    }

12ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif MachineRoot
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.ChaninOfResponsibility
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class MachineRoot:MachineComposite
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public MachineRoot(Engineer responsible):base(null,responsible)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9ExpandedSubBlockEnd.gif        }

10InBlock.gif        public override MachineComponent Parent
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            get
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif                throw new Exception("根节点没有上一级");
15ExpandedSubBlockEnd.gif            }

16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18ExpandedSubBlockEnd.gif    }

19ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Engineer
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.ChaninOfResponsibility
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class Engineer
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public Engineer(string name)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            _name = name;
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        public string Name
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            get
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return _name;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif            set
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                _name = value;
20ExpandedSubBlockEnd.gif            }

21ExpandedSubBlockEnd.gif        }
private string _name = string.Empty; 
22ExpandedSubBlockEnd.gif    }

23ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值