Answer the question
In order to leave comments, you need to log in
How to correctly implement the creation of child instances of a class?
Hello!
There is something like an autoresponder, questions can be sent to the script, for example, from mail, via SMS, from a social network.
The task of the algorithm is to pick up the answer, and answer where the request came from. What the IAnswerProvider
interface was made for , which requires all descendants that implement it to implement the sendAnswer() method.
There are classes inheriting it SmsAnswerProvider EmailAnswerProvider PhoneAnswerProvider
And everything works fine until the most interesting question arises.
I want to send a notification to everyone who has ever interacted with me ... for which I select all users from the database (the database contains information about the source) and start the mailing list.
in the implementation I am forced to do something like
switch(источник)
case "sms":
answerProvider = new SmsAnswerProvider();
answerProvider.sendAnswer("Hello world")!
break;
case "email":
answerProvider = new EmailAnswerProvider();
answerProvider.sendAnswer("Hello world")!
break;
Answer the question
In order to leave comments, you need to log in
First, replace ul.unstyled span6 with ul.unstyled.span6 and:
ul.unstyled span6 > li:hover{
display: block;
}
label.control-label:hover + .controls > ul.unstyled.span6{
display: block;
}
interface SenderInterface
{
public function sendAnswer(string $message): void;
public function supports(string $type): bool;
}
class EmailSender implements SenderInterface
{
public function sendAnswer(string $message): void
{
// тут отправляете сообщение
}
public function supports(string $type): bool
{
return 'email' === $type;
}
}
class SmsSender implements SenderInterface
{
public function sendAnswer(string $message): void
{
// тут отправляете сообщение
}
public function supports(string $type): bool
{
return 'sms' === $type;
}
}
class SenderAggregate
{
private $providers;
public function __construct(SenderInterface ...$providers)
{
$this->providers = $providers
}
public function send(string $type, string $message)
{
foreach ($this->providers as $provider) {
if ($provider->supports($type) {
return $provider->sendAnswer($message);
}
}
throw Exception;
}
}
Create a Registry where you store the RegistryEntry. For each type of provider, according to the corresponding RegistryEntry implementation.
pseudo:
class Registry<T extends RegistryEntry> {
private Map<string, T> registries = new HashMap();
public void register(T entry) {}
public T find(string registryName) {}
}
class RegistryEntry {
public string registryName;
}
class AnswerProviderRegistry<AnswerProviderRegistryEntry> extends Registry {}
class AnswerProviderRegistryEntry<T extends IAnswerProvider> extends RegistryEntry {
abstract public T create();
}
class SmsAnswerProviderRegistryEntry<SmsAnswerProvider> extends AnswerProviderRegistryEntry {
constructor() {
this.registryName = "sms";
}
public SmsAnswerProvider create() {
return new SmsAnswerProvider();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question