[ create a new paste ] login | about

Link: http://codepad.org/at37vpqW    [ raw code | output | fork ]

arielcr - PHP, pasted on Nov 24:
<?php

interface CoffeeService {

    public function getCost();

    public function getDescription();

}

class SingleCoffee implements CoffeeService{

    public function getCost(){
        return 15;
    }

    public function getDescription(){
        return 'Single Coffee';
    }

}

class Milk implements CoffeeService {
    
    protected $coffeeService;

    function __construct(CoffeeService $coffeeService){
        $this->coffeeService = $coffeeService;
    }

    public function getCost(){
        return 5 + $this->coffeeService->getCost();
    }

    public function getDescription(){
        return $this->coffeeService->getDescription() . ', with milk';
    }

}

class Cinnamon implements CoffeeService {
    
    protected $coffeeService;

    function __construct(CoffeeService $coffeeService){
        $this->coffeeService = $coffeeService;
    }

    public function getCost(){
        return 2 + $this->coffeeService->getCost();
    }

    public function getDescription(){
        return $this->coffeeService->getDescription() . ', with cinammon';
    }

}

$coffeeService = new Cinnamon(new Milk(new SingleCoffee));

echo $coffeeService->getCost();

echo $coffeeService->getDescription();


Output:
1
22Single Coffee, with milk, with cinammon


Create a new paste based on this one


Comments: