News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Chris Hartjes' Blog:
Have a 'Strategy'
May 16, 2012 @ 10:42:29

In response to this suggestion from Alessandro Nadalin about using the "Strategy" design pattern to replace a switchChris Hartjes has this new post sharing his opinion of "the right way" do it it.

Once I realized what he was doing, I realized that the Strategy pattern was applicable in this case…but his chosen example was dumb and one that I wouldn't have used to demonstrate things. As expected, he told me to supply a sample of a better way. I did, telling him that the sample would be better if he didn't mash the logging level together with the message.

Included in the post is sample code, first showing the initial version of the logging class, complete with accompanying tests. Following that, he shows how to refactor it into something using the Strategy pattern, replacing the logging type switch statement with protected methods for each logging message type (critical, notice, etc).

0 comments voice your opinion now!
strategy designpattern refactor logging unittest


blog comments powered by Disqus

Similar Posts

Leonid Mamchenkov's Blog: Unit tests with CakePHP

KillerPHP.com: What is Refactoring PHP Code?

Mike Naberezny's Blog: Wrapping PHP Functions for Testability

Padraic Brady's Blog: Mocks, Stubs, And SimpleTest Wins

Lorna Mitchell's Blog: PHPUnit with Zend_Controller_Action_Helper


Community Events

Don't see your event here?
Let us know!


performance configure series podcast release conference example introduction framework symfony2 install voicesoftheelephpant application interview laravel library api php7 opinion community

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework