public virtual void Handle() { if (next != null) { next.Handle(); } }
private static void Test1() { var creature = new Creature(); var cm = new CreatureModifier(creature); cm.Add(new AttackModifier(creature)); cm.Handle(); Console.WriteLine("Hello World!"); }
static void Main(string[] args) { var goblin = new Creature("goblin", 2, 2); Console.WriteLine(goblin); var root = new CreatureModifier(goblin); Console.WriteLine("Let's double the goblin's attack"); root.Add(new DoubleAttackModifier(goblin)); root.Add(new IncreasedDefenseModifier(goblin)); root.Handle(); Console.WriteLine(goblin); }
public static void MethodChainRun() { var goblin = new Creature("Goblin", 1, 1); WriteLine(goblin); var root = new CreatureModifier(goblin); root.Add(new DoubleAttackModifier(goblin)); root.Add(new DoubleAttackModifier(goblin)); root.Add(new IncreaseDefenseModifier(goblin)); root.Handle(); WriteLine(goblin); }
static void Main(string[] args) { var goblin = new Creature("Goblin", 2, 2); Console.WriteLine(goblin); var root = new CreatureModifier(goblin); root.Add(new NoBonusesModifier(goblin)); Console.WriteLine($"Attempting to double {goblin.name}'s attack!"); root.Add(new DoubleAttackModifier(goblin)); Console.WriteLine("Modifier nullified!"); root.Handle(); // checks for modifiers Console.WriteLine(goblin); }
public static void Main(string[] args) { var goblin = new Creature("Goblin", 2, 2); var root = new CreatureModifier(goblin); WriteLine(goblin); WriteLine($"Let's Double goblin's attack!!!"); root.Add(new DoubleAttackModifier(goblin)); root.Add(new NoBonusesModifier(goblin)); WriteLine($"Let's Increase goblins defense!!!"); root.Add(new IncreasedDefenseModifier(goblin)); root.Handle(); WriteLine(goblin); }
static void Main() { var goblin = new Creature("Goblin", 2, 2); Console.WriteLine(goblin); var root = new CreatureModifier(goblin); root.Add(new NoBonusesModifier(goblin)); Console.WriteLine("Let's double goblin's attack..."); root.Add(new DoubleAttackModifier(goblin)); Console.WriteLine("Let's increase goblin's defense"); root.Add(new IncreaseDefenseModifier(goblin)); // eventually... root.Handle(); Console.WriteLine(goblin); }
// Modification of base object private static void MethodChainExample() { var goblin = new Creature("Goblin", 2, 2); Console.WriteLine(goblin); var root = new CreatureModifier(goblin); Console.WriteLine("Let's double the goblins attack"); root.Add(new DoubleAttachModifier(goblin)); Console.WriteLine("Let's block further additional goblin modifiers"); root.Add(new StatsModBlockerModifier(goblin)); Console.WriteLine("Let's increase the goblins defense"); root.Add(new IncreaseDefenseModifier(goblin, 2.4)); root.Handle(); Console.WriteLine(goblin); }
// change to Main to run. public static void none(string[] args) { var goblin = new Creature("Goblin", 2, 2); Console.WriteLine(goblin); var root = new CreatureModifier(goblin); // casts protection spell where it cannot be buffed (silenced) COOL!!!!!!!!!!!!! // prevents walk of linked list aka no chain of responsibility traversal. root.Add(new SilenceModifier(goblin)); Console.WriteLine("Let's double the goblin's attack"); // adding another modifier to the chain of responsibility root.Add(new DoubleAttackModifier(goblin)); Console.WriteLine("Let's increase the goblin's defense"); root.Add(new IncreasedDefenseModifer(goblin)); root.Handle(); Console.WriteLine(goblin); // Downside - method chain implemented this way permanently changes the creature // so that modifiers cannot be removed. We would have to follow the entire linked list // and then recalculate original state of creature. // See CoR_BrokerChain for better implementation. }
//在基类中设置一个虚的要执行的方法 public virtual void Handle() => next?.Handle();