Which is the correct design pattern for my PHP application?

Posted by user1487141 on Stack Overflow See other posts from Stack Overflow or by user1487141
Published on 2012-06-27T23:55:34Z Indexed on 2012/07/01 15:16 UTC
Read the original article Hit count: 182

Filed under:
|
|
|

I've been struggling to find good way to implement my system which essentially matches the season and episode number of a show from a string, you can see the current working code here: https://github.com/huddy/tvfilename

I'm currently rewriting this library and want a nicer way to implement how the the match happens, currently essentially the way it works is:

There's a folder with classes in it (called handlers), every handler is a class that implements an interface to ensure a method called match(); exists, this match method uses the regex stored in a property of that handler class (of which there are many) to try and match a season and episode.

The class loads all of these handlers by instantiating each one into a array stored in a property, when I want to try and match some strings the method iterates over these objects calling match(); and the first one that returns true is then returned in a result set with the season and episode it matched.

I don't really like this way of doing it, it's kind of hacky to me, and I'm hoping a design pattern can help, my ultimate goal is to do this using best practices and I wondered which one I should use?

The other problems that exist are:

  • More than one handler could match a string, so they have to be in an order to prevent the more greedy ones matching first, not sure if this is solvable as some of the regex patterns have to be greedy, but possibly a score system, something that shows a percentage of how likely the match is correct, i'd have no idea how to actually implement this though.

  • I'm not if instantiating all those handlers is a good way of doing it, speed is important, but using best practices and sticking to design patterns to create good, extensible and maintainable code is my ultimate priority. It's worth noting the handler classes sometimes do other things than just regex matching, they sometimes prep the string to be matched by removing common words etc.

Cheers for any help Billy

© Stack Overflow or respective owner

Related posts about php

Related posts about regex