static void Main12() { Console.WriteLine("Hello Chain of Responsibility Pattern!"); Approver wjzhang, gyang, jguo, meeting; wjzhang = new Director("Wuji Zhang"); gyang = new VicePresident("Guo Yang"); jguo = new President("Jing Guo"); meeting = new Congress("Board"); wjzhang.SetNextApprover(gyang); gyang.SetNextApprover(jguo); jguo.SetNextApprover(meeting); PurchaseRequest pr1 = new PurchaseRequest(45000, 10001, "Purchase Yi Tian Jian"); PurchaseRequest pr2 = new PurchaseRequest(60000, 10002, "Purchase <<Muxplay>>"); PurchaseRequest pr3 = new PurchaseRequest(160000, 10003, "Purchase <<Diamond Sutra>>"); PurchaseRequest pr4 = new PurchaseRequest(800000, 10004, "Purchase Peach Land"); wjzhang.ProcessRequest(pr1); wjzhang.ProcessRequest(pr2); wjzhang.ProcessRequest(pr3); wjzhang.ProcessRequest(pr4); Console.ReadLine(); }
public static void Execute() { ConsoleExtension.WriteSeparator("Purchase approval example"); Approver manager = new Manager(); Approver director = new Director(); Approver vicePresident = new VicePresident(); Approver president = new President(); manager .RegisterNext(director) .RegisterNext(vicePresident) .RegisterNext(president) .RegisterNext(ExecutiveMeeting.Instance); var lowCostPurchase = new Purchase { Number = 1, Cost = 400, Purpose = "Whiteboard purchase." }; var mediumCostPurchase = new Purchase { Number = 2, Cost = 3000, Purpose = "Laptops purchase." }; var highCostPurchase = new Purchase { Number = 3, Cost = 11500, Purpose = "Car purchase." }; manager.Approve(lowCostPurchase); manager.Approve(mediumCostPurchase); manager.Approve(highCostPurchase); }
static void Main(string[] args) { // Setup Chain of Responsibility Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); // Wait for user Console.ReadKey(); }
private static void ChainOfResponsability() { try { // Setup Chain of Responsibility Approver director = new Director(); Approver vicePresidente = new VicePresident(); Approver presidente = new President(); director.SetSuccessor(vicePresidente); vicePresidente.SetSuccessor(presidente); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); director.ProcessRequest(p); p = new Purchase(2035, 72590.10, "Project X"); director.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); director.ProcessRequest(p); // Wait for user Console.ReadKey(); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 下面以公司采购东西为例子来实现责任链模式。公司规定,采购架构总价在1万之 /// 内,经理级别的人批准即可,总价大于1万小于2万5的则还需要副总进行批准,总 /// 价大于2万5小于10万的需要还需要总经理批准,而大于总价大于10万的则需要组 /// 织一个会议进行讨论。对于这样一个需求,最直观的方法就是设计一个方法,参数 /// 是采购的总价,然后在这个方法内对价格进行调整判断,然后针对不同的条件交 /// 给不同级别的人去处理,这样确实可以解决问题,但这样一来,我们就需要多重 /// if-else语句来进行判断,但当加入一个新的条件范围时,我们又不得不去修改原 /// 来设计的方法来再添加一个条件判断,这样的设计显然违背了“开-闭”原则。 /// 这时候,可以采用责任链模式来解决这样的问题 /// </summary> /// <param name="args"></param> static void Main(string[] args) { //实例化采购的请求 PurchaseRequest approveOfTeamLeader = new PurchaseRequest(4000.00, "Telphone"); PurchaseRequest approveOfVicePresident = new PurchaseRequest(10000.00, "Camera"); PurchaseRequest approveOfPresident = new PurchaseRequest(40000.00, "Second-hand car"); PurchaseRequest approveOfBoardDirectors = new PurchaseRequest(400000.00, "Second-hand car"); //实例化参与审批的管理者 Approver teamLeader = new TeamLeader("LearningHard"); Approver vicePresident = new VicePresident("Tony"); Approver president = new President("BossTom"); //设置责任链 管理者的层次排序 teamLeader.NextApprover = vicePresident; vicePresident.NextApprover = president; //处理请求 Console.WriteLine("小组长能审批的:"); teamLeader.ProcessRequest(approveOfTeamLeader); Console.WriteLine("副总经理能审批的:"); teamLeader.ProcessRequest(approveOfVicePresident); Console.WriteLine("总经理能审批的:"); teamLeader.ProcessRequest(approveOfPresident); Console.WriteLine("董事会开会能审批的:"); teamLeader.ProcessRequest(approveOfBoardDirectors); Console.Read(); }
public void Ex1() { /* * Director can handle purchases below just: 10000 * VicePresident can handle purchases below: 25000 * President can handle purchases below: 100000 * */ Approver larry = new Director(); // har minst makt Approver sam = new VicePresident(); Approver tammy = new President(); var client = new Client(); client.SetupChain(larry, sam, tammy); client.ProcessPurchase(new Purchase(1, 50, "Assets")); client.ProcessPurchase(new Purchase(2, 12000, "Project X")); client.ProcessPurchase(new Purchase(3, 75000, "Project Y")); client.ProcessPurchase(new Purchase(4, 2000000, "Project Z")); CollectionAssert.AreEqual(new[] { "Director approved request 1", "VicePresident approved request 2", "President approved request 3", "No one can handle request :(", }, _logger); }
//LD_COR_000 public static void RunChainOfResponsabilityBehavioralPattern() { /* * Let's apply the pattern to an example. In a bank where the approval route for mortgage applications are * from the bank manager to the director then to the vice president, where the approval limits are: * - Manager – 0 to 100k * - Director – 100k to 250k * - Vice President – anything above 250k * We will pass the request to the manager until the application is processed * * Notice that the request is automatically processed by the correct person without the client code * having to determine who will process the request */ LoanApprover a = new Manager(); LoanApprover b = new ChainOfResponsabilityBehavioralPattern.Director(); LoanApprover c = new VicePresident(); //LD here I setup the hierarchy a.SetNextApprover(b); b.SetNextApprover(c); a.ApproveLoan(new Loan(50000)); //this will be approved by the manager a.ApproveLoan(new Loan(200000)); //this will be approved by the director a.ApproveLoan(new Loan(300000)); //this will be approved by the vice president }
public void SwitchWithCondition() { Employee theEmployee = new VicePresident { Salary = 160, Years = 7 }; //(theEmployee as Manager).NumberOfManaged = 3; (theEmployee as VicePresident).StockShares = 6000; (theEmployee as VicePresident).NumberOfManaged = 200; switch (theEmployee) { // conditional case statement case VicePresident vp when(vp.StockShares < 5000): WriteLine($"Junior vice president with {vp.StockShares} shares"); break; case VicePresident vp when(vp.StockShares >= 5000): WriteLine($"Senior vp with {vp.StockShares} shares"); break; case Manager m: WriteLine($"Manager with {m.NumberOfManaged} reporting."); break; case Employee e: WriteLine($"Employee with salary:{e.Salary}"); break; } }
/// <summary> /// 测试初始情况. /// </summary> private static void Test1() { Approver wjzhang, gyang, jguo, meeting; wjzhang = new Director("张无忌"); gyang = new VicePresident("杨过"); jguo = new President("郭靖"); meeting = new Congress("董事会"); //创建职责链 wjzhang.Successor = gyang; gyang.Successor = jguo; jguo.Successor = meeting; //创建采购单 PurchaseRequest pr1 = new PurchaseRequest(45000, 10001, "购买倚天剑"); wjzhang.processRequest(pr1); PurchaseRequest pr2 = new PurchaseRequest(60000, 10002, "购买《葵花宝典》"); wjzhang.processRequest(pr2); PurchaseRequest pr3 = new PurchaseRequest(160000, 10003, "购买《金刚经》"); wjzhang.processRequest(pr3); PurchaseRequest pr4 = new PurchaseRequest(800000, 10004, "购买桃花岛"); wjzhang.processRequest(pr4); }
private void BuildSalesChain() { President president = new President(null); VicePresident vicePresident = new VicePresident(president); Director director = new Director(vicePresident); Manager manager = new Manager(director); Sales sales = new Sales(manager); this.salesChain = sales; }
public void Calc() { PurchaseRequest request = new PurchaseRequest("iphone", 8000); Approver manager = new Manager("tom"); Approver vre = new VicePresident("lyn"); Approver pre = new President("marin"); manager.NextApprover = vre; vre.NextApprover = pre; manager.ProcessRequest(request); }
static void Main(string[] args) { var director = new Director(); var visaPresident = new VicePresident(); var president = new President(); director.SetSuccessor(visaPresident); visaPresident.SetSuccessor(president); director.ProcessRequest(new Purchase(10000, 55000, "Project X")); }
public static void Main() { Approver teamLead = new TeamLead(); Approver vicePresident = new VicePresident(); Approver president = new President(); teamLead.SetSuccessor(vicePresident); vicePresident.SetSuccessor(president); Purchase purchase = new Purchase(2019, 90000.00); teamLead.ProcessRequest(purchase); }
public void Run() { Approver mrDirector = new Director(); Approver mrVicePresident = new VicePresident(); Approver mrPresident = new President(); mrDirector.SetSuccessor(mrVicePresident); mrVicePresident.SetSuccessor(mrPresident); mrDirector.ProcessRequest(new Purchase(1, 350.00, "Assets")); mrDirector.ProcessRequest(new Purchase(2, 32590.10, "Project X")); mrDirector.ProcessRequest(new Purchase(3, 122100.00, "project Y")); }
public static void Run() { PurchaseRequest req = new PurchaseRequest(60000, "Purchase PC for employee. "); Approver approver = new Director("刘主任"); Approver approver1 = new President("张董事"); Approver approver2 = new VicePresident("王副董"); Approver approver3 = new Congress("董事会"); approver.SetSuccessor(approver2); approver1.SetSuccessor(approver3); approver2.SetSuccessor(approver1); approver.HandleRequest(req); }
public void TestChainOfResponsibility() { Approver vasya = new President(null); Approver petya = new VicePresident(vasya); Approver kirill = new Director(petya); var purchase = new Purchase(2034, 350.00, "Project X"); kirill.ProcessRequest(purchase); purchase = new Purchase(2035, 32590.10, "Project Y"); kirill.ProcessRequest(purchase); purchase = new Purchase(2036, 122100.00, "Project Z"); kirill.ProcessRequest(purchase); }
public static void Test() { Director Larry = new Director("Larry"); VicePresident Sam = new VicePresident("Sam"); President Tammy = new President("Tammy"); Larry.SetReporter(Sam); Sam.SetReporter(Tammy); PurchaseRequest reqx = new PurchaseRequest(2034, 350.00, "Project X"); PurchaseRequest reqy = new PurchaseRequest(2035, 32590.10, "Project Y"); PurchaseRequest reqz = new PurchaseRequest(2036, 122100.00, "Project Z"); Larry.ProcessRequest(reqx); Larry.ProcessRequest(reqy); Larry.ProcessRequest(reqz); }
static void Main() { Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); }
static void Main(string[] args) { Project projectX = new Project("Project X", 24000); Project projectY = new Project("Project Y", 48000); Project projectZ = new Project("Project Z", 79000); Approver director = new Director(); Approver vp = new VicePresident(); Approver pres = new President(); director.SetSuccessor(vp); vp.SetSuccessor(pres); director.ApproveProject(projectX); // Will be approved by the director director.ApproveProject(projectY); // Will be approved by the VP director.ApproveProject(projectZ); // Will be approved by the President }
static void ChainInvoke() { PurchaseRequest requestTelphone = new PurchaseRequest("Telphone", 8000.0); PurchaseRequest requestSoftware = new PurchaseRequest("Ms System", 60000.0); PurchaseRequest requestComputers = new PurchaseRequest("Computers", 80000.0); Approver manager = new Manager("XiaoJun"); Approver vp = new VicePresident("XiaoHong"); Approver pre = new President("XiaoMing"); manager.NextApprover = vp; vp.NextApprover = pre; manager.ProcessRequest(requestTelphone); manager.ProcessRequest(requestSoftware); manager.ProcessRequest(requestComputers); }
public static void Main() { Approver teamLead = new TeamLead(); Approver vp = new VicePresident(); Approver ceo = new President(); teamLead.SetSuccessor(vp); vp.SetSuccessor(ceo); var purchase = new Purchase(2034, 350.00); teamLead.ProcessRequest(purchase); purchase = new Purchase(2035, 32590.10); teamLead.ProcessRequest(purchase); purchase = new Purchase(2036, 122100.00); teamLead.ProcessRequest(purchase); }
private static void Main() { var larry = new Director(); var sam = new VicePresident(); var tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); var purchase = new Purchase(1231, 350.0, "Assets"); larry.ProcessRequest(purchase); purchase = new Purchase(321, 12313.123, "Project X"); larry.ProcessRequest(purchase); purchase = new Purchase(123123, 123123123.123, "Project Y"); larry.ProcessRequest(purchase); }
internal static void Main() { Approver teamLead = new TeamLead(); Approver vicePresident = new VicePresident(); Approver president = new President(); teamLead.SetSuccessor(vicePresident); vicePresident.SetSuccessor(president); var purchase = new Purchase(2034, 350.00); teamLead.ProcessRequest(purchase); purchase = new Purchase(2035, 32590.10); teamLead.ProcessRequest(purchase); purchase = new Purchase(2036, 122100.00); teamLead.ProcessRequest(purchase); }
static void Main(string[] args) { Approver andrei = new Director(); Approver gelu = new VicePresident(); Approver valentin = new President(); andrei.SetSuccessor(gelu); gelu.SetSuccessor(valentin); var p = new Purchase(2034, 350.00, "Assets"); andrei.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); andrei.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); andrei.ProcessRequest(p); }
public static void Main() { Approver ronny = new Director(); Approver bobby = new VicePresident(); Approver ricky = new President(); ronny.SetSuccessor(bobby); bobby.SetSuccessor(ricky); Purchase p = new Purchase(8884, 350.00, "Assets"); ronny.ProcessRequest(p); p = new Purchase(5675, 33390.10, "Project Poison"); ronny.ProcessRequest(p); p = new Purchase(5676, 144400.00, "Project BBD"); ronny.ProcessRequest(p); }
public static void ChainOfResponsibility() { Approver vicePresident = new VicePresident(); Approver president = new President(); Approver director = new Director(); director.SetSuccessor(vicePresident); vicePresident.SetSuccessor(president); var order1 = new Purchase(2034, 350.0, "Assets"); director.ProcessRequest(order1); var order2 = new Purchase(2035, 35000.0, "Assets bigger amount."); director.ProcessRequest(order2); var order3 = new Purchase(2036, 350000.0, "Assets the biggest amount."); director.ProcessRequest(order3); }
static void Main(string[] args) { PurchaseRequest requestTelphone = new PurchaseRequest(4000.0, "Telphone"); PurchaseRequest requestSoftware = new PurchaseRequest(10000.0, "Visual Studio"); PurchaseRequest requestComputers = new PurchaseRequest(40000.0, "Computers"); Approver manager = new Manager("LearningHard"); Approver Vp = new VicePresident("Tony"); Approver Pre = new President("BossTom"); // 设置责任链 manager.NextApprover = Vp; Vp.NextApprover = Pre; // 处理请求 manager.ProcessRequest(requestTelphone); manager.ProcessRequest(requestSoftware); manager.ProcessRequest(requestComputers); Console.ReadLine(); }
static void Main() { //The chain of responsibility Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccesor(sam); sam.SetSuccesor(tammy); //Generate and process purchase requests Purchase p = new Purchase(2034, 350.5, "Assets"); larry.ProcessRequest(p); p = new new Purchase(2034, 2343.4, "Project X"); larry.ProcessRequest(p); Console.ReadKey(); }
public void Run(IUnityContainer Container) { // Setup Chain of Responsibility Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); }
private static void ChainOfResponsibilityDemo() { Approver larry = new DesignPatterns.ChainOfResponsibility.Director(); Approver sam = new VicePresident(); Approver tammy = new DesignPatterns.ChainOfResponsibility.President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); }
public static void ChainOfRespRealWorld() { // Setup Chain of Responsibility Approver larry = new Boss(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); }
static void Main(string[] args) { // Setup Chain of Responsibility Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); Approver July = new Chairperson(); larry.SetSuccessor(sam); sam.SetSuccessor(July); July.SetSuccessor(tammy); // Generate and process purchase requests Purchase purchase1 = new Purchase(2034, 5000.00, "Assets"); larry.ProcessRequest(purchase1); Purchase purchase2 = new Purchase(2035, 20000.00, "Keyboard"); larry.ProcessRequest(purchase2); Purchase purchase3 = new Purchase(2036, 30000.00, "Mouse"); larry.ProcessRequest(purchase3); Purchase purchase4 = new Purchase(2037, 90000.00, "CPU"); larry.ProcessRequest(purchase4); Purchase purchase5 = new Purchase(2038, 600000.00, "GPU"); larry.ProcessRequest(purchase5); // Wait for user Console.ReadKey(); }
public static void Main() { Approver teamLead = new TeamLead(); Approver vicePresident = new VicePresident(); Approver president = new President(); teamLead.SetNext(vicePresident); vicePresident.SetNext(president); var purchase = new Purchase(350, 1); teamLead.ProcessRequest(purchase); purchase = new Purchase(24000, 2); teamLead.ProcessRequest(purchase); purchase = new Purchase(32590, 3); teamLead.ProcessRequest(purchase); purchase = new Purchase(100001, 4); teamLead.ProcessRequest(purchase); }
// Entry point into console application. static void Main() { // Setup Chain of Responsibility (bottom-up) Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 92100.00, "Project Y"); larry.ProcessRequest(p); p = new Purchase(2037, 122100.00, "Project Y"); larry.ProcessRequest(p); // Wait for user Console.ReadKey(); }
public static void Main( string[] args ) { // Setup Chain of Responsibility Director Larry = new Director( "Larry" ); VicePresident Sam = new VicePresident( "Sam" ); President Tammy = new President( "Tammy" ); Larry.SetSuccessor( Sam ); Sam.SetSuccessor( Tammy ); // Generate and process different requests PurchaseRequest rs = new PurchaseRequest( 2034, 350.00, "Supplies" ); Larry.ProcessRequest( rs ); PurchaseRequest rx = new PurchaseRequest( 2035, 32590.10, "Project X" ); Larry.ProcessRequest( rx ); PurchaseRequest ry = new PurchaseRequest( 2036, 122100.00, "Project Y" ); Larry.ProcessRequest( ry ); }