PHP设计模式之工厂模式

it2022-05-05  187

<?php // 设计模式之工厂模式 class Factory { static public function fac ($id) { switch ($id) { case 1: return new A(); case 2: return new B(); case 3: return new C(); default: return new D(); } } } /** * 创建一个接口 */ interFace FetchName { public function getname(); } class A implements FetchName { private $name = "A"; public function getname () { return $this->name; } } class B implements FetchName { private $name = "B"; public function getname () { return $this->name; } } class C implements FetchName { private $name = "C"; public function getname () { return $this->name; } } class D implements FetchName { private $name = "D"; public function getname () { return $this->name; } } $obj = Factory::fac(5); echo $obj->getname();

转载于:https://www.cnblogs.com/phpcurd/p/8506560.html

相关资源:PHP设计模式之工厂模式实例总结

最新回复(0)