Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Visual Basic

Design Your Soccer Engine, and Learn How To Apply Design Patterns (Observer, Decorator, Strategy, and Builder Patterns) - Part III and IV

Rate me:
Please Sign up or sign in to vote.
4.87/5 (70 votes)
21 Oct 2009CPOL9 min read 125.8K   607   144  
This article is a continuation of the previous article, and in this article, we will discuss (1) Applying the Strategy pattern to solve design problems related with 'Team' and 'TeamStrategy', and (2) Applying the Decorator pattern to solve design problems related with the 'Player'.
'Let us put it together
Public Class GameEngine

    Public Shared Sub Main()

        '-- Step 1: 
        'Create few players (concrete components)

        'Create few field Players
        Dim owen As New FieldPlayer("Owen")
        Dim beck As New FieldPlayer("Beckham")


        'Create a goal keeper
        Dim khan As New GoalKeeper("Khan")

        '-- Step 2: 
        'Just make them pass the ball 
        '(during a warm up session ;))

        System.Console.WriteLine()
        System.Console.WriteLine(" > Warm up Session... ")

        owen.PassBall()
        beck.PassBall()
        khan.PassBall()

        '-- Step 3: Create and assign the responsibilities
        '(when the match starts)

        System.Console.WriteLine()
        System.Console.WriteLine(" > Match is starting.. ")


        'Set owen as our first forward
        Dim forward1 As New Forward()
        forward1.AssignPlayer(owen)

        'Set Beckham as our midfielder
        Dim midfielder1 As New MidFielder()
        midfielder1.AssignPlayer(beck)

        'Now, use these players to do actions
        'specific to their roles

        'Owen can pass the ball
        forward1.PassBall()
        'And owen can shoot as well
        forward1.ShootGoal()

        'Beckham can pass ball
        midfielder1.PassBall()
        'Beckham can dribble too
        midfielder1.Dribble()

        ' [ Arrange the above operations to some meaningfull sequence, like
        ' "Beckham dribbled and passed the ball to owen and owen shooted the
        ' goal ;) - just for some fun ]"

        '-- Step 4: Now, changing responsibilities
        '(during a substitution)

        'Assume that owen got injured, and we need a new player
        'to play as our forward1

        System.Console.WriteLine()
        System.Console.WriteLine(" > OOps, Owen got injured. " & _
                                        "Jerrard replaced Owen.. ")

        'Create a new player
        Dim jerrard As New FieldPlayer("Jerrard")

        'Ask Jerrard to play in position of owen
        forward1.AssignPlayer(jerrard)
        forward1.ShootGoal()

        '-- Step 5: Adding multiple responsibilities
        '(When a player need to handle multiple roles)

        'We already have Beckham as our midfielder. 
        'Let us ask him to play as an additional forward

        Dim onemoreForward As New Forward()
        onemoreForward.AssignPlayer(beck)

        System.Console.WriteLine()
        System.Console.WriteLine(" > Beckham has multiple responsibilities.. ")

        'Now Beckham can shoot
        onemoreForward.ShootGoal()
        'And use his earlier responsibility to dribble too
        midfielder1.Dribble()

        'According to our design, you can attach the responsibility of
        'a forward to a goal keeper too, but when you actually 
        'play football, remember that it is dangerous ;)

        'Wait for key press
        System.Console.Read()


    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions