Fork me on Github s

Posts Tagged design patterns

What are the downsides to using dependency injection?

I recently came across an interesting question on stack overflow with some interesting reponses.  I like this post for three reasons. First, I am a big fan of dependency injection, it forces you to decouple your code, create cohesive interfaces, and should result in testable classes. Second, the author took the approach I usually do when trying to evaluate a technique or technology; suspend personal feelings and try to find some compelling arguments against it. Third, it proved that it is very difficult to come up with a compelling argument against dependency injection.

What are the downsides to using dependency injection?

Tags: , , , , ,

No Comments

Design Patterns for Everyday Living: Front Controller

The front controller pattern has saved me a ton of time in my recycling efforts.  I started recycling about 2 years ago.  My first procedure was unsatisfactory.  I bought two trash cans, and would put all the trash we identified as recyclable into them.  Once they were full I would take them to the recycling center and put them in the appropriate containers.  The sorting procedure was executed at the facility and everything was sorted at one time.  This was both a time consuming and tedious procedure (kinda gross too).

Enter the front controller pattern…

I decided to buy separate containers and sort trash on the fly.  By moving the sorting procedure to the ‘front’ of the process, I was able to save time and effort.  Now I spend about 5 minutes at the facility and don’t have to handle any stinky trash.  From time to time I see a person who is sorting at the facility.  If only they knew how easy it was to implement the front controller.

Design Patterns for Everyday Living is a regular feature that describes how we can apply software design principles to our daily life.  Saving time, increasing productivity, and gaining a better understanding of their use in the software world.

View more Design Patterns for Everyday Living

Tags: ,

No Comments

Gracefully degrading AJAX calls

What happens if grandma fires up her Packard Bell and attempts to buy some medicinal ointment from your site in IE 5?  Or, what if one of your 3rd party advertisers (likely story) causes a javascript error?  Will your page still work?  Have you ever thought of what happens when javascript is not running on your fancy AJAX pages?

Not many of us do.  But, it is still important to think about what will happen if javascript is not running in the user’s browser.  Around 5% of user’s have javascript completely turned off.  Still other’s may have security settings which disallow ajax calls or obsolete browser’s which are not supported.  Degrading your javascript gracefully can be tedious and time consuming.   However, if you follow the pattern that I present it is hardly an inconvenience at all.

Consider the following code:

   
   

Pretty simple.  We can see that when the link is clicked an ajax call is made.  The click event returns false, preventing the browser from navigating to the href location.  If Javascript is not running the browser will just navigate to the ‘/book/details’ page.

The next part is where most people might get lost.  But, if you are using a modern MVC framework (why wouldn’t you be?), then you should be fine.  Or, at the very least, you should be implementing the view decorator pattern in some form.

Basically, your controller (or page) should be able to detect whether or not the request was made via AJAX, the XMLHttpRequest object.  Most javascript libraries make this easy by adding a special request header, ‘X-Requested-With’.  If your library does not do this, you should be able to manually add the header.  By checking this header, you can easily determine whether it is AJAX you are dealing with.

In PHP:

     
     function isAjax() {
       return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
     }
     
     $book = Book->find($id);
     
     if( !isAjax() ) {
       include('includes/header.inc');
     }
    
   echo "
   

${book[title]

${book[description]}

"; if( !isAjax() ) { include('includes/footer.inc'); }

That is pretty straightforward, but the great thing is that most MVC frameworks will do it automatically!  But, what if we want to automatically create our links.  This is easy with the jquery library.  We will create a method that will run when the page loads, and add automatically add the functionality to our links (designated with the ajaxme class).

   
   
   

Keep in mind there are many situations where it is not possible to gracefully degrade an application.  In these cases a <noscript> tag should be used to warn the user that they are missing out on additional functionality.

Tags: , , , ,

No Comments