示例#1
0
        private static void StrategyDesignPattern()
        {
            var queen = new Queen();
            var king = new King();
            var knight = new Knight();
            var troll = new Troll();

            var knife = new KnifeBehaviour();
            var bowAndArrow = new BowandArrowBehaviour();
            var sword = new SwordBehaviour();
            var axe = new AxeBehaviour();

            Console.WriteLine("Strategy Pattern Example:");
            Console.WriteLine("-------------------------------------------------------------------");

            /////QUEEN
            Console.WriteLine("Character selected: Queen");
            queen.fight();
            queen.setWeapon(knife);
            queen.fight();

            //KING
            Console.WriteLine("Character selected: King");
            king.fight();
            king.setWeapon(bowAndArrow);
            king.fight();

            //KNIGHT
            Console.WriteLine("Character selected: Knight");
            knight.fight();
            knight.setWeapon(bowAndArrow);
            knight.fight();

            //TROLL
            Console.WriteLine("Character selected: Troll");
            troll.fight();
            troll.setWeapon(knife);
            troll.fight();

            Console.WriteLine("-------------------------------------------------------------------");
        }
        static void Main(string[] args)
        {
            Character aKing = new King();
            aKing.whoAreYou();
            aKing.fight();

            Character aQueen = new Queen();
            aQueen.whoAreYou();
            aQueen.fight();

            Character anArcher = new Archer();
            anArcher.whoAreYou();
            anArcher.fight();

            Character aKnight = new Knight();
            aKnight.whoAreYou();
            aKnight.fight();

            aKnight.setWeapon(new KnifeBehavior());
            aKnight.fight();
        }