Introduction to Design Patterns

Created by Juan Manuel Torres / @onema / onema.io

Reviewing Object Oriented Programming

Who knows what OOP is?

Wikipedia: Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods.

Who knows the difference between a class and a object?

Class Structure


class ClassName
{
    // These are variables bound to the object and can
    // only be accessed by the object
    public $classProperty01;

    public function classMethod01()
    {
        // Methods are class specific function.
        // Objects can reference themselves using $this.
    }
}

Magic methods

Set of special methods that are called when certain actions occur with in objects.

Constructors and Destructors are examples

Inheritance

Classes can extend the behaviour of other classes by using the "extends" keyword

class Animal
{
    protected $sound;

    public function setSound($sound)
    {
        $this->$sound = $sound;
    }
}
class Dog extends Animal
{
    protected $trick;

    public function setTrick(TrickClass $trickObject)
    {
        $this->$trick = $trickObject;
    }
}

Overwriting properties and methods

class Animal
{
    protected $sound;

    public function setSound($sound)
    {
        $this->$sound = $sound;
    }
}
class Dog extends Animal
{
    protected $trick;

    public function setSound($sound)
    {
        parent::setSound($sound)
        // assume DoggyTalk extends TrickObject
        $trick = new DoggyTalk($sound); 
        $this->setTrick($trick)
    }
    // . . .
}

Preserving method functionality while overwriting

class Animal
{
    protected $sound;

    public function setSound($sound)
    {
        $this->$sound = $sound;
    }
}
class Dog extends Animal
{
    protected $trick;

    public function setSound($sound)
    {
        parent::setSound($sound)
        // assume DoggyTalk extends TrickObject
        $trick = new DoggyTalk($sound); 
        $this->setTrick($trick)
    }
    // . . .
}

Bad Doggy!

class Dog extends Animal
{
    protected $trick;

    public function setSound($sound, DoggyTalk $trick)
    {
        parent::setSound($sound)
        $this->setTrick($trick)
    }
    // . . .
}

Public, protected and private properties

Polymorphism

Describes a pattern in which classes have different behaviour but share common interface.

Welcome to design patterns

Your problems have already been solved!

Let' assume we all work for an independent car design company

As developers we are in charge of creating the software that will display new 3D models and simulations.

New requirement

As part of improving the software, we should be able to create 4WD vehicles. This is the first of many requirements that will come from management.

"Improved" class design

What is wrong with this?

All cars are not full time 4WD

There are part-time, automatic, shift on the fly 4WD, 2WD and All-Wheel Drive!

We could just overwrite the behaviour of things that change.

Inheritance will not solve this problem, what can we do?

Exercise 1: In groups of 2 discus and improve the current class design.

HINT: use polymorphism

Design principle 1:

Identify what parts of the application changes and separate them from what stays the same.

Remember be DRY

Code repetition may be an indication that you are not doing things right!

Exercise 2:Improve your own design.

HINT: use polymorphism

What is wrong with this design?

Design principle 2:

Program to an interface, not an implementation.

Make classes for each of our behaviors

Design principle 3:

Favor composition over inheritance.

Encapsulate Behaviors


abstract class Automobile
{
    protected $wheelDriveBehavior;
    protected $honkBehavior;

    public function wheelDrive()
    {
        $this->wheelDriveBehavior->wheelDrive();
    }

    public function honk()
    {
        $this->honkBehavior->honk();
    }
}

class Truck extends Automobile
{
    public function __construct(WheelDriveInterface $wheelDrive, HonkInterface $honk)
    {
        //$this->wheelDriveBehavior = new 4WheelDrive();
        //$this->honkBehavior = new LaCucarachaHonk();
        $this->wheelDriveBehavior = $wheelDrive;
        $this->honkBehavior = $honk;
    }

    // other methods ...
}

THE END

BY Juan Manuel Torres / onema.io / @onema / kinojman@gmail.com

Introduction to Design Patterns

By Juan Manuel Torres

Introduction to Design Patterns

  • 1,424