上一节理解了工厂模式。其代码原理如下:
<?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"; } }
然后抽象工厂模式只是在工厂模式基础上进行升级,具体如下:
1 <?php 2 3 abstract class ApptEncoder{ 4 abstract function encode(); 5 } 6 7 class BloggsApptEncoder extends ApptEncoder{ 8 function encode(){ 9 return "data encode format appt"; 10 } 11 } 12 13 class BloggsTdcEncoder extends ApptEncoder{ 14 function encode(){ 15 return "data encode format tdc"; 16 } 17 } 18 19 class BloggsConEncoder extends ApptEncoder{ 20 function encode(){ 21 return "data encode format con"; 22 } 23 } 24 25 26 abstract class CommsManager{ 27 abstract function getHeaderText(); 28 abstract function getApptEncoder(); 29 abstract function getFootText(); 30 31 abstract function getTdcEncoder(); 32 abstract function getConEncoder(); 33 } 34 35 class BloggsCommsManager extends CommsManager{ 36 function getHeaderText(){ 37 return "header"; 38 } 39 function getApptEncoder(){ 40 return new BloggsApptEncoder(); 41 } 42 function getFooterText(){ 43 return "footer"; 44 } 45 46 47 function getTdcEncoder(){ 48 return new BloggsTdcEncoder(); 49 } 50 51 function getConEncoder(){ 52 return new BloggsConEncoder(); 53 } 54 }
具体原理就是在工厂模式上的扩展和分支吧