我觉得这个问题有一个设计模式,但我似乎无法找到合适的设计模式。
我有一个Server类,你可以调用3 processes 来运行,然后输出结果。每个进程都有一个"插件列表"这一切都在每个过程中运行。每个插件(类)都有一个常量,即插件的名称。所以它看起来像这样:
<?php
// Simplified Server class.
class Server {
protected $format = array();
protected $service = array();
protected $handler = array();
public function addFormat($class, $name = '') {
if ($name === FALSE) {
$name = $class::NAME;
}
$this->formats[$name] = $class;
return $this;
}
public function getFormat($name) {
if (is_string($this->formats[$name])) {
$this->formats[$name] = new $this->formats[$name]($this);
}
return $this->formats[$name];
}
// And then these 2 functions are repeated 2 times, one for each of the other processes.
// So I got a addServer, getService, addHandler, getHandler function now.
public function processEverything($service, $handler, $format) {
$output = $this->getService($service)->getOutput();
$this->getHandler($handler)->handle($output);
return $this->getFormat($format)->format($output);
}
}
// The Format base class.
abstract class FormatBase {
const NAME = '';
abstract public function format($output);
}
// There's also 2 other base classes, that both have the NAME constant, but different
// abstract functions.
// A simple Format class
class UppercaseFormat extends FormatBase {
const NAME = 'test_format';
public function format($output) {
return strtoupper($output);
}
}
我可以将其塞进设计模式中,在服务器中执行所有复制和粘贴功能的感觉都是错误的。
请告诉我是否需要详细说明。