In client code, and at it's very simplest it should read something like:
<?php
// Client code
$int1 = new _Integer(2);
$int2 = new _Integer(3);
$operator = new Operator('+');
$op1 = new Operands($int1);
$op2 = new Operands($int2);
// $op1 + $operator + $op2 = Solution === 5
?>
However, we're attempting this using the composite design pattern and things soon become non-trivial. I have been working on the following:
<?php
// Use case: 2 + 3 = 5
// Component
interface Solution {
function f();
}
// Composite
class Operands implements Solution {
private $ints = [];
function __construct(_Integer $_integer = null ){
$this->_integer = $_integer;
array_push($this->ints, $this->_integer);
}
function f(){ /* Do nothing */ }
}
// Leaf
class _Integer implements Solution {
private $_int;
function __construct($_int){
$this->_int = $_int;
}
function f(){ /* Do nothing */ }
function getInt(){
return $this->_int;
}
}
// Leaf
class Operator implements Solution {
private $operator;
function __construct($operator){
$valid = in_array($operator, ['+','-','/','*']);
if($valid) $this->operator = $operator;
}
function f(){ /* Do nothing */ }
}
Ive reached a point where I need some help with Verbs and Nouns moreover the logic behind this great pattern - based around this simple equation.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire