💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ <?php class Button{} class Border{} class MacButton extends Button{} class WinButton extends Button{} class MacBorder extends Border{} class WinBorder extends Border{} interface AbstractFactory{ public function createButton(); public function createBorder(); } class MacFactory implements AbstractFactory{ public function createButton(){return new MacButton;} public function createBorder(){return new MacBorder;} } class WinFactory implements AbstractFactory{ public function createButton(){return new WinButton;} public function createBorder(){return new WinBorder;} } $mac_obj = new MacFactory(); var_dump($mac_obj->createButton()); var_dump($mac_obj->createBorder()); $win_obj = new WinFactory(); var_dump($win_obj->createButton()); var_dump($win_obj->createBorder()); ~~~ 只是定义公共的方法,具体的类去实现各自的方法 [root@centos6 dp]# php abstract_factory.php object(MacButton)#2 (0) { } object(MacBorder)#2 (0) { } object(WinButton)#3 (0) { } object(WinBorder)#3 (0) { }