模式 / 行为模式
image

using System;

Ambulance ambulance = new Ambulance(null);
Police police = new Police(ambulance);
Firefighter firefighter = new Firefighter(police);
firefighter.Help(1);
//printed: call firefighters
firefighter.Help(2);
//printed: call the police
firefighter.Help(3);
//printed: call an ambulance

//Handler
abstract class Rescuer {
    protected Rescuer Next { private getinit; }
    protected int Code { private getinit; }

    //HandleRequest()
    public void Help(int code) {
        if (Code == code) {
            ToHelp();
        }  else {
            Next?.Help(code);
        }
    }

    public abstract void ToHelp(); 
}

//ConcreteHandler
class FirefighterRescuer {
    public Firefighter(Rescuer next) {
        Next = next;
        Code = 1;
    }

    public override void ToHelp() {
        Console.WriteLine("call firefighters");
    }
}

//ConcreteHandler
class PoliceRescuer {
    public Police(Rescuer next) {
        Next = next;
        Code = 2;
    }

    public override void ToHelp() {
        Console.WriteLine("call the police");
    }
}

//ConcreteHandler
class AmbulanceRescuer {
    public Ambulance(Rescuer next) {
        Next = next;
        Code = 3;
    }

    public override void ToHelp() {
        Console.WriteLine("call an ambulance");
    }
}