|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Contents
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In this case, we used this pattern because we need to notify all the players, when the position of the ball is changed.
Next, we have to address the specifications related to the team and team strategy. As we discussed earlier, when the game is in progress, the end user can change the strategy of his team (E.g., From Attack to Defend). This clearly means that we need to separate the Team's Strategy from the Team that uses it.
Specific Design Problem: "When the game is in progress, the end user can change the strategy of his team (E.g., From Attack to Defend)"
Problem Generalized: "We need to let the algorithm (TeamStrategy) vary independently from clients (in this case, the Team) that use it."
Then, you can chose the 'Strategy' pattern to address the above design problem.
Now, let us address the design specifications related to the player. From our problem definition, it is clear that we need to assign responsibilities (like forward, defender etc) to each player during run time. At this point, you can think about sub classing (i.e, inheritance) - by creating a player class, and then inheriting classes like Forward, Defender etc from the base class. But the disadvantage is that, when you do sub classing, you cannot separate the responsibility of an object from its implementation.
I.e, In our case, sub classing is not the suitable method, because we need to separate the responsibilities like 'Forward', 'Midfielder', 'Defender' etc from the Player implementation. Because, a player can be a 'Forward' one time, and some other time, the same player can be a 'Midfielder'.
Specific Design Problem: "A player in a team should have additional responsibilities, like Forward, Defender etc, that can be assigned during the runtime."
Problem Generalized: "We need to attach additional responsibilities (like Forward, Midfielder etc) to the object (In this case, the Player) dynamically, with out using sub classing"
Then, you can chose the 'Decorator' pattern to address the above design problem.
If you take a look at the specifications of Ground, we see that a ground's appearance is decided by various sub units like gallery, surface of the ground, audience etc. The appearance of the ground may vary, according to these sub units. Hence, we need to construct the ground in such a way that, the construction of the ground can create different representations of the ground. I.e, a ground in Italy may have different gallery structure and surface when compared to a ground in England. But, the game engine may create both these grounds by calling the same set of functions.
Specific Design Problem: "Each ground constitutes of gallery, ground surface, audience, etc - and each ground has a different appearance."
Problem Generalized: "We need to separate the construction of an object (ground) from its representation (the appearance of the ground) and we need to use the same construction process to create different representations."
Now, you can chose the 'Builder' pattern to address the above design problem.
Part II
Solution Architect: "I asked you to learn about patterns"
Dumb Developer: "Yes, now I can develop a football engine using patterns"
Solution Architect: "Huh? What do you mean? !@@#!"
In this section, we will have a closer look at the observer pattern, and then we will apply the pattern to solve our first design problem. If you can remember, our first design problem was,
The UML class diagram of the observer pattern is shown below.

Fig 2 - Observer Pattern
The participants of the pattern are detailed below.
This class provides an interface for attaching and detaching observers. Subject class also holds a private list of observers. Functions in Subject class are
Update function in the observer, when a change occurs.
This class provides the state of interest to observers. It also sends a notification to all observers, by calling the Notify function in its super class (i.e, in the Subject class). Functions in ConcreteSubject class are
This class defines an updating interface for all observers, to receive update notification from the subject. The Observer class is used as an abstract class to implement concrete observers
This class maintains a reference with the subject, to receive the state of the subject when a notification is received.
ConcreteObserver calls the GetState function of the subject to update the information it have about the subject's state. Now, let us see how this pattern can be adapted to solve our specific problem. This will give you a better idea.

Fig 3 - Solving Our First Design Problem
When we call the SetBallPosition function of the ball to set the new position, it inturn calls the Notify function defined in the Ball class. The Notify function iterates all observers in the list, and invokes the Update function in each of them. When the Update function is invoked, the observers will obtain the new state position of the ball, by calling the GetBallPosition function in the Foot ball class.
The participants are detailed below.
The implementation of Ball class is shown below.
' Subject : The Ball Class
Public Class Ball
'A private list of observers
Private observers As new System.Collections.ArrayList
'Routine to attach an observer
Public Sub AttachObserver(ByVal obj As IObserver)
observers.Add(obj)
End Sub
'Routine to remove an observer
Public Sub DetachObserver(ByVal obj As IObserver)
observers.Remove(obj)
End Sub
'Routine to notify all observers
Public Sub NotifyObservers()
Dim o As IObserver
For Each o In observers
o.Update()
Next
End Sub
End Class ' END CLASS DEFINITION Ball
The implementation of FootBall class is shown below.
' ConcreteSubject : The FootBall Class
Public Class FootBall
Inherits Ball
'State: The position of the ball
Private myPosition As Position
'This function will be called by observers to get current position
Public Function GetBallPosition() As Position
Return myPosition
End Function
'Some external client will call this to set the ball's position
Public Function SetBallPosition(ByVal p As Position)
myPosition = p
'Once the position is updated, we have to notify observers
NotifyObservers()
End Function
'Remarks: This can also be implemented as a get/set property
End Class ' END CLASS DEFINITION FootBall
The implementation of IObserver class is shown below. This class provides interface specifications for creating Concrete Observers.
' Observer: The IObserver Class
'This class is an abstract (MustInherit) class
Public MustInherit Class IObserver
'This method is a mustoverride method
Public MustOverride Sub Update()
End Class ' END CLASS DEFINITION IObserver
The implementation of Player class is shown below. Player is inherited from IObserver class
' ConcreteObserver: The Player Class
'Player inherits from IObserver, and overrides Update method
Public Class Player
Inherits IObserver
'This variable holds the current state(position) of the ball
Private ballPosition As Position
'A variable to store the name of the player
Private myName As String
'This is a pointer to the ball in the system
Private ball As FootBall
'Update() is called from Notify function, in Ball class
Public Overrides Sub Update ()
ballPosition = ball.GetBallPosition()
System.Console.WriteLine("Player {0} say that the ball is at {1},{2},{3} ", _
myName, ballPosition.X, ballPosition.Y, ballPosition.Z)
End Sub
'A constructor which allows creating a reference to a ball
Public Sub New(ByRef b As FootBall, ByVal playerName As String)
ball = b
myName = playerName
End Sub
End Class ' END CLASS DEFINITION Player
The implementation of Referee class is shown below. Referee is also inherited from IObserver class
' ConcreteObserver : The Referee Clas
Public Class Referee
Inherits IObserver
'This variable holds the current state(position) of the ball
Private ballPosition As Position
'This is a pointer to the ball in the system
Private ball As FootBall
'A variable to store the name of the referee
Private myName As String
'Update() is called from Notify function in Ball class
Public Overrides Sub Update()
ballPosition = ball.GetBallPosition()
System.Console.WriteLine("Referee {0} say that the ball is at {1},{2},{3} ", _
myName, ballPosition.X, ballPosition.Y, ballPosition.Z)
End Sub
'A constructor which allows creating a reference to a ball
Public Sub New(ByRef b As FootBall, ByVal refereeName As String)
myName = refereeName
ball = b
End Sub
End Class ' END CLASS DEFINITION Referee
Also, we have a position class, to hold the position of the ball.
'Position: This is a data structure to hold the position of the ball
Public Class Position
Public X As Integer
Public Y As Integer
Public Z As Integer
'This is the constructor
Public Sub New(Optional ByVal x As Integer = 0, _
Optional ByVal y As Integer = 0, _
Optional ByVal z As Integer = 0)
Me.X = x
Me.Y = y
Me.Z = Z
End Sub
End Class ' END CLASS DEFINITION Position
Now, let us create a ball and few observers. We will also attach these observers to the ball, so that they are notified automatically when the position of the ball changes. The code is pretty self explanatory.
'Let us create a ball and few observers
Public Class GameEngine
Public Shared Sub Main()
'Create our ball (i.e, the ConcreteSubject)
Dim ball As New FootBall()
'Create few players (i.e, ConcreteObservers)
Dim Owen As New Player(ball, "Owen")
Dim Ronaldo As New Player(ball, "Ronaldo")
Dim Rivaldo As New Player(ball, "Rivaldo")
'Create few referees (i.e, ConcreteObservers)
Dim Mike As New Referee(ball, "Mike")
Dim John As New Referee(ball, "John")
'Attach the observers with the ball
ball.AttachObserver(Owen)
ball.AttachObserver(Ronaldo)
ball.AttachObserver(Rivaldo)
ball.AttachObserver(Mike)
ball.AttachObserver(John)
System.Console.WriteLine("After attaching the observers...")
'Update the position of the ball.
'At this point, all the observers should be notified automatically
ball.SetBallPosition(New Position())
'Just write a blank line
System.Console.WriteLine()
'Remove some observers
ball.DetachObserver(Owen)
ball.DetachObserver(John)
System.Console.WriteLine("After detaching Owen and John...")
'Updating the position of ball again
'At this point, all the observers should be notified automatically
ball.SetBallPosition(New Position(10, 10, 30))
'Press any key to continue..
System.Console.Read()
End Sub
End Class
After running the project, you'll get the output as

Patterns can be classified
With respect to purpose, patterns are classified to Creational, Structural and Behavioral. For example,
Here is the complete classification diagram.
I hope this article
And finally, if you have strokes in your head (a sign of great programmers
) - I'll recommend an Art Of Living Part I workshop for you (See http://www.artofliving.org/courses.html ). It is an interactive workshop of 18 hours spread over 6 days. As it did for me, I hope that it may help you to find the right balance between your work and life - to improve the clarity of your mind, and to improve the quality of your life. You can get in touch with them here - http://www.artofliving.org/centers/main.htm
To read Second Part Of This Article, Click Here >>
Also, here is an article about Applying Provider Pattern - http://www.codeproject.com/useritems/providerframework.asp
Here are some other popular articles I wrote.. You may refer them to see some patterns in action. :)
If you want to understand VB code better, try reading this article I wrote..
VB.NET Tutorial - Learning VB.NET in a very easy manner.
Also, you can visit my own website at http://amazedsaint.blogspot.com/ for more articles, projects and source code
Also, you may view my tech-bits blog , download my opersource projects , or even have look at my intuitions blog
Thanks a lot :)
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|