- A soccer game must have a ball
- They are 11 players in each team
- There are two teams
- Two lines men and one referee
- Each team has reserve players
- There is the playground of course
Here is where the first problem comes in .
How do I solve the problem where the referees and players know the current position and get updates of the ball position as its played on the field.
Lets define some real objects needed to solve the problem
- Ball
- Players
- Playground
- Referee
- Team
The SoccerGame object will simply simulate a football game.
Here is an abstract view of the system as I have described above.
I am a visual person so the best way to see the problem is to get a visual of the problem .Here is the way i see it below
The pattern that fits this is the Observer pattern which says that "Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically." In our case all objects are dependent on the ball for a soccer game and the players, referees must be notified and updated as it changes state(moving).
The observer pattern in its raw state is shown below
The next step is to put the soccer problem in the context of the observer pattern and this time there is a position object in three dimensions(x,y,z) to connect to the change of the ball state since it describes the current position on the play ground. Changes of the state of the ball is reflected by the position .It is the state that every observer has be notified and interested about.
The subject is the soccer ball
The concrete object is the actual football that will be kicked
The observers are the players and referees and they implement the IObserver interface.
The ball must have a list of observers
The observers have a pointer to the football object
The positions of all the participants with the ball in the game has to be known.
Now how do we implement this in C# ? Here is where I have to shut my mouth and get a prototype of the game engine for my customer.
Here is the code of the Game Engine only .It brings everything together
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SoccerGame { ///That is it for now and happy programming !/// Simulating part of a soccer game using the Observer pattern /// public class GameEngine { public static void Main() { // Create our ball (the ConcreteSubject) var ball = new FootBall(); // Create few players ( some ConcreteObservers) var messi = new Player(ball, "Messi"); var etofils = new Player(ball, "Etofils"); var Ibrahimmovic = new Player(ball, "Ibrahimovic"); // Create some referees (also ConcreteObservers) var collina = new Referee(ball, "Collina"); var ngala = new Referee(ball, "Ngala"); Console.WriteLine(); // Attach them with the ball ball.AttachObserver(messi); ball.AttachObserver(etofils); ball.AttachObserver(Ibrahimmovic); ball.AttachObserver(collina); ball.AttachObserver(ngala); Console.WriteLine(" After attaching the observers..."); // Update the position of the ball. // At this point, all the observers should be notified ball.SetBallPosition(new Position()); Console.WriteLine(); // Remove some observers ball.DetachObserver(etofils); ball.DetachObserver(collina); Console.WriteLine(" After detaching the refree Collina and Etofils from the ball..."); // Updating the position of ball again // At this point, all the observers should be notified ball.SetBallPosition(new Position(10, 10, 30)); // Press any key to continue....more things happen Console.Read(); } } }
No comments:
Post a Comment