Answer the question
In order to leave comments, you need to log in
How to implement saving data using the factory method pattern?
Do I understand correctly that it is necessary to create an interface with a save method and several classes (saving to a database, saving to a file) that implement it?
Answer the question
In order to leave comments, you need to log in
Laravel has little to do with it. The pattern can be applied anywhere.
abstract class Presister
{
abstract public function factoryMethod(): Post;
public function save(): void
{
$post = $this->factoryMethod();
// сюда логику сохранения
$post->save();
}
}
class FancyPostPresister extends Presister
{
public function factoryMethod(): Post
{
return new FancyPost();
}
}
class RegularPostPresister extends Presister
{
public function factoryMethod(): Post
{
return new RegularPost();
}
}
interface Post
{
public function save(): void;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question