所以如果我有一个界面 iConnection
interface iConnection
{
}
通过以下方式实现:
class OutlookConnection implements iConnection
{
}
和
class GoogleConnection implements iConnection
{
}
和另一个界面 iComparison
interface iComparison
{
}
通过以下方式实现:
class OutlookComparison implements iComparison
{
private $connection;
public function __construct($user, iConnection $iConnection) {
$this->connection = $iConnection;
}
}
和
class GoogleComparison implements iComparison
{
private $connection;
public function __construct($user, iConnection $iConnection) {
$this->connection = $iConnection;
}
}
在主程序中,我希望能够基于iConnection类型在GoogleComparison和OutlookComparison之间进行切换,而无需使用if或switch语句:
public function __construct(iConnection $connection)
{
$this->connect = $connection;
if($this->connection instanceof GoogleConnection){
$this->comparison = new GoogleComparison();
}
elseif($this->connection instanceof OutlookConnection){
$this->comparison = new OutlookComparison();
}
}
在没有switch或if语句的情况下,是否可以在此构造函数中实现?
动漫人物