08. July 2013

Sleep Sort in Javascript

Sleep Sort is a humourous algorithm for sorting. The idea is to output the numeric array elements after a time interval proportional to the value of the array element. So if you had an array [3, 2, 1], 3 could be output three seconds after the sort, 2 two seconds after and 1 one second after. The result is that you’d see 1, 2, 3 three seconds later.

Of course this is a terrible idea, but it’s also a heck of a lot of fun!

Here’s an implementation of the idea in javascript:

function opnode(a) {
    el = document.createElement('div');
    el.innerHTML = a;
    document.body.appendChild(el);
}

var temporalSort = function(ar) {
    for (i = 0; i < ar.length; i++) {
        (
            function(a){
                window.setTimeout(
                    function() {
                        opnode(a);
                    },
                100 * a);
            }
        )(ar[i]);
    }
};

var i, array = [6, 14, 1, 12, 8, 3, 9, 2, 10, 15, 4, 11, 7, 13, 5];

temporalSort(array);

03. July 2013

Simple Observer Pattern in PHP

The observer pattern is a nifty way to decouple objects from one another. Rather than having methods explicitly rely on, create, and access other objects, the other objects can subscribe to an object and enact their own changes as necessary.

Our implementation will have two general groups of objects, subscribers and observers. A subscriber class will hook in to an observer class and make actions when the observer publishes certain messages. The observer class will give objects methods to subscribe and unsubscribe from its messages.

We’ll begin by setting up an interface for our subscribers. Our subscribers could really be anything, so we want to specify some general behaviour that they will have. We don’t want to be restrictive and have an abstract class that they will inherit from because that would restrict functionality of all subscribers and limit the usefulness of this pattern.

interface Subscriber {
    public function EventCall($str);
}

Now we’ll set up an abstract class for Observables. This may be more useful as a trait but we actually do have to implement some functionality here.

abstract class Observable {
    private $_subscribers = array();
    public function Subscribe($o) {
        // give objects an ability to add themselves to the subscribers list.
        if (!in_array($o, $this->_subscribers)) {
            $this->_subscribers[] = $o;
        }
    }
    public function Unsubscribe($o) {
        // give objects an ability to remove themselves from the subscribers list.
        if (in_array($o, $this->_subscribers)) {
            foreach($this->_subscribers as $key => $value) {
                if ($o == $value) {
                    unset($this->_subscribers[$key]);
                }
            }
        }    
    }
    public function Event($event) {
        // when the event occurs, call the corresponding method on the clients.
        foreach($this->_subscribers as $subscriber) {
            $subscriber->EventCall($event);
        }
    }
}

Now that the abstract class and interface are ready, we can make some concrete classes based on these.

class Observer extends Observable {
    public function Talk() {
        echo "I am an observern";
    }
}

class Subscriber1 implements Subscriber{
    public function EventCall($str) {
        echo "Subscriber1 event occured $strn";
    }
}

class Subscriber2 implements Subscriber {
    private $data;
    public function __construct($data) {
        $this->data = $data;
    }
    public function EventCall($str) {
        echo "Subscriber2 event occured $str data is " . $this->data . "n";
    }
}

class Subscriber3 implements Subscriber {
    public function EventCall($str) {
        echo "Subscriber3 event occured $strn";
    }
}

And now let’s make some instances of these:

$observer = new Observer();
$subscriber1 = new Subscriber1();
$subscriber2 = new Subscriber2('one');
$subscriber2too = new Subscriber2('two');
$subscriber3 = new Subscriber3();

$observer->Subscribe($subscriber1);
$observer->Subscribe($subscriber2);
$observer->Subscribe($subscriber2);
$observer->Subscribe($subscriber2too);
$observer->Subscribe($subscriber3);

$observer->Event('wow check out this awesome message that is being passed, bro.');
$observer->Talk();

$observer->Unsubscribe($subscriber2);

$observer->Event('what unheard of madness will happen next?');

And of course the output is:

Subscriber1 event occured wow check out this awesome message that is being passed, bro.

Subscriber2 event occured wow check out this awesome message that is being passed, bro. data is one

Subscriber2 event occured wow check out this awesome message that is being passed, bro. data is two

Subscriber3 event occured wow check out this awesome message that is being passed, bro.

I am an observer

Subscriber1 event occured what unheard of madness will happen next?

Subscriber2 event occured what unheard of madness will happen next? data is two

Subscriber3 event occured what unheard of madness will happen next?

28. June 2013

Using Interfaces to reduce coupling

Consider the following three scenarios:

  1. class Dog {
    ...
    public function Bark($str) {
       echo $str;
    }
    ... }
    
    

class AnimalCommunication { … public function DogBark(Dog $dog, $str) { $dog->Bark($str); } … }

  1. class Dog {
    ...
    public function Bark($str) {
       echo $str;
    }
    ... }
    
    
    

class AnimalCommunication { … public function AnimalCommunicate($str) { echo $str; } … }

  1. Interface IAnimal {
    public function Speak($str); }
    
    

Class Dog implements IAnimal { … public function Speak($str) { $this->Bark($str); } …. } class AnimalCommunication { … public function AnimalCommunicate(IAnimal $animal, $str) { $animal->speak($str); } … }

Note the following about these examples:

  1. The first is tightly coupled to the dog class, and hence is not very extensible.
  2. The second is coupled to echo. It does not allow different “animals” to output their text differently. What if we later added a TelepathicGecko class, which didn’t echo out to speak, but rather published to some ESP API somewhere? Clearly the coupling here is not ideal either.
  3. The third is best. By programming to an interface, we reduce coupling to the minimum necessary amount.