blogs

Design Principals – Encapsulate what varies

Posted 13 December 2020

The most important design patterns and the foundational principal. Identify the aspects of your application that vary and separate them what stays the same  If you have got some aspect of your code that changing with every new requirement that you got. And some part of codes does not change so we want to pulled out and separate them. So that later you can alter or extend parts that vary without affecting the parts that don’t vary  

Encapsulate what varies underlies almost all design pattern

 

Lest See some example  

<?php


class OrderPancake
{
    private $pancake;

    public function orderPancake(string $type)
    {
        $this->pancake = new PaneCakeFactory($type);
        if ($type === 'classic') {
            $this->pancake = new ClassicPaneCake();
        } elseif ($type === 'blueberry') {
            $this->pancake = new BlueBerryPaneCake();
        } elseif ($type === 'chocolateChip') {
            $this->pancake = newChocolateChipPaneCake();
        }

        $this->pancake->cook();
        $this->pancake->plate();
        $this->pancake->addButter();
        return $this->pancake;
    }
} 

You can see there OrderPaneCake class have an function to order with different step of processing. First thing you can see create suitable pane cake object with type then cook, plate and add butter

In here you can see cook , plate and addButter same for all pane cakes, other parts is getting PaneCake object. In here Think when add new type of pane cake or remove any pane cake from menu because of poor sales we want to update piece of code frequently

So now our design principal says or guide to encapsulate what varies. In other words we want to find what is varying and pull it out into it’s own self container thing so that we can change or update or remove late  that on its own

Look at below image it show what varies from each other

Getting type of paneCake object part is varying. so we can pull it and encapsulate with different class. There lot of design pattern to do this here we can simply add in to factory class

 

class PaneCakeFactory
{

    public function createPaneCake(string $type)
    {
        if ($type === 'classic') {
            $paneCakeObject = new ClassicPaneCake();
        } elseif ($type === 'blueberry') {
            $paneCakeObject = new BlueBerryPaneCake();
        } elseif ($type === 'chocolateChip') {
            $paneCakeObject = newChocolateChipPaneCake();
        }

        return $paneCakeObject;
    }
}

So here one class responsible for generate pane cake object with type. So in future if any changes in menu we can localize our changes in to one class. When we look at the orderPaneCake  class there only responsible for prepare cake and instantiate PaneCakeFactory to get PaneCake Instance.
So Now our pancake menu can alter at anytime without affecting the result of our code. Its not best design pattern,   but it simply explain one class of encapsulate one varies

Encapsulate What varies as kind of master principal. its all about letting one part of the system vary independence of another part . something almost all design patterns do in some way

Encapsulate What Varies

  • Look for code that changes with every new requirement
  • Alter or extend the code that varies without affecting code that does not
  • Basis of almost every design pattern
  • Pay attention to how each pattern makes use of this principal

 

Thanks

 

Leave a Reply

Your email address will not be published. Required fields are marked *