0
\$\begingroup\$

I have a soccer team with 14 players (11 active + 3 substitutes). The team is going on a tour participating in different matches across the world. I have two very different design ideas of how for the classes for such match, and I am wondering if there is a better approach here. The language is Python and the database is Mongo.

My requirements:

  1. At any time during a match, I want to know the current status of a player. (substitutes, active, injured) as well as their penalty cards if they got any. If they are actively playing, I want to know when they started. If they got back to sitting or got injured, I want to know their most recent start time and end time.

  2. I want to be able to retrieve the information about a past match. For any player in a past match, I only need to know their latest status (as described above)

Here is my idea about db collections:

Players collection:
- Player ID
- Player Name
- Player Tshirt Size

Teams collection:
- Team ID
- Team Player IDs []
- Team Name
- Team logo

Matches collection:
- Match ID
- Team ID
- Match Location
- Match Time

Match Participants collection:
- Player ID
- Match ID
- Latest Status
- Latest Start Time
- Latest End Time
- Latest Penalty Card
- Latest Injury Note

As you see, I tried to separate the static information about a player (Players collection) from their dynamic information in a match (Players History collection) but I not sure how to define classes?

Class Player:
- Player ID
- Player Name
- Player Tshirt Size
- Latest Status
- Latest Start time
- Latest End time
- Latest Penalty Card
- Latest Injury Note

Class Match:
- Match ID
- Player IDs []
- Match Location
- Match Start time
- Match Tshirt color

The second option would be creating another class for the dynamic part to match to my db collection:

Class Player:
- Player ID
- Player Name
- Player Tshirt Size

Class Match:
- Match ID
- Player IDs []
- Match Location
- Match Start time
- Match Tshirt color

Class Match Participant:
- Player ID
- Match ID
- Latest Status
- Latest Start time
- Latest End time
- Latest Penalty Card
- Latest Injury Note

I do not know how I should decide which approach is better. I would appreciate if you help me figure out the approaches pros and cons or even just what criteria I need to think about when deciding between the two.

Is there any design patterns that would describe any of these approaches?

\$\endgroup\$

migrated from stackoverflow.com Mar 25 '17 at 7:36

This question came from our site for professional and enthusiast programmers.

1
\$\begingroup\$

The first point of your requirements describe events that belong to the match itself, you can define a base class for that

Event
- matchId
- time

And construct specific classes based on it: MatchStarted, PlayerInjured, PlayerEnters, PlayerLeaves, PenaltyCardShown, if want to avoid that, you can create classes with a type property (less oop). Each class can contains specific properties for each event, like playerId for example.

Now. using a service or a query class, you can query against the event list to get the status of players or the match at any time. With the time field is easy to get the last one. Also you can have methods like latestPenaltyCard in your match class to get the data more at hand

In that way your model classes gets very clean, just holding the static data

For raising and storing the events you can use event sourcing or something like that

For storing in the database, maybe I would use an event class that contains all the columns for every event type (and the "type" field), for facilitate querying

\$\endgroup\$
  • \$\begingroup\$ I do like this solution a lot. But never the less I like to suggest minor improvements, which are: remove the event MatchStarted because it is not motivated by the requirements. Now we are able to add the attribute playerId to the Event class, because all the events we are interested in are associated with a player right? That's it. \$\endgroup\$ – Harmlezz Mar 15 '17 at 12:19
  • \$\begingroup\$ @Harmlezz yes, you can do that, it all depends on the requirements. \$\endgroup\$ – HeCtOr Mar 15 '17 at 15:04
  • \$\begingroup\$ @HeCtOr Why do I need to have an events generated when an action happens? Then I will ended up with so many "event" objects in my db, right? As mentioned in the requirement, for the past or a current match, I only need to know the latest information about each of the players (And I do not know when a match finished, just know the start time of a match) So I only needed to keep one record per match per each player. \$\endgroup\$ – Mahshid Zeinaly Mar 15 '17 at 17:58
  • \$\begingroup\$ And besides, I do not need to generate an event when a match starts, because that information is static (I know in advanced when a match supposed to start). If you think I did not get your approach, do you mind explaining more in detail maybe write the classes and their fields? And I appreciate if you could name a design pattern to support your ideas. Especially I separated the db design from my object oriented design, I so much appreciate if you could do the same. \$\endgroup\$ – Mahshid Zeinaly Mar 15 '17 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy