Unity implements design pattern - chain of responsibility pattern
Chain of Responsibility Pattern Definition: Decouple the sending and receiving of requests so that multiple receiving objects have the opportunity to process the request. String these receiving objects into a chain and pass the request along the chain until one of the receiving objects in the chain can handle it.
In the chain of responsibility model, multiple processors (that is, the "receiving objects" just defined) process the same request in sequence. A request is first processed by processor A, and then the request is passed to processor B. After processor B is processed, it is passed to processor C, and so on, forming a chain. Each processor in the chain assumes its own processing responsibilities, so it is called the chain of responsibility model.
Here we use the example of manager, vice president, president, purchasing goods in sequence. The
manager can purchase goods. When the quantity purchased is greater than a certain threshold, the manager cannot complete the task and needs to be completed by the superior. When the superior cannot complete the task, It’s time to report the processing request to the highest level.
1.Approver manager’s base class
abstract class Approver
{
protected Approver successor;
public void SetSuccessor(Approver successor)
{
this.successor = successor;
}
public abstract void ProcessRequest(Purchase purchase);
}
2.Director manager
class Director : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 10000.0)
{
Debug.Log(this.GetType().Name+" approved request# "+purchase.Number);
}
else if (successor != null)
{
successor.ProcessRequest(purchase);
}
}
}
3.VicePresident
class VicePresident : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 25000.0)
{
Debug.Log(this.GetType().Name + " approved request# " + purchase.Number);
}
else if (successor != null)
{
successor.ProcessRequest(purchase);
}
}
}
4.President President
class President : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 100000.0)
{
Debug.Log(this.GetType().Name + " approved request# " + purchase.Number);
}
else
{
Debug.Log("Request# "+purchase.Number+ "requires an executive meeting!");
}
}
}
5.Purchase purchased goods
class Purchase
{
private int _number;
private double _amount;
private string _purpose;
// Constructor
public Purchase(int number, double amount, string purpose)
{
this._number = number;
this._amount = amount;
this._purpose = purpose;
}
// Gets or sets purchase number
public int Number
{
get {
return _number; }
set {
_number = value; }
}
// Gets or sets purchase amount
public double Amount
{
get {
return _amount; }
set {
_amount = value; }
}
// Gets or sets purchase purpose
public string Purpose
{
get {
return _purpose; }
set {
_purpose = value; }
}
}
6. Test
public class ChainOfResponsibilityExample1 : MonoBehaviour
{
void Start ( )
{
// 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);
}
}