Using Factories

Factories are neat, because they allow you to set common properties for objects before you create the object. Plus, all the objects coming out of the factory will have those same settings.

For instance, take the follow factory:


class Item{
    public $price = 0;
}

class ItemFactoryA{

    public $item_price = 15;
    public function get(){
        $item = new Item;
        $item->price = $this->item_price;
    }

}

$iFactory = new ItemFactoryA;
$item = $iFactory->get();

The benefit of this is that every item coming out of FactoryA will have the same price. This is useful if you are going to set the price once, but want to remember it for a while.

In a real life scenario, it could look more like this:


class UTC_TimeClock{
    public function get_time(){
        return time();
    }
}
class TimeZoneClock{
    public function __construct( $utc_clock, $timezone_offset ){
        $this->utc_clock = $utc_clock;
        $this->timezone_offset = $timezone_offset;
    }
    public function get_time(){
        return $this->utc_clock->get_time() + $timezone_offset;
    }
}
class TimeZoneClockFactory(){
    private $utc_clock;
    public function __construct(){
        $this->utc_clock = new UTC_TimeClock;
    }
    public function build( $timezone_offset ){
        return new TimeZoneClock( $this->utc_clock, $timezone_offset );
    }
}

This factory would make sure that all of the Time Zone clocks are running off of the same UTC time clock, but that they all could have different offsets based on what time zone they are representing.

There are many more examples of how this could be useful.

Building a Language: Math Expressions

If someone tells you to list every math operation you know, you might tell them: addition, subtraction, multiplication and division. And you would be right. Each of these are math operations. But to a programming language, these are the least used math operators in existence.

When a programming language goes to evaluate an expression, it must deal with a variety of math operators. Each of these math operators has a common behavior: it takes 2 values (on the left and on the right) and spits out a single value (the result of the operation).

Basic arithmetic operators all fit this description. And if you were to write a programming language, you could very easily model those operators after this behavior. However, it would be a big waste to forget 3 other operators that have this very same behavior: equals, and, and or.

If you think about the ‘==’ operator (or equality operator), it takes the value on the left and compares it to the one on the right. If both are equal, it outputs a single value: boolean true. The same can be said for ‘and’ and ‘or’. Both look at the values on the left and right of them and output true or false, based on the keyword in use.

If you look at SomeLanguage, you’ll see that all of these math operations are implemented using a single MathOperation behavior that allows expressions to be evaluated very easily.