3
\$\begingroup\$

I want to use SOLID more and more but still learning, I'm improving a PPT generator and would like your help applying SOLID principles in it.

  • That generator should print two photos per slide, first one to the left, second to the right;
  • If the last photo is a left photo, it should be placed in center;
  • Each photo has a text right below it;
  • I'll use a third party PPT lib to do the low level stuff.

This is what I came up with, a little bit abstracted just to pass the idea, the sample is in PHP but I'll accept answers in any programming language:

class Controller 
{
 $ppt = new Ppt(Ppt::DEFAULT_RESOLUTION, $background, $header, $footer);
 $ppt->writeSlides($photoCollection);
 $pptFile = $ppt->saveToFile();
 $ppt->deleteLocalTemporaryFiles();
 readfile($pptFile);
 unlink($pptFile);
}
class Ppt extends thirdPartyPptLib
{
 const DEFAULT_RESOLUTION = 'x';
 private $resolution;
 private $background;
 private $header;
 private $footer;
 public function __construct($resolution = null, $background = null, $header = null, $footer = null)
 {
 $this->resolution = $resolution;
 $this->background = $background;
 $this->header = $header;
 $this->footer = $footer;
 }
 public function writeSlides(\Traversable $photoCollection)
 {
 $i = 0;
 $count = count($photoCollection);
 foreach ($photoCollection as $photoInfo) {
 $photoToLeft = $i % 2 == 0;
 $isLastPhoto = $i == ($count - 1);
 if ($isLastPhoto) {
 $slide = $this->newSlide(); 
 $slide->addCenterPhotoInfo($photoInfo); 
 } else if ($photoToLeft) {
 $slide = $this->newSlide(); 
 $slide->addLeftPhotoInfo($photoInfo); 
 } else {
 $slide->addRightPhotoInfo($photoInfo); 
 } 
 $i++; 
 }
 }
 private function newSlide()
 {
 $slide = parent->newSlide();
 if ($this->hasBackground()) {
 $slide->setBackground();
 }
 if ($this->hasHeader()) {
 $slide->setHeader();
 }
 if ($this->hasFooter()) {
 $slide->setFooter();
 } 
 return $slide;
 }
}
class Slide extends thirdPartyPptLibSlide
{
 public function addLeftPhotoInfo($photoInfo)
 {
 $this->addLeftPhoto($photoInfo->getPhoto());
 $this->addUnderlyingText($photoInfo->getText());
 }
 // functions addRightPhotoInfo and addCenterPhotoInfo very similar
}

Should I separate it more into different classes? Or methods? Should I create Interfaces?

Phrancis
20.5k6 gold badges69 silver badges155 bronze badges
asked Jun 20, 2018 at 18:15
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

You can remove the overloading of properties from constructor. This way you can improve the code maintaning and readability.

An example of improvement:

class Style {
 // Properties here 
}
class Ppt {
 public function __construct(Style $style) {
 }
}

Now you pass just the object of style with all the needed properties.

answered Jun 20, 2018 at 18:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.