Decorator pattern and baking a pizza

I am a self proclaimed master chef. And I love baking pizza. I was going through a video and the presenter mentioned the process of baking pizza as an example of decorator pattern. I had some example PHP “pizza” code lying around and decided to use namespaces and composer to build upon it. Here is the Just an example of decorator pattern. I might push more code to GitHub.

Update: I have been trying to get hang of Java and I did this same example using Java. Actually it was pretty easy to translate this code to Java. I did find and replace all for most of the stuff. Here is the repo.

Method chaining

I love the way CodeIgniter explains method chaining by saying.

Method chaining allows you to simplify your syntax by connecting multiple functions.

As explained above it simply lets you do things like

FileObject->OpenFile('FileName')->AppendLine('Hello World!')->CloseFile()

Its neat. But in PHP, you have to have PHP5.x. This wonderful thing is also available in JavaScript. From jQuery home page

$("p.neat").addClass("ohmy").show("slow");

How to do method chaining?
To have an object chain methods every method in that object must return a reference to itself. Easy. Example?

<?php
class MethodChainingExample{
	public function methodOne(){
		echo __METHOD__." \n";
		return $this;
	}
	public function anotherMethod(){
		echo __METHOD__." \n";
		return $this;
	}
	public function oneMoreMethod(){
		echo __METHOD__." \n";
		return $this;
	}
}
$example =  new MethodChainingExample;
$example->methodOne()->anotherMethod()->oneMoreMethod();

Hello World in CakePHP – 3 steps

  1. Download and set up CakePHP in your webroot. Stop reading further if you cant do it or you are not sure how to do it.
  2. Create a file named “helloworld_controller.php” in folder “cakephp/app/controllers/helloworld_controller.php” and paste following code in the file
    <?php
    class HelloWorldController extends Controller {
    public $name = 'HelloWorld'; //Controller name, we need it.
    public $uses=null; //The example doesn't "use" any model.
    public $autoRender=false; //Do not render "automagically"
    public function index() {//The default action for a CakePHP controller
    echo __METHOD__;//This will print HelloWorld suffixed with some more info ;-)
    }
    }
  3. Point your browser to CakePHP installation on your server for example “http://localhost/cakephp/helloworld/”

Piece of cake… 🙂

Interfaces versus Abstract classes in PHP

Interfaces and abstract classes are similar because neither one can be used directly to instantiate an object. The key difference is that an abstract class can have some fully implemented methods, whereas interfaces just have method declarations. Use abstract classes when you want to maintain the same methods in all your subclasses but have some general functionality that can be shared by the subclasses. Use interfaces when the implementations will differ across most or all the methods in subclasses.

Edited on 12th December @ 13:00 Hrs
More differences are as follows:

  • Interfaces provide a form of multiple inheritance. A class can extend only one other class.
  • Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
  • A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
  • Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.

Similarities:

  • Neither Abstract classes or Interface can be instantiated.

Edited (again :-/ ) on 16th Dec @ 16:40 Hours IST

When to use what?

  • Use Abstract class for creating a bond between similar items like Splendor and Bullet 350 are similar entities, motor bikes, so extend them from abstract class like Bike.
  • Use interface for creating a bond between dissimilar items like Splendor and Civic are different from Bike but both are vehicles, have engine, have mileage, have wheels, consume fuel, and so many common behaviors.
  • Always try to use Abstract class or interface whenever you need to up-cast them to achieve simplicity e.g. accelerate and brake methods of bikes.

(not so) Short and (but still) sweet. 🙂