下面的代码使用了哪种设计模式?
which design pattern is used in the code below?
谁能告诉我下面的代码中使用了哪种设计模式?
此代码已用于招聘测试(实时编码游戏测试)。
所以我想知道答案,以防相同的代码返回。
class UnixText
{
function write($txt){echo $txt;}
function lf() {echo "\n";}
}
class MSWindowsText
{
function write($txt){echo $txt;}
function crlf() {echo "\r\n";}
}
interface Writer
{
function write($txt);
function newLine();
}
class UnixWriter implements Writer{
private $target;
public function __construct($unixText){$this->target=$unixText; }
function write($txt){$this->target->write($txt);}
function newLine(){$this->target->lf();}
}
class MSWindowsWriter implements Writer{
private $target;
public function __construct($winText){$this->target=$winText; }
function write($txt){$this->target->write($txt);}
function newLine(){$this->target->crlf();}
}
//example of use
$writer=NULL;
if($isUnix){$writer= new UnixWriter(new UnixText());}
else {$writer= new MSWindowsWriter(new MSWindowsText());}
$writer->write('First Line');
$writer->newLine();
$writer->write('Second Line');
```
我终于收到了回复,以防对其他人有帮助。
本例使用的设计模式是Adapter。
谢谢
谁能告诉我下面的代码中使用了哪种设计模式?
此代码已用于招聘测试(实时编码游戏测试)。 所以我想知道答案,以防相同的代码返回。
class UnixText
{
function write($txt){echo $txt;}
function lf() {echo "\n";}
}
class MSWindowsText
{
function write($txt){echo $txt;}
function crlf() {echo "\r\n";}
}
interface Writer
{
function write($txt);
function newLine();
}
class UnixWriter implements Writer{
private $target;
public function __construct($unixText){$this->target=$unixText; }
function write($txt){$this->target->write($txt);}
function newLine(){$this->target->lf();}
}
class MSWindowsWriter implements Writer{
private $target;
public function __construct($winText){$this->target=$winText; }
function write($txt){$this->target->write($txt);}
function newLine(){$this->target->crlf();}
}
//example of use
$writer=NULL;
if($isUnix){$writer= new UnixWriter(new UnixText());}
else {$writer= new MSWindowsWriter(new MSWindowsText());}
$writer->write('First Line');
$writer->newLine();
$writer->write('Second Line');
```
我终于收到了回复,以防对其他人有帮助。 本例使用的设计模式是Adapter。 谢谢