Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Positions
            Position playerPosition = new Position();

            playerPosition.X = 5;
            playerPosition.Y = 10;
            playerPosition.Z = 15;
            Position ballPosition = new Position();

            ballPosition.X = 10;
            ballPosition.Y = 20;
            ballPosition.Z = 30;
            Position newBallPosition = new Position();

            newBallPosition.X = 5;
            newBallPosition.Y = 10;
            newBallPosition.Z = 15;
            Position refreePosition = new Position();

            refreePosition.X = 5;
            refreePosition.Y = 15;
            refreePosition.Z = 25;

            //---------------------------------------BALL----------------------

            Football football = new Football();

            football.Ball_Position = ballPosition;
            //-------------------------------------PLAYER-----------------------
            Player player1 = new Player();

            player1.Player_Name     = "Abo Trika";
            player1.Player_Team     = "Al Ahly";
            player1.Player_Position = playerPosition;

            Console.WriteLine("Before Updating : " + player1.ToString());
            football.Attach(player1);
            football.changePosition(newBallPosition);
            Console.WriteLine("After Updating : " + player1.ToString());
            //------------------------------REFREE---------------------------
            Refree refree1 = new Refree();

            refree1.Refree_Name     = "Al7km";
            refree1.Refree_Position = refreePosition;

            Console.WriteLine("Before Updating : " + refree1.ToString());
            football.Attach(refree1);
            football.changePosition(newBallPosition);
            Console.WriteLine("After Updating : " + refree1.ToString());
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //setting position of subsucribers(State)
            Position playerPosition = new Position()
            {
                X = 5, Y = 10, Z = 20
            };
            Position refreePosition = new Position()
            {
                X = 2, Y = 7, Z = 9
            };

            //setting position of Publisher(State)
            Position footballPosition = new Position()
            {
                X = 10, Y = 2, Z = 12
            };

            //publisher object
            Football football = new Football();

            //preparing subscribers objects...
            Refree refree = new Refree(football, refreePosition);
            Player player = new Player(football, playerPosition);

            //subscribing player and refree to the football
            football.AttachObserver(player);
            football.AttachObserver(refree);

            ////changing the state of the publisher
            football.Position = footballPosition;

            ////notify subscribers(player, refree,...)
            football.Notify();


            //deatching refree (unsubscribing)
            football.DetachObserver(refree);

            ///notfiy subscriber(player)=>the only one subscribed
            football.Notify();


            Console.WriteLine("American Football  Publisher \n");

            Ball americanFootball = new AmericanFootball();

            //Old Subucribers
            player = new Player(americanFootball, playerPosition);
            refree = new Refree(americanFootball, refreePosition);

            americanFootball.AttachObserver(player);
            americanFootball.AttachObserver(refree);

            americanFootball.Position = new Position()
            {
                X = 45,
                Y = 78,
                Z = 71
            };

            americanFootball.Notify();
        }