创建对象时的 SOLID 原则
SOLID principles while creating objects
场景如下:
A User can create shapes using a form.
User will select from a list of options of shapes (circle, square, rectangle and so on) and submit the form by clicking on create shape button.
The shape he created will be linked to his account.
我是这样处理的:
一个接口 Shape
带有 create()
形状的方法签名,它将由单独的形状 类.
实现
# Shape.php
interface Shape {
public function create();
public function area();
}
# Rectangle.php
class Rectangle implements Shape {
private $user;
private $data; # user submitted data
public __construct($user, $data) {
$this->user = $user;
$this->data = $data;
}
public function create() {
# get rectangle shape details from RectangleShapeAPI
$userShape = new UserShape; # the model to store data
$userShape->shape_id = $this->data['shape_id'];
$userShape->area = $this->area();
$userShape->users_id = $user->id;
# save rectangle shape details from API
if (!$userShape->save()) {
return false;
}
return true;
}
public function area() {
return $this->data['length'] * $this->data['breadth'];
}
}
现在,我必须 switch($shapeType)
用户选择的形状类型以适当地实例化 Square
或 Circle
或 Rectangle
类 .
$data = $_POST;
switch($shapeType) {
case 'square':
Shape $shape = new Square($user, $data);
$shape->create();
break;
and so on...
}
This is my first step towards implementing SOLID Principles
and I
think I may have gotten it wrong. Please forgive me in that case.
The problem is I think the switch statement (I've seen examples
where people try to eliminate switch
or conditions
when they try
to use those objects). I've seen this only at the time of using the
objects which I think is possible very easily. But can we achieve the
same during the time of object creation (I am not able to find any
logical solution to this other than switch
ing over shapes).
Other thing is, how to handle the $_POST
data effectively as the
keys to the array may change in future and anything may happen. How
would you handle this scenario?
以上解释是我的案例的简化版本。
我有一个 Accounts
接口与 add()
方法,由 FacebookAccount
、GoogleAccount
和其他 类 实现。
现在 add()
这些帐户的方法不同,因此用作接口。创建了 Facebook
或 Google
api 对象并提取用户数据以存储到 UserAccount
。帐户链接到用户。
我欢迎所有想告诉我如何使用 SOLID 原则解决这个问题的人。我会非常感谢所有人,希望我能学到很多关于原则的东西以及我做错了什么。
谢谢。
编辑
Link 到 SOLID 原则:SOLID Principles Wikipedia
终于想出了解决这两个问题的方法。
开关问题
创建一个工厂 class,它将 return 所需的对象。这将隐藏从场景中检索对象的条件复杂性。
$_POST
数据问题
这实际上很容易,并且是将数据传递给 create
方法而不是传递给构造函数的常识。将数据传递给 create
方法解决了这个问题,因为该方法只处理创建新对象,class 的其他部分现在独立于该更改。
场景如下:
A User can create shapes using a form.
User will select from a list of options of shapes (circle, square, rectangle and so on) and submit the form by clicking on create shape button.
The shape he created will be linked to his account.
我是这样处理的:
一个接口 Shape
带有 create()
形状的方法签名,它将由单独的形状 类.
# Shape.php
interface Shape {
public function create();
public function area();
}
# Rectangle.php
class Rectangle implements Shape {
private $user;
private $data; # user submitted data
public __construct($user, $data) {
$this->user = $user;
$this->data = $data;
}
public function create() {
# get rectangle shape details from RectangleShapeAPI
$userShape = new UserShape; # the model to store data
$userShape->shape_id = $this->data['shape_id'];
$userShape->area = $this->area();
$userShape->users_id = $user->id;
# save rectangle shape details from API
if (!$userShape->save()) {
return false;
}
return true;
}
public function area() {
return $this->data['length'] * $this->data['breadth'];
}
}
现在,我必须 switch($shapeType)
用户选择的形状类型以适当地实例化 Square
或 Circle
或 Rectangle
类 .
$data = $_POST;
switch($shapeType) {
case 'square':
Shape $shape = new Square($user, $data);
$shape->create();
break;
and so on...
}
This is my first step towards implementing
SOLID Principles
and I think I may have gotten it wrong. Please forgive me in that case.The problem is I think the switch statement (I've seen examples where people try to eliminate
switch
orconditions
when they try to use those objects). I've seen this only at the time of using the objects which I think is possible very easily. But can we achieve the same during the time of object creation (I am not able to find any logical solution to this other thanswitch
ing over shapes).Other thing is, how to handle the
$_POST
data effectively as the keys to the array may change in future and anything may happen. How would you handle this scenario?
以上解释是我的案例的简化版本。
我有一个 Accounts
接口与 add()
方法,由 FacebookAccount
、GoogleAccount
和其他 类 实现。
现在 add()
这些帐户的方法不同,因此用作接口。创建了 Facebook
或 Google
api 对象并提取用户数据以存储到 UserAccount
。帐户链接到用户。
我欢迎所有想告诉我如何使用 SOLID 原则解决这个问题的人。我会非常感谢所有人,希望我能学到很多关于原则的东西以及我做错了什么。
谢谢。
编辑
Link 到 SOLID 原则:SOLID Principles Wikipedia
终于想出了解决这两个问题的方法。
开关问题
创建一个工厂 class,它将 return 所需的对象。这将隐藏从场景中检索对象的条件复杂性。
$_POST
数据问题
这实际上很容易,并且是将数据传递给 create
方法而不是传递给构造函数的常识。将数据传递给 create
方法解决了这个问题,因为该方法只处理创建新对象,class 的其他部分现在独立于该更改。