Answer the question
In order to leave comments, you need to log in
How to choose the right architecture (OOP) for the task?
I can’t choose the right architecture for the task, I’m writing something like a site aggregator.
There is an Account model with the following fields: login, password, provider_name
There is a Manager class, in the constructor of which the Account model is passed. The Manager class has a getProvider() method. Which initializes and returns an object of the Provider class according to the value of the provider_name property in the Account model.
Each aggregated site has its own Provider class that can implement interfaces, for example: ProfileInterface, MessageInterface, NewsInterface, etc.
Interfaces describe the basic methods for working with the site. So that you can work in a loop (using the instanceof construct) with different sites without thinking about what happens inside the Provider class.
So it turns out that when the Provider class implements several interfaces, then everything is fine, but when the number of implemented interfaces becomes more than 5-10, the file becomes unreadable, there are too many methods in the Provider class.
Suppose there are such providers:
Social/VK/Provider
Social/OK/Provider
Social/Facebook/Provider
The Manager class, receiving the Account object in the constructor by the provider_name field, finds the required Provider, for example, Social/VK/Provider and initializes it and returns the Provider object. That is, authorization is carried out on the Vkontakte website in the constructor of the Provider class.
Next, there is a task to get news from all accounts:
foreach($accounts as $account) {
$provider = Manager::getProvider($account);
if ($provider instanceof NewsInterface) {
$news = $provider->getNews();
//...
} else continue;
}
Answer the question
In order to leave comments, you need to log in
So that you can work in a loop (using the instanceof construct) with different sites without thinking about what happens inside the Provider class.That's what polymorphism is for. Instanceof is an antipattern.
class FooProfileThing implements ProfileThingInterface {}
class FooMessageThing implements MessageThingInterface {}
class FooProvider extends Provider
{
public function __constructor($bar)
{
$this->bar = $bar;
$this->profileThing = new FooProfileThing;
$this->messageThing = new FooMessageThing;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question