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?

\$\endgroup\$
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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.