Exemplo n.º 1
0
        static void Main(string[] args)
        {
            TeamContext    teamContext    = new TeamContext();
            AttackStrategy attackStrategy = new AttackStrategy();
            DefendStrategy defendStrategy = new DefendStrategy();

            teamContext.setStartegy(attackStrategy);
            teamContext.PlayGame();
            teamContext.setStartegy(defendStrategy);
            teamContext.PlayGame();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 【策略模式】
        ///
        /// 球队与球队策略(Team and TeamStrategy)在比赛中,终端用户可以改变球队的策略(如从进攻改为防守)
        /// </summary>
        static void Strategy()
        {
            ////'Let us create a team and set its strategy,
            // //'and make the teams play the game

            ////'Create few strategies
            var attack = new AttackStrategy();
            var defend = new DefendStrategy();

            ////'Create our teams
            var france = new Team("France");
            var italy  = new Team("Italy");

            System.Console.WriteLine("Setting the strategies..");

            ////'Now let us set the strategies
            france.SetStrategy(attack);
            italy.SetStrategy(defend);

            // //'Make the teams start the play
            france.PlayGame();
            italy.PlayGame();

            System.Console.WriteLine();
            System.Console.WriteLine("Changing the strategies..");

            // //'Let us change the strategies
            france.SetStrategy(defend);
            italy.SetStrategy(attack);

            // //'Make them play again
            france.PlayGame();
            italy.PlayGame();

            ////'Wait for a key press
            System.Console.Read();
        }