For those who have used macros, you should know that you can use macros to bind multiple skills to one key. For example, if the skill ranked first has a CD, this skill will be skipped and the subsequent skills will be executed. I remember when I used to play DK, when fighting monsters, I just used one button and just kept pressing it. In the doGet and doPost methods in the servlet, we usually send the doGet request to doPost for processing. This is also a chain of responsibility model.
Here, there is a macro that binds the two skills "Ice-Blooded Cold Vein" and "Ice Arrow". The program example is as follows:
package responsibility; /** * DOC 技能接口,要绑定的技能都要实现这个接口 * */ public interface ISkill { public void castSkill(); } package responsibility; import java.util.ArrayList; import java.util.List; /** * DOC 宏类,用来把多个技能绑在一起,实现一键施放 * */ public class Macro { /** * DOC 多个技能绑在一起的集合 */ public List<ISkill> skills = new ArrayList<ISkill>(); /** * * DOC 按照绑定顺序施放技能. */ public void castSkill() { for (int i = 0; i < skills.size(); i++) { skills.get(i).castSkill(); } } /** * DOC 绑定技能. * * @param skill */ public void bindSkill(ISkill skill) { skills.add(skill); } } package responsibility; /** * DOC 寒冰箭技能,无冷却时间 * */ public class IceArrow implements ISkill { @Override public void castSkill() { System.out.println("施放--》寒冰箭"); } } package responsibility; /** * DOC 冰血冷脉技能,冷却时间2分钟 */ public class IceBloodFast implements ISkill { @Override public void castSkill() { // 这里可以用来判断技能是否在冷却当中,这里略去了 System.out.println("施放--》冰血冷脉"); } }
Test class:
package responsibility; public class Main { public static void main(String[] args) { Macro macro = new Macro(); macro.bindSkill(new IceBloodFast()); macro.bindSkill(new IceArrow()); macro.castSkill(); } }
Test result:
正在施放--》冰血冷脉 施放--》寒冰箭
Summary: The chain of responsibility model is mainly used for situations where a request may have multiple objects to be processed.
For more articles related to the introduction of the chain of responsibility model of Java design patterns, please pay attention to the PHP Chinese website!
Related articles:
Detailed introduction to the code for implementing the Mediator pattern in Java