static void Main(string[] args) { //BuilderExample(); //FactoryExample(); //FactoryMethodExample(); //FactoryMethodExample2(); //ProtoTypeExample(); //SingletonExample(); //AdapterExample(); //BridgeExample(); //CompositeExample(); //DecoratorExample(); //FacadeExample(); //FlyweightExample(); //ProxyExample();
ChainOfResponsibilityExample();
//Console.WriteLine("Hello World!"); Console.ReadKey(); }
static void ChainOfResponsibilityExample() { Approver larry = new Behavioral_Patterns.ChainOfResponsibility.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); }
main.cs |
using System;
namespace DesignPatterns.Behavioral_Patterns.ChainOfResponsibility { public class Purchase { public Int32 Number { get { return number; } set { number = value; } }
public double Amount { get { return amount; } set { amount = value; } }
public string Purpose { get { return purpose; } set { purpose = value; } }
private Int32 number; private double amount; private string purpose;
public Purchase(int number, double amount, string purpose) { this.number = number; this.amount = amount; this.purpose = purpose; } } }
Purchase.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Behavioral_Patterns.ChainOfResponsibility { public abstract class Approver { protected Approver successor;
public void SetSuccessor(Approver successor) { this.successor = successor; }
public abstract void ProcessRequest(Purchase purchase); } }
Approver.cs |
using System;
namespace DesignPatterns.Behavioral_Patterns.ChainOfResponsibility { public class Director : Approver { public override void ProcessRequest(Purchase purchase) { if(10000.0 > purchase.Amount) { Console.WriteLine("{0} approved request# {1}", this.GetType().Name, purchase.Number); } else if(successor != null) { successor.ProcessRequest(purchase); } } } }
Director.cs |
using System;
namespace DesignPatterns.Behavioral_Patterns.ChainOfResponsibility { public class President : Approver { public override void ProcessRequest(Purchase purchase) { if(100000.0 > purchase.Amount) { Console.WriteLine("{0} approved request# {1}", this.GetType().Name, purchase.Number); } else { Console.WriteLine("Request# {0} requires an executive meeting!", purchase.Number); } } } }
President.cs |
using System;
namespace DesignPatterns.Behavioral_Patterns.ChainOfResponsibility { public class VicePresident : Approver { public override void ProcessRequest(Purchase purchase) { if(25000.0 > purchase.Amount) { Console.WriteLine("{0} approved request# {1}", this.GetType().Name, purchase.Number); } else if(successor != null) { successor.ProcessRequest(purchase); } } } }
VicePresident.cs |
참고 사이트
https://www.dofactory.com/net/chain-of-responsibility-design-pattern
'C# 디자인패턴' 카테고리의 다른 글
14# Proxy패턴 Structural Patterns (0) | 2019.06.19 |
---|---|
13# Flyweight패턴 Structural Patterns (0) | 2019.06.19 |
12# Facade패턴 Structural Patterns (0) | 2019.06.03 |
11# Decorator패턴 Structural Patterns (0) | 2019.06.03 |
10# Composite패턴 Structural Patterns (0) | 2019.06.03 |