問題:一個公司的員工被要求與另外一個公司進行數據交流,如今暫定使用A格式來寫,可是後期可能會有B或更多的格式。或許還會出現不一樣的產品。好比產品PA,產品PB。php
<?php abstract class PAEncoder { abstract function encode(); } abstract class PBEncoder { abstract function encode(); } abstract class Manager { const PA = 1; const PB = 2; abstract function make($init); } class AManager extends Manager { public function make($init) { switch ($init) { case self::PA: return new PAA(); case self::PB: return new PAB(); default: return new PAA(); } } } class BManager extends Manager { public function make($init) { switch ($init) { case self::PA: return new PBA(); case self::PB: return new PBB(); default: return new PBA(); } } } class PAA extends PAEncoder { public function encode() { echo 'PAA'; } } class PAB extends PBEncoder { public function encode() { echo 'PAB'; } } class PBA extends PAEncoder { public function encode() { echo 'PBA'; } } class PBB extends PBEncoder { public function encode() { echo 'PBB'; } } $a = new BManager(); $pa = $a->make(1); $pa->encode();