<div class="gmail_quote">On Thu, May 7, 2009 at 2:54 PM, Earl Miles <span dir="ltr"><<a href="mailto:merlin@logrus.com">merlin@logrus.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div class="im">Earnie Boyd wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Quoting James Gilliland <<a href="mailto:neclimdul@gmail.com" target="_blank">neclimdul@gmail.com</a>>:<br>
<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote"><br>Drupal Law 0: If you need to do something, make an API for it first.<br><br></blockquote><br>print t('Hello World');<br>
<br>Oh wait I should create an API to do it first.<br><br>function print_hello_world() {<br> print t('Hello World');<br>}<br></blockquote><br></div>That's not an API, that's abstraction. Kind of. It doesn't follow any of the rules, which has to do with flexibility and reusability. So it's bad abstraction. :P<br>
</blockquote></div>
<div><br>I personally like designing at the interface level. It's so much neater... then you can start thinking about the hairy details of abstract and concrete classes and their guts.</div>
<div> </div>
<div>Admittedly the normal Drupal model is implement an API that provides and abstration to select the properly implementation of an API's beckend interface... </div>
<div> </div>
<div>Now if you had....</div>
<div> </div>
<div>drupal_print_string("Hello World");</div>
<div> </div>
<div>function drupal_print_string($string) {</div>
<div> $function = variable_get('drupal_print_implementation', 'default') .'_print_string';</div>
<div> return $function($string);</div>
<div>}</div>
<div> </div>
<div>function default_print_string($string) {</div>
<div> print $string;</div>
<div>}</div>
<div> </div>
<div>These day I prefer stuff more along the lines of ....</div>
<div> </div>
<div>intrerface iPrinter {</div>
<div> bool print(string str);</div>
<div>}</div>
<div> </div>
<div>class defaultPrinter implements iPrinter {</div>
<div> function print($str) {</div>
<div> print($str);</div>
<div> }</div>
<div>}</div>
<div> </div>
<div>class printerFactory {</div>
<div> function load($implementation) {</div>
<div> if (in_array('iPrinter', class_implements($implementation))) {</div>
<div> return new $implementation(); </div>
<div> }</div>
<div> // maybe throw an exception....</div>
<div> }</div>
<div>}</div>
<div> </div>
<div>$pf = new printerFactory();</div>
<div>$printer = $pf->load(variable_get('default_printer_implementation', 'defaultPrinter'));</div>
<div>$printer->print('Hello World');</div>
<div> </div>
<div>I feel its a little more work to set up the interface and factory, but it's so much easier to expand the implementations, especially when you only need to overload one or two methods...</div>