I regularly define classes for solving problems for several reasons, I will describe an example of my thinking below. I have no compromise on mixing OO models and procedural styles, which are often more a reflection of your working society than your personal religion. Often this works to have a procedural facade of the class hierarchy, if other proponents expect it. (Please excuse the PHP syntax.)
I develop strategies, and I can follow the general model. Therefore, a possible simulation of your task may include getting something to chew on the submitted URLs. This works if you want to simplify external logic and remove conventions. This shows that I can use DNRY for the gather () method.
// batch process method function MunchPages( $list_of_urls ) { foreach( $list_of_urls as $url ) { $muncher = PageMuncher::MuncherForUrl( $url ); $muncher->gather(); $muncher->process(); } } // factory method encaps strategy selection function MuncherForUrl( $url ) { if( strpos( $url, 'facebook.com' )) return new FacebookPageMuncher( $url ); if( ... ) return new .... ; } // common tasks defined in base PageMuncher class PageMuncher { function gather() { /* use some curl or what */ } function process() {} } class FacebookPageMuncher extends PageMuncher { function process() { /* I do it 'this' way for FB */ } }
I create a set of routines that are ideally hidden and nonetheless accessible. An example of this is a class that defines toolbar methods common to a task. More specific tasks can expand the set of tools for developing your own behavior.
class PageMuncherUtils { static function begin( $html, $context ) {
I want to organize my code in order to convey a broader meaning to the maintainer. Keep in mind that if I have at least one remote service that I interact with, I could dive into different APIs within my interface, and I want to group these procedures on similar topics. Below I show an example of how I like to define a class that defines common constants, a class that defines the basic methods of service, and a more specific class for meteorological warnings, because the warning should know how to update itself, and this is more specific than the weather service itself but also uses WeatherAPI constants.
class WeatherAPI { const URL = 'http://weather.net'; const URI_TOMORROW = '/nextday/'; const URI_YESTERDAY= '/yesterday/'; const API_KEY = '123'; } class WeatherService { function get( $uri ) { } function forecast( $dateurl ) { } function alerts( $dateurl ) { return new WeatherAlert( $this->get( WeatherAPI::URL.$date ."?api=".WeatherAPI::API_KEY )); } } class WeatherAlert { function refresh() {} }
source share