R
R
RMate2019-11-01 15:59:35
css
RMate, 2019-11-01 15:59:35

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;

And if there are 30 sources? I'm doing something wrong. Please tell me how to implement my idea in this case. Thank you.
PS:
I thought that the "factory" pattern would be perfect for me, but the more I read about it, the less I understand whether I need it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Loginov, 2018-09-18
@Huf

First, replace ul.unstyled span6 with ul.unstyled.span6 and:

ul.unstyled span6 > li:hover{
    display: block;
}

on the
label.control-label:hover + .controls > ul.unstyled.span6{
      display: block;
}

K
kafkiansky, 2019-11-01
@RMate

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;
    }
}

A
Alex Wells, 2019-11-01
@Alex_Wells

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 question

Ask a Question

731 491 924 answers to any question