SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with an inheritance design pattern

    (Note: if you start getting deja vu, this is a different question about a different, but similar, program than one I recently asked a question about in another thread)

    Let's see if I can explain this well: I'm writing a program that scrapes sports statistics from multiple online sources. It needs to support multiple sports. These different online sources and different sports also have additional properties that need to be maintained. My first inclination would be to make a different class for each sport that extends an abstract class (Sport), and also create a separate class for each online source I'm scraping from (StatSource). I want to make the design flexible enough so that it's easy to add sports and also easy to add online stat sources. If I add a new Sport, I will need to add new code so that every StatSource knows how to scrape this new Sport. And if I decide to add a new StatSource, I need to write code so it it knows how to scrape every existing Sport.

    Ultimately, I need to get a set of stats back, given a Sport and a StatSource. What is the best design pattern for this relationship? I can't figure out how to do it without some using lots of switch/case statements at some point in the code. These two inheritance structures almost seem to be competing (if I add a new class on one side, I need to write new code in each class on the other side). Is there a pattern that solves this dilemma, such that if I decide to add EITHER a new Sport OR a new StatSource, I can add code in only one place? Let me know if you need more information, and thanks in advance!

  2. #2
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So StatSource is a data source that will provide stats for more than one sport correct?

  3. #3
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sadpanda View Post
    So StatSource is a data source that will provide stats for more than one sport correct?
    Right. Every StatSource will need to know how to provide data for every existing sport, although at some point in the future I might decide to add a new sport, and ALL existing StatSources will then need to know how to support it. Similarly, at some point I might add a new StatSource that will need to know how to support all existing sports.

    I hope that makes sense

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The most maintainable and "future proof" way in my opinion is to break it down / layer it as far down as you can go. Make each component as independent as possible.

    Note since I do not know how you are actually retrieving the data and the difficulty of using the data source's API or the missing API I am going to assume the worst, but please take my opinion with a big grain of salt.

    Example Directory Structure

    /Sports/Basketball.php
    /Source/Basketball/StatSource.php

    You should break down the structure to one data source adapter per unique sport and data source. You can create a "glue" class of your choice then to have it pull the correct data sources for a sport.

    I hope I was clear.

  5. #5
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This also will allow you to have different sources for different sports. A data source that offers stats for basketball may not offer it for baseball. This structure does not force that a data source must have stats for all sports.

    As for relations to which sports has what data sources... This I have to leave to you as I don't know your application well enough, but it could come from the DB, config file..etc.

  6. #6
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sadpanda View Post
    This also will allow you to have different sources for different sports. A data source that offers stats for basketball may not offer it for baseball. This structure does not force that a data source must have stats for all sports.

    As for relations to which sports has what data sources... This I have to leave to you as I don't know your application well enough, but it could come from the DB, config file..etc.
    Thanks for the input. I think this is the best way to go also. My only concern was this: let's say I have three sports (e.g. basketball, baseball, and hockey) and two data providers (e.g. ESPN and Yahoo), but I want to add a third data provider (e.g. FoxSports). The way the application is setup, each data source DOES need to support every sport, so in this case I would need to add three files in three different places:

    /Source/Basketball/FoxSports.php
    /Source/Baseball/FoxSports.php
    /Source/Hockey/FoxSports.php

    Since the FoxSports provider might have other properties I want to keep track of that aren't necessarily sport-dependent (e.g. a generic URL for this website like: http://msn.foxsports.com), I would probably want a 4th file somewhere (maybe /Sources/FoxSports.php?) that each of these 3 new files would reference.

    So to add a single provider, I would need to add at least 4 new files, which isn't ideal, but I think it's the best I can do given the program's constraints.

    Let me know if I misunderstood your design though, and thanks for the help!

  7. #7
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your data sources have good api's, the adapters should be very short and simple. In addition if the data source provides multiple sports the common functionality are already in the abstract source class. By structuring the sources individually it makes it easier to test. Your adapters now have a single purpose.

    Since the FoxSports provider might have other properties I want to keep track of that aren't necessarily sport-dependent
    I would plan out what you need to track. This smells a bit.

  8. #8
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sadpanda View Post
    If your data sources have good api's, the adapters should be very short and simple.
    They don't ...it's going to be a lot of painful regular expressions to get the data I need.

    Quote Originally Posted by sadpanda View Post
    In addition if the data source provides multiple sports the common functionality are already in the abstract source class. By structuring the sources individually it makes it easier to test. Your adapters now have a single purpose.

    I would plan out what you need to track. This smells a bit.
    Thanks again for the help! Can you elaborate a little bit on these two points? I'm a little confused now about how the data source files should be structured. Also, in regards to the "potential smell" - where should I store a Source property (like a URL) that isn't depended on the sport? I wouldn't want to put it in all three sub classes, right?

  9. #9
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, in regards to the "potential smell" - where should I store a Source property (like a URL) that isn't depended on the sport?
    You need to define what your purpose of the URL is. What data do you need, goals..etc. I labeled as smell as I didn't know what you wanted from it.

    I'm a little confused now about how the data source files should be structured.
    It is the same structure that I proposed earlier. You need to adjust these to your own needs.

    Another example:

    /Sport/Basketball/StatSource.php
    /Sport/Basketball/Fox.php
    /Sport/Basketball/Yahoo.php
    /Sport/Basketball/Google.php

    --Could be abstract classes
    /Source/StatSource/Abstract.php
    /Source/Fox/Abstract.php
    /Source/Yahoo/Abstract.php
    ..etc

  10. #10
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The idea is to create yourself a flexible structure.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •