方法工厂模式
方法工厂模式
<?php interface LogInterface { public function log(); } //mysql报错日志 class MysqlLog implements LogInterface { public function log() { // TODO: Implement log() method. } } //Redis报错日志 class RedisLog implements LogInterface { public function log() { // TODO: Implement log() method. } } //用户操作错误日志 class UserLog implements LogInterface { public function log() { // TODO: Implement log() method. } } //代码错诶日志 class ErrorLog implements LogInterface { public function log() { // TODO: Implement log() method. } } interface LogFactory { public function make(); } class MysqlLogFactory implements LogFactory { public function make() { return new MysqlLog(); } } class RedisLogFactory implements LogFactory { public function make() { return new RedisLog(); } } class UserLogFactory implements LogFactory { public function make() { return new UserLog(); } } class ErrorLogFactory implements LogFactory { public function make() { return new ErrorLog(); } } class Product { protected $Log; public function __construct() { $this->Log = array( (new MysqlLogFactory())->make(), (new RedisLogFactory())->make(), (new UserLogFactory())->make(), (new ErrorLogFactory())->make() ); } public function getLog() { return $this->Log; } } $product = new Product(); var_dump($product->getLog());