Interfaces: You are doing them wrong.

Code Naked

  • Homepage
  • About
  • Contact

Jan 16: Interfaces: You are doing them wrong.

Normally I don't do any sort of Introduction to Object Oriented Programming posts because to be honest, it's been a long time since I read chapter one and thinking in objects comes naturally to me. Today I happened across the Interfaces section of the PHP manual; the examples and comments on that page were so brutal I thought maybe I should do a public service and write something about how to create a clear object model using a couple of rules of thumb.

In my opinion, one of the most misunderstood OOP mechanisms in the PHP world is the Interface. Unfortunately, the manual seems to support my opinion. In the first example we have the class Template implementing the interface iTemplate. What is an iTemplate? The rest of the examples are even worse: class a implements b. No wonder developers struggle with interfaces when the people that literally wrote the book for them don't seem to get it. Even worse are the comments though, class Rain extends Weather and implements Water? What? Maybe if they mean water as in the sense of watering your lawn. Even so, there has got to be a better name for it than that. The way that interfaces are done in the Zend Framework offers a real-world example of bad interface design. Take a look at the following sentence and let's see if you can make sense of what the involved components are:

Zend_Validate_Date is a Zend_Validate_Abstract and can Zend_Validate_Interface

Now to be completely fair, these class names do get ugly as a result of having to bear the weight of pseudo-namespacing, but if your interface is actually called "Interface" you know you have a problem. Let's look at it again but this time we will be using PHP 5.3+ (which you should be using because it's awesome) and Zend Framework 2.0 - which may have namespace support:

Date is an Abstract and can Interface

This does not describe what type of an object the Date is. Is it a calendar date? Is it a fruit that can be dried and old people drink to help them go #2 more regularly? Or maybe it is a meeting involving two or more people in order to determine if their physical proximity elicits a chemical response. Who knows? And don't even get me started on "can Interface"! Maybe that's what happens if there is chemistry between the people on the Date? Now if you are an experienced OOP developer, you will know that Abstract is the home of some common functionality that all child classes share and that the Interface defines the contract that any objects implementing it must fulfill. But it doesn't tell you anything about what type of functionality is common to the children of the abstract or what types of behavior must be implemented by objects implementing the interface.

Designing objects can be fairly easy for the most part if you take the stance that your objects are nouns and your interfaces are adjectives. Until you get better at it, try to add the suffix "able" to indicate that objects implementing it are capable of doing something. Try this version of the Zend Framework validation action:

Date is a Validator and is Validatable

You won't always be able to use the "able" rule of thumb, but going through the process of trying to think of one will help you arrive at a better object model. Take a look at a few more examples:

A Circle is a Shape and is Drawable
A TeddyBear is a Toy and is Huggable
A CardDeck is an ArrayObject and is both Countable and Sortable


So hopefully these examples help give you an idea how easy it is to create good code. As always when it comes to designing objects, your best tool is simply reading the objects out loud. The better your design, the less like Yoda you will sound. Of course I couldn't leave this post without throwing down some code, and since I was most appalled by the Weather examples in the manual comments I thought I would do my own weather example. The real key here is to note that not only do weather conditions such as the amount of clouds and the UV index rating affect what the actual temperature feels like, so can what you wear. In other words, weather can use objects of completely different types to do work because it knows that those objects are capable of doing a particular thing.
Don't hate me because it's beautiful ;-)
Posted by Matthew Purdon in PHP Comment: (1) Trackbacks: (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

#1 - Brian DeShong said:
2010-01-17 07:06 - (Reply)

Great post! So true on the poorly-named interfaces and classes, too. And yes, the less you sound like Yoda, the better your design is!

Many years ago, I read "The Object-Oriented Thought Process," which is a great book with well-named, practical examples that anyone can relate to (i.e. class "Ford" implements "Car"). I'd highly recommend it to anyone starting out in OOP design. It's also a great book to re-read every year or two.

http://www.amazon.com/Object-Oriented-Thought-Process-3rd/dp/0672330164/ref=sr_1_1?ie=UTF8&s=books&qid=1263740608&sr=8-1


Add Comment

Standard emoticons like :-) and ;-) are converted to images.
 
 

Subscribe

Archives

  • March 2010 (0)
  • February 2010 (1)
  • January 2010 (4)
  • December 2009 (6)
  • Recent...
  • Older...

Categories

  • XML Technology
  • XML Databases
  • XML MySQL
  • XML Software Development
  • XML Being a Contractor
  • XML Client Side
  • XML PHP
  • XML State of the Art


All categories

Blog Administration

Open login screen

Recommended Reading

Amazon.com: Refactoring: Improving the Design of Existing Code (9780201485677): Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts: Books
Amazon.com: The Nomadic Developer: Surviving and Thriving in the World of Technology Consulting (9780321606396): Aaron Erickson: Books
Amazon.com: Domain-Driven Design: Tackling Complexity in the Heart of Software (0076092019565): Eric Evans: Books
Amazon.com: Patterns of Enterprise Application Architecture (0076092019909): Martin Fowler: Books

Feedburner

Numeric Feedburner ID Required!




 

Layout by Andreas Viklund | Serendipity template by Carl