上一节理解了工厂模式。其代码原理如下:
<?php abstract class ApptEncoder{
abstract function encode();
} class BloggsApptEncoder extends ApptEncoder{
function encode(){
return "data encode farmat";
}
} abstract class CommsManager{
abstract function getHeaderText();
abstract function getApptEncoder();
abstract function getFootText();
} class BloggsCommsManager extends CommsManager{
function getHeaderText(){
return "header";
}
function getApptEncoder(){
return new BloggsApptEncoder();
}
function getFooterText(){
return "footer";
}
}
然后抽象工厂模式只是在工厂模式基础上进行升级,具体如下:
<?php abstract class ApptEncoder{
abstract function encode();
} class BloggsApptEncoder extends ApptEncoder{
function encode(){
return "data encode format appt";
}
} abstract class TdcEncoder{
abstract function encode();
}
class BloggsTdcEncoder extends TdcEncoder{
function encode(){
return "data encode format tdc";
}
}
abstract class ConEncoder{
abstract function encode();
}
class BloggsConEncoder extends ConEncoder{
function encode(){
return "data encode format con";
}
} abstract class CommsManager{
abstract function getHeaderText();
abstract function getApptEncoder();
abstract function getFootText(); abstract function getTdcEncoder();
abstract function getConEncoder();
} class BloggsCommsManager extends CommsManager{
function getHeaderText(){
return "header";
}
function getApptEncoder(){
return new BloggsApptEncoder();
}
function getFooterText(){
return "footer";
} function getTdcEncoder(){
return new BloggsTdcEncoder();
} function getConEncoder(){
return new BloggsConEncoder();
}
}
具体原理就是在工厂模式上的扩展和分支吧