예제 #1
0
 public void Add(CreatureModifier cm)
 {
     if (next != null)
     {
         next.Add(cm);
     }
     else
     {
         next = cm;
     }
 }
        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);
        }
예제 #3
0
        // 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.
        }