definition
The responsibility chain model is a model of "handling" events in a hierarchical process, which may be a little vague,
For example: suppose I have a restaurant in which there are three roles of "apprentice, chef and chef". When customers come to the restaurant for dinner, they first hand over the dishes to be cooked to the apprentice. The apprentice can't complete the dishes with complex production process, but can only pass the cooking information to the next level and the chef. The chef can't complete the dishes with special difficulty. When the dishes are too difficult, they can only find the chef to complete them.
When the ability of a character cannot complete an event, it will be passed to the next. This scenario is called the responsibility chain mode when it is implemented in code.
Hard coding implementation
public void Cook(Meal meal){ Console.WriteLine("Customers order"); if(meal.Difficulty <= 3) Console.WriteLine("The apprentice completes the dishes"); else if(meal.Difficulty > 3 && meal.Difficulty <= 5) Console.WriteLine("The cook finished the dishes"); else Console.WriteLine("The chef finished the dishes"); }
The above code can also implement the scenario described above, but there will be several problems
- If a new role is added, it will lead to more confusion and poor reading of IF/ELSE
- Adding new conditional judgment will make IF/ELSE more confused and lead to unclear responsibilities of each role
Use responsibility chain pattern coding
We rewrite the previous scenario using the responsibility chain model and encapsulate the responsibilities in three roles of "apprentice, chef and chef".
Define three roles and assign responsibilities to roles
/// <summary> ///Dealing with human abstraction /// </summary> public abstract class Handler { public Handler NextHandler { get; private set; } public string Name { get; private set; } public Handler(string name, Handler nextHandler = null) { this.Name = name; if (nextHandler != null) this.NextHandler = nextHandler; } public abstract void Cook(Meal meal); } /// <summary> ///Apprentice /// </summary> public class Apprentice : Handler { public Apprentice(string name, Cooker handler) : base(name, handler) { } public override void Cook(Meal meal) { if (meal.Difficulty > 3) this.NextHandler.Cook(meal); else Console.WriteLine($"apprentice{this.Name}make{meal.Name}success"); } } /// <summary> ///Chef /// </summary> public class Cooker : Handler { public Cooker(string name, CookerMaster handler) : base(name, handler) { } public override void Cook(Meal meal) { if (meal.Difficulty > 5) this.NextHandler.Cook(meal); else Console.WriteLine($"cook{this.Name}make{meal.Name}); } } } /// <summary> ///Chef /// </summary> public class CookerMaster : Handler { public CookerMaster(string name) : base(name) { } public override void Cook(Meal meal) { Console.WriteLine($"Head cook{this.Name}It's done{meal.Name}This dish"); } }
Create a responsibility chain by specifying the superior.
CookerMaster master = new CookerMaster("Xiao Ming"); Cooker cooker = new Cooker("Xiao Gang", master); Apprentice apprentice = new Apprentice("Xiao Hong", cooker);
Customers order and start making dishes
var aoZhouLongXia = new Meal("Australian Lobster", 7); apprentice.Cook(aoZhouLongXia);
Advantages and disadvantages
advantage:
- Binding responsibilities and roles together is more in line with object-oriented design
- In the above scenario, the "Apprentice" is always the first to handle the event, and there is no need to find out who should handle the event according to the conditions
- And the scalability is improved, and it is more convenient to join new roles
shortcoming
- When the responsibility chain is too long, it may lead to performance problems