I feel like there's a design pattern for this problem, but I can't seem to find the proper one.
I have a Server class that has what you might call 3 processes to run, and then output a result. Each process has a list of "plugins" that all run at every process. Each plugin(class) has a constant, that is the name of the plugin. So it looks something like this:
<?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);
}
}
Can I cram this into a design pattern, it feels wrong to do all that copying and pasting for my functions in Server.
And please do tell me if I need to elaborate on anything.
Aucun commentaire:
Enregistrer un commentaire