سلام ,
با توجه به چیزی که متوجه شدم , کد پتر decorator رو اینجا میزارم.
امیدوارم مفید واقع بشود.
<?php
// Create your base class
interface Cost{
public function getCost();
public function getDescription();
public function getTotalCost();
public function getDetails();
}
// create all different type of costs :
class BasketCost implements cost
{
public function getCost()
{
return 15000;
}
public function getDescription()
{
return self::class;
}
public function getTotalCost()
{
return self::getCost();
}
public function getDetails()
{
return [
self::getDescription() => self::getCost()
];
}
}
class TaxCost implements Cost
{
public function __construct(Cost $cost)
{
$this->cost = $cost;
}
public function getCost()
{
return $this->cost->getTotalCost() * 0.09;
}
public function getDescription()
{
return self::class;
}
public function getTotalCost()
{
return $this->cost->getCost() + self::getCost();
}
public function getDetails()
{
return $this->cost->getDetails() + [
self::getDescription() => self::getCost()
];
}
}
class ShippingCost implements Cost
{
public function __construct(Cost $cost)
{
$this->cost = $cost;
}
public function getCost()
{
return 5000;
}
public function getDescription()
{
return self::class;
}
public function getTotalCost()
{
return $this->cost->getTotalCost() + self::getCost();
}
public function getDetails()
{
return $this->cost->getDetails() + [
self::getDescription() => self::getCost()
];
}
}
$basketCost = new BasketCost;
$basketWithTaxCost = new TaxCost(new BasketCost);
$backetWithTaxAndShippingCost = new ShippingCost($basketWithTaxCost);
# فقط قیمت کالاهای سبد خرید را برگردان
//var_dump($basketCost->getTotalCost());
# قیمت کالاهای سبد خرید + مالیات
//var_dump($basketWithTaxCost->getTotalCost());
# قیمت کالاهای سبد خرید + مالیات + هزینه ارسال
//var_dump($backetWithTaxAndShippingCost->getTotalCost());
این هم کد اصلاح شده برای جلسه بعدی :
<?php
// Create your Interface :
interface Cost
{
public function getCost();
public function getDescription();
public function getTotalCost();
public function getDetails();
}
// this is base class
class BasketCost implements Cost
{
public function getCost()
{
return 15000;
}
public function getDescription()
{
return self::class;
}
public function getTotalCost()
{
return self::getCost();
}
public function getDetails()
{
return [
self::getDescription() => self::getCost()
];
}
}
// create abstract class to avoid duplication of code :
abstract class AbstractCost implements Cost
{
protected $cost;
public function __construct(Cost $cost)
{
$this->cost = $cost;
}
public function getDescription()
{
return static::class;
}
public function getTotalCost()
{
return $this->cost->getTotalCost() + static::getCost();
}
public function getDetails()
{
return $this->cost->getDetails() + [
static::getDescription() => static::getCost()
];
}
}
// create all different type of costs as you like :
class TaxCost extends AbstractCost
{
public function getCost()
{
return $this->cost->getTotalCost() * 0.09;
}
}
class ShippingCost extends AbstractCost
{
public function __construct(Cost $cost)
{
$this->cost = $cost;
}
public function getCost()
{
return 5000;
}
}
$basketCost = new BasketCost;
$basketWithTaxCost = new TaxCost(new BasketCost);
$backetWithTaxAndShippingCost = new ShippingCost($basketWithTaxCost);
# فقط قیمت کالاهای سبد خرید را برگردان
//var_dump($basketCost->getTotalCost());
# قیمت کالاهای سبد خرید + مالیات
//var_dump($basketWithTaxCost->getTotalCost());
# قیمت کالاهای سبد خرید + مالیات + هزینه ارسال
//var_dump($backetWithTaxAndShippingCost->getTotalCost());