In a dense approach to the use of patterns in the design and came across a contradiction between the factory pattern and the 2 SOLID law of principles, which says about openness / closedness (Open-closed): "software entities must be open for expansion, but closed for modification".
Suppose we already have a code (listed below) based on a pattern (template) of the Factory in which data is stored and sent in Json format. Suppose we still have to give the data in a different format. We are based on the interface make the class CsvEncode. But how to deal with the Notepad factory - add the public function method getCsvEncode () there
Is adding a method to a class a violation of the second law of the SOLID principles?
Or in this case, the programmer should review the code and rewrite it under the abstract factory? I am just starting to use design patterns, and in order to apply them appropriately only where needed, i.e. throwing the responsibility of executing code on them, being able to create independent libraries, I need to understand. Maybe I misunderstood the factory pattern in the context of the SOLID principles? I saw that in the factory method they sometimes put the switch case. And depending on the parameter they return an instance of the corresponding class - this is where I found a contradiction, if you need to create another instance of the class - you will have to go into the code and hardcode
<?php //я так понимаю что шаблон фабрика это как правило связь один к одному обоих классов проиходящих от заданных интерфейсов //итак задача - у нас есть данные в виде массива //необходимо получить из этого массива заголовок,подвал, контент //массив отдать в формате json и csv /* *это interface Factory создающий другие классы * */ interface Factory { /* * *@return string| NULL возвращает заголовок */ public function getHeader(); /* * *@return string| NULL возвращает содержимое */ public function getContent(); /* * *@return string| NULL возвращает подвал|нижний колонтитул */ public function getFooter(); /* * *@return Encode возвращает объект для управления данными в формате Json */ public function getJsonEncode(); } /* *это interfaceс Encode для перекодировки * */ interface Encode { public function getEncode(); } /* *класс для перкодировки данных исходящи от класса,поддерживающего интерфейс Factory * *@property Factory $data */ class JsonEncode implements Encode { protected $data; public function __construct(Factory $obj) { $this->data=$obj; } /* *преобразует данные в json формат * *@return string */ public function getEncode() { return json_encode( ['head'=>$this->data->getHeader(),'content'=>$this->data->getContent(),'footer'=>$this->data->getFooter()] ); } } class CsvEncode implements Encode { protected $data; public function __construct(Factory $obj) { $this->data=$obj; } /* *преобразует данные в csv формат * *@return string */ public function getEncode() { return $this->data->getHeader(). PHP_EOL .$this->data->getContent(). PHP_EOL .$this->data->getFooter(); } } /* *класс для перкодировки данных исходящи от класса,поддерживающего интерфейс Factory * *@property string $head *@property string $content *@property string $footer */ class Notepad implements Factory{ protected $head; protected $content; protected $footer; /* * *@param array $arr */ public function __construct($arr){ $this->head=$arr['head']; $this->content=$arr['content']; $this->footer=$arr['footer']; } /* * *@return string| NULL возвращает заголовок */ public function getHeader() { return $this->head; } /* * *@return string| NULL возвращает содержимое */ public function getContent() { return $this->content; } /* * *@return string| NULL возвращает подвал|нижний колонтитул */ public function getFooter() { return $this->footer; } /* * *@return Encode возвращает объект для управления данными в формате Json */ public function getJsonEncode() { return new JsonEncode($this); } } $arr=['head'=>'заголовок','content'=>'содержимое','footer'=>'колонтитул']; $a=new Notepad($arr); echo $a->getJsonEncode()->getEncode();//выводим массив в формате json I'm still learning how to use phpDoc, for simplicity I have not used the example of checking for values, catching errors (try {} catch () {}) and automatic connection of classes :) thanks for reading