top of page

Design pattern – facade for C++

Welcome again, today I will talk shortly about façade design pattern. So we can make function inside warrior.cc on which we can show and explain why we are using this pattern – it is one from patterns which you are using but never know about it ;D

Let’s nightmare begin

warrior.h

 

#pragma once #include "human.h"

class warrior : public human { public: Weapon weaponFirstSlot; Weapon weaponSecondSlot; warrior(); ~warrior(); void goAndAttack(int distance, human* warrior); private: now we cannot access it from outsite void move(int); void attack(human*); };

 

warrior.cc

 

#include "warrior.h" warrior::warrior() { Weapon dagger(5,”Dagger”); weaponFirstSlot = dagger; weaponSecondSlot = dagger; } warrior::~warrior() { } void warrior::move(int steps) { position += steps; } void warrior::attack(human* man) { man->getHit(10 + weaponFirstSlot.getAttackPower() + weaponSecondSlot.getAttackPower()); } Void warrior::goAndAttack(int distance, human* warrior) { move(distance); attack(warrior); move(-distance); }

 

What is exactly mean? You hide functions attack and move, but you create one function which is making few things. We use it when end user don’t need to make something many times – he want to use it simple. Remote controller is best example – you press one, but when TV is off it will make few operations: - Check is tv is on - If not turn on TV - Check channel - If channel is wrong change channel - etc

Facade hide many implementations and make it smarter in use because you need to use one function for making tones of work. Good architect think about his stake holders.

Subscribe me!

Be sure that you read all.

  • Facebook Clean
  • Black Twitter Icon
  • Black Instagram Icon
  • Black YouTube Icon
Recent Posts
bottom of page