Previous

Recent From Articles

Use Social Instead of Email to Boost Pro...

Use Social Instead of Email to Boost Productivity Posted by on Sep 17, 2012

Recent From General Updates

Let’s Try This Again

Let’s Try This Again Posted by on Aug 20, 2012

Recent From Tutorials

Design Patterns: Observer

Design Patterns: Observer Posted by on Nov 9, 2012
Next

Recent Posts

Design Patterns: Observer

One of my favorite design patterns is the observer.  Like most patterns, this serves to encourage developers to create loosely coupled code.  Properly implemented, the observer allows you work with a single object and inform related processes of changes that may require action to be taken.  One of the places where this can be incredibly useful is in committing models to storage.  Think about it – there’s a good chance you aren’t just saving and retrieving information to a database.  In many of the applications that I’ve dealt with, saving information in the modeling layer means I’ll need to deal with caching, search,  email notifications, statistics, and more.  Trying to cram all of this into the data access layer might look something like this:

As you can imagine, once you replace all of the comments above with actual code, these methods can get pretty heavy.  This is far from the OO ideal of a single purpose for a single class.  Now what happens when you have to save a large series of records?  How could you take a part of this process and make it asynchronous?  The observer pattern comes to the rescue.  For this example, I’ve chosen to implement the Standard PHP Library’s built in observer interfaces.

Now adding additional observers to handle data changes is easy.  We can add a few more classes and do something like this:

 Output:

So now with observers, you can create a workflow for your data specific to the situation.  Add as much or as little as you need to.  Each of the observers can go off and do what they need, such as engaging third party services like Gearman to create asynchronous offline tasks.  What’s more, all of these are much smaller, simpler classes that don’t need to know anything about what is going on elsewhere in the application to perform their given tasks.

read more

Design Patterns: Singleton

The singleton design pattern is probably one of the most basic patterns in use.  Many consider this an anti-pattern because developers tend to over-use it and create code that is really just procedural code in disguise of OO.  Still, this pattern can be useful in cases where the developer would like to reuse a single instance of an object with expensive resources, such as a database connection.

Now in order to use the object, you use the static instance method to retrieve it:

An interesting alternative to the singleton pattern is to use a dependency injection container (DIC).  The DIC works by keeping a registry of instantiated objects and providing them to various parts of the application upon request.  See http://framework.zend.com/manual/2.0/en/modules/zend.di.introduction.html

read more

CentOS Setting Time with NTP Date

From a bash terminal type:

# ntpdate us.pool.ntp.org

read more

Method Chaining in PHP

I saw this come across Stack Overflow today, and wanted to elaborate.  Method chaining is a particularly useful technique in modeling objects where the developer may call a series of methods that typically have no return value.  Let’s use a model Person object as an example:

Now if we wanted to set all of the properties of a newly instantiated Person object, we need to split the calls up like so:

 

But I like to type less.  That $person in there happens over and over again.  Let’s make it go away.  Revise the Person class, adding “return $this;” to the setter methods:

 

Now when we set the same properties as above, we can skip the reference to the object over and over:

 

read more

Use Social Instead of Email to Boost Productivity

I walked into work this morning to find more than 160 new messages waiting for me from just the past 3 days (that includes the weekend).  Add to that, the 300 or so (internally generated) emails sitting in my junk mail folder. The majority of these messages have little or nothing of interest to me.  They don’t effect my responsibilities today in any meaningful way or give me any information that helps me to be better at what I do. However, I have to spend time sifting through each of them in order to find the small handful that actually matter.  The time wasted is significant.  The potential for lost information is even higher.

read more