Skip to content

Instantly share code, notes, and snippets.

@cirpo
Created August 10, 2013 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cirpo/6201231 to your computer and use it in GitHub Desktop.
Save cirpo/6201231 to your computer and use it in GitHub Desktop.
<?php
class Cart {
private $status;
public function __construct() {
$this->status = new EmptyCartStatus($this);
}
public function setStatus(CartStatus $cartStatus) {
$this->status = $cartStatus;
}
public function checkout() {
$this->status->checkout();
}
public function whatever() {
$this->status->whatever();
}
}
<?php
class CloseCartStatus implements CartStatusInterface {
private $cart;
public function __construct(Cart $cart) {
$this->cart = $cart;
}
public function checkout() {
throw new Exception('you cannot checkout an aldready close cart');
}
public function whatever() {
//logic
}
}
<?php
class EmptyCartStatus implements CartStatusInterface{
private $cart;
public function __construct(Cart $cart) {
$this->cart = $cart;
}
public function checkout() {
throw new Exception('you cannot checkout an empty cart');
}
public function whatever() {
//logic
}
}
<?php
class OpenCartStatus implements CartStatusInterface{
private $cart;
public function __construct(Cart $cart) {
$this->cart = $cart;
}
public function checkout() {
//logica di checkout...
}
public function whatever() {
//logic
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment