我目前正在学习“复合设计模式”及其在Laravel中的使用,我想不通。
假设我有一个Things
的列表。这些东西具有相同的基本属性,因此,例如,如果我为每个Things
创建一个模型,我将得到类似的信息:
Thing1.php:
<?php
class Thing1 extends Model {
protected $title;
protected $description;
protected $fillable = [
'title',
'description'
];
public function __construct(string $title, string $description) {
$this->title = $title;
$this->description = $description;
}
public function getTitle(): string {
return $this->title;
}
public function getDescription(): string {
return $this->description;
}
public function setTitle(string $title) {
$this->title = $title;
}
public function setDescription(string $description) {
$this->description = $description;
}
}
Thing2.php:
<?php
class Thing2 extends Model {
protected $title;
protected $description;
protected $totalPrice = null;
protected $parent_id;
protected $fillable = [
'title',
'description',
'totalPrice',
'parent_id'
];
public function __construct(string $title, string $description, int $totalPrice = null, int $parent_id) {
$this->title = $title;
$this->description = $description;
$this->totalPrice = $totalPrice;
$this->parent_id = $parent_id;
}
public function getTitle(): string {
return $this->title;
}
public function getDescription(): string {
return $this->description;
}
public function getTotalPrice(): int {
return $this->totalPrice;
}
public function setTitle(string $title) {
$this->title = $title;
}
public function setDescription(string $description) {
$this->description = $description;
}
public function setTotalPrice(int $totalPrice) {
if(!is_null($totalPrice))
$this->totalPrice = $totalPrice;
}
public function setParentId(int $parent_id) {
$this->parent_id = $parent_id;
}
}
Thing3.php:
<?php
class Thing3 extends Model {
protected $title;
protected $description;
protected $totalPrice;
protected $fillable = [
'title',
'description',
'totalPrice'
];
public function __construct(string $title, string $description, int $totalPrice) {
$this->title = $title;
$this->description = $description;
$this->totalPrice = $totalPrice;
}
public function getTitle(): string {
return $this->title;
}
public function getDescription(): string {
return $this->description;
}
public function getTotalPrice(): int {
return $this->totalPrice;
}
public function setTitle(string $title) {
$this->title = $title;
}
public function setDescription(string $description) {
$this->description = $description;
}
public function setTotalPrice(int $totalPrice) {
$this->totalPrice = $totalPrice;
}
}
但是现在使用Composite Design Pattern,我的想法是创建一个名为ThingComposite.php
的模型,该模型将包含以下基本方法:
<?php
abstract class ThingComposite extends Model {
public function getTitle(): string;
public function getDescription(): string;
public function setTitle(string $title);
public function setDescription(string $description);
}
以及将扩展ThingComposite
的3个事物模型。
但是我想不出几件事,希望您能对我有所帮助:
如果所有Things
扩展了一个类,该类扩展了ThingComposite
并与一个表有关系,那么我将如何在Things
之间进行“仲裁”? / p>
属性:totalPrice
和parent_id
是“特殊键”,由于它们对于特定模型是唯一的,因此不会显示为基本属性/方法。那么我将如何在模型中表示它呢?怎样在表格中表示呢?
我听说过MySQL中的JSON,也许这是解决方案?
如果我想通过自己的对象显示每个Thing
,但使用与ThingComposite
类相关的相同“复合表”,则如何构建控制器?