1
\$\begingroup\$

I am going over a series of design patterns and I like you to verify that I implemented it properly. The code works. It is not a homework assignment. Checking for null pointers is omitted for simplicity.

Compiling:

g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CArmsLongSleeve.cpp -o dbg/bin/CArmsLongSleeve.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CArmsMuscular.cpp -o dbg/bin/CArmsMuscular.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CGameplay.cpp -o dbg/bin/CGameplay.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CHeadOblong.cpp -o dbg/bin/CHeadOblong.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CHeadSquare.cpp -o dbg/bin/CHeadSquare.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CLegsFrog.cpp -o dbg/bin/CLegsFrog.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CLegsLong.cpp -o dbg/bin/CLegsLong.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CR2D2Builder.cpp -o dbg/bin/CR2D2Builder.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CRobot.cpp -o dbg/bin/CRobot.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CRobotBuilderBase.cpp -o dbg/bin/CRobotBuilderBase.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CTerminatorBuilder.cpp -o dbg/bin/CTerminatorBuilder.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c main.cpp -o dbg/bin/main.o
g++ -g -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -lm -lpthread dbg/bin/CArmsLongSleeve.o dbg/bin/CArmsMuscular.o dbg/bin/CGameplay.o dbg/bin/CHeadOblong.o dbg/bin/CHeadSquare.o dbg/bin/CLegsFrog.o dbg/bin/CLegsLong.o dbg/bin/CR2D2Builder.o dbg/bin/CRobot.o dbg/bin/CRobotBuilderBase.o dbg/bin/CTerminatorBuilder.o dbg/bin/main.o -o Builder_dbg.exe
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CArmsLongSleeve.cpp -o rel/bin/CArmsLongSleeve.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CArmsMuscular.cpp -o rel/bin/CArmsMuscular.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CGameplay.cpp -o rel/bin/CGameplay.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CHeadOblong.cpp -o rel/bin/CHeadOblong.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CHeadSquare.cpp -o rel/bin/CHeadSquare.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CLegsFrog.cpp -o rel/bin/CLegsFrog.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CLegsLong.cpp -o rel/bin/CLegsLong.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CR2D2Builder.cpp -o rel/bin/CR2D2Builder.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CRobot.cpp -o rel/bin/CRobot.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CRobotBuilderBase.cpp -o rel/bin/CRobotBuilderBase.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c CTerminatorBuilder.cpp -o rel/bin/CTerminatorBuilder.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -c main.cpp -o rel/bin/main.o
g++ -I inc -Wall -std=gnu++11 -pthread -Wextra -Weffc++ -lm -lpthread rel/bin/CArmsLongSleeve.o rel/bin/CArmsMuscular.o rel/bin/CGameplay.o rel/bin/CHeadOblong.o rel/bin/CHeadSquare.o rel/bin/CLegsFrog.o rel/bin/CLegsLong.o rel/bin/CR2D2Builder.o rel/bin/CRobot.o rel/bin/CRobotBuilderBase.o rel/bin/CTerminatorBuilder.o rel/bin/main.o -o Builder_rel.exe

Code:

CArmsBase.h

#ifndef CARMSBASE_H
#define CARMSBASE_H

class CArmsBase
{
public:
  virtual void IdentifyArms() = 0;
  virtual void UseArms() = 0;
};

#endif // CARMSBASE_H

CHeadBase.h

#ifndef CHEADBASE_H
#define CHEADBASE_H

class CHeadBase
{
public:
  virtual void IdentifyHead() = 0;
  virtual void UseHead() = 0;
};

#endif // CHEADBASE_H

CLegsBase.h

#ifndef CLEGSBASE_H
#define CLEGSBASE_H

class CLegsBase
{
public:
  virtual void IdentifyLegs() = 0;
  virtual void UseLegs() = 0;
};

#endif // CLEGSBASE_H

CRobotBuilderBase.cpp

#include <iostream>

#include "CRobotBuilderBase.h"
#include "CRobot.h"

CRobotBuilderBase::CRobotBuilderBase() : iRobotPtr(std::make_shared<CRobot>())
{
}

CRobotBuilderBase::~CRobotBuilderBase()
{
  std::cout << __PRETTY_FUNCTION__ << ": Started." << std::endl;
}

std::shared_ptr<CRobot> CRobotBuilderBase::GetRobot()
{
  return iRobotPtr;
}

CRobotBuilderBase.h

#ifndef CROBOTBUILDERBASE_H
#define CROBOTBUILDERBASE_H

#include "CRobot.h"

class CRobotBuilderBase
{
public:
  CRobotBuilderBase();
  virtual ~CRobotBuilderBase();
  virtual void Build() = 0;

  std::shared_ptr<CRobot> GetRobot();

private:
  std::shared_ptr<CRobot> iRobotPtr;
};

#endif // CROBOTBUILDERBASE_H

CArmsLongSleeve.cpp

#include <iostream>

#include "CArmsLongSleeve.h"

void CArmsLongSleeve::IdentifyArms()
{
  std::cout << __PRETTY_FUNCTION__ << " long sleeve arms" << std::endl;
}

void CArmsLongSleeve::UseArms()
{
  std::cout  << __PRETTY_FUNCTION__<< " staying warm" << std::endl;
}

CArmsLongSleeve.h

#ifndef CLONGSLEEVEARMS_H
#define CLONGSLEEVEARMS_H

#include "CArmsBase.h"

class CArmsLongSleeve : public CArmsBase
{
public:
  void IdentifyArms();
  void UseArms();
};

#endif // CLONGSLEEVEARMS_H

CArmsMuscular.cpp

#include <iostream>

#include "CArmsMuscular.h"

void CArmsMuscular::IdentifyArms()
{
  std::cout << __PRETTY_FUNCTION__ << " long sleeve arms" << std::endl;
}

void CArmsMuscular::UseArms()
{
  std::cout << __PRETTY_FUNCTION__ << " lift a bag" << std::endl;
}

CArmsMuscular.h

#ifndef CARMSMUSCULAR_H
#define CARMSMUSCULAR_H

#include "CArmsBase.h"

class CArmsMuscular : public CArmsBase
{
public:
  void IdentifyArms();
  void UseArms();
};

#endif // CARMSMUSCULAR_H

CGameplay.cpp

#include <iostream>

#include "CGameplay.h"
#include "CR2D2Builder.h"
#include "CTerminatorBuilder.h"

CGameplay::CGameplay() : iRobotVector(std::vector<std::shared_ptr<CRobot>> {})
{
}

void CGameplay::BuildR2D2()
{
  CR2D2Builder r2d2Builder;

  r2d2Builder.Build();

  iRobotVector.push_back(r2d2Builder.GetRobot());
}

void CGameplay::BuildTerminator()
{
  CTerminatorBuilder terminatorBuilder;

  terminatorBuilder.Build();

  iRobotVector.push_back(terminatorBuilder.GetRobot());
}

void CGameplay::Run()
{
  std::cout << __PRETTY_FUNCTION__ << ": Started." << std::endl;

  BuildR2D2();
  BuildTerminator();

  std::cout << __PRETTY_FUNCTION__ << ": Both robots are ready." << std::endl;

  for(auto const& it : iRobotVector)
  {
    it->iArmsBasePtr->IdentifyArms();
    it->iArmsBasePtr->UseArms();

    it->iHeadBasePtr->IdentifyHead();
    it->iHeadBasePtr->UseHead();

    it->iLegsBasePtr->IdentifyLegs();
    it->iLegsBasePtr->UseLegs();
  }

  std::cout << __PRETTY_FUNCTION__ << ": Finished." << std::endl;
}

CGameplay.h

#ifndef CGAMEPLAY_H
#define CGAMEPLAY_H

#include <vector>

#include "CRobot.h"

class CGameplay
{
public:
  CGameplay();
  void Run();

private:
  void BuildTerminator();
  void BuildR2D2();

  std::vector<std::shared_ptr<CRobot>> iRobotVector;
};

#endif // CGAMEPLAY_H

CHeadOblong.cpp

#include <iostream>

#include "CHeadOblong.h"

void CHeadOblong::IdentifyHead()
{
  std::cout << __PRETTY_FUNCTION__ << " Identifying head as oblong" << std::endl;
}

void CHeadOblong::UseHead()
{
  std::cout << __PRETTY_FUNCTION__ << " Using oblong head to think" << std::endl;
}

CHeadOblong.h

#ifndef CHEADOBLONG_H
#define CHEADOBLONG_H

#include "CHeadBase.h"

class CHeadOblong : public CHeadBase
{
  void IdentifyHead() override;
  void UseHead() override;
};

#endif // CHEADOBLONG_H

CHeadSquare.cpp

#include <iostream>

#include "CHeadSquare.h"

void CHeadSquare::IdentifyHead()
{
  std::cout << __PRETTY_FUNCTION__ << " Identifying head as square" << std::endl;
}

void CHeadSquare::UseHead()
{
  std::cout << __PRETTY_FUNCTION__ << " Using square head to play chess" << std::endl;
}

CHeadSquare.h

#ifndef CHEADOBLONG_H
#define CHEADOBLONG_H

#include "CHeadBase.h"

class CHeadSquare : public CHeadBase
{
  void IdentifyHead() override;
  void UseHead() override;
};

#endif // CHEADOBLONG_H

CLegsFrog.cpp

#include <iostream>

#include "CLegsFrog.h"

void CLegsFrog::IdentifyLegs()
{
  std::cout << __PRETTY_FUNCTION__ << " Frog legs" << std::endl;
}

void CLegsFrog::UseLegs()
{
  std::cout << __PRETTY_FUNCTION__ << " twitch in electric discharge" << std::endl;
}

CLegsFrog.h

#ifndef CLEGSLONG_H
#define CLEGSLONG_H

#include "CLegsBase.h"

class CLegsFrog : public CLegsBase
{
  void IdentifyLegs() override;
  void UseLegs() override;
};

#endif // CLEGSLONG_H

CLegsLong.cpp

#include <iostream>

#include "CLegsLong.h"

void CLegsLong::IdentifyLegs()
{
  std::cout << __PRETTY_FUNCTION__ << " Long legs" << std::endl;
}

void CLegsLong::UseLegs()
{
  std::cout << __PRETTY_FUNCTION__ << " Using legs to walk" << std::endl;
}

CLegsLong.h

#ifndef CLEGSLONG_H
#define CLEGSLONG_H

#include "CLegsBase.h"

class CLegsLong : public CLegsBase
{
  void IdentifyLegs() override;
  void UseLegs() override;
};

#endif // CLEGSLONG_H

CR2D2Builder.cpp

#include <iostream>

#include "CR2D2Builder.h"

#include "CArmsLongSleeve.h"
#include "CHeadOblong.h"
#include "CLegsFrog.h"

CR2D2Builder::CR2D2Builder()
{
  std::cout << __PRETTY_FUNCTION__ << ": Constructing." << std::endl;
}

void CR2D2Builder::Build()
{
  GetRobot()->iArmsBasePtr = std::make_shared<CArmsLongSleeve>();
  GetRobot()->iHeadBasePtr = std::make_shared<CHeadOblong>();
  GetRobot()->iLegsBasePtr = std::make_shared<CLegsFrog>();
}

std::shared_ptr<CRobot> CR2D2Builder::GetRobot()
{
  return CRobotBuilderBase::GetRobot();
}

CR2D2Builder.h

#ifndef CR2D2BUILDER_H
#define CR2D2BUILDER_H

#include "CRobot.h"
#include "CRobotBuilderBase.h"

class CR2D2Builder : public CRobotBuilderBase
{
public:
  CR2D2Builder();
  void Build();
  std::shared_ptr<CRobot> GetRobot();
};

#endif // CR2D2BUILDER_H

CRobot.cpp

#include "CRobot.h"

CRobot::CRobot()
  : iArmsBasePtr(nullptr)
  , iHeadBasePtr(nullptr)
  , iLegsBasePtr(nullptr)
{
}

CRobot.h

#ifndef CROBOT_H
#define CROBOT_H

#include <memory>

#include "CArmsBase.h"
#include "CHeadBase.h"
#include "CLegsBase.h"

class CRobot
{
public:
  CRobot();

  std::shared_ptr<CArmsBase> iArmsBasePtr;
  std::shared_ptr<CHeadBase> iHeadBasePtr;
  std::shared_ptr<CLegsBase> iLegsBasePtr;
};


#endif // CROBOT_H

CTerminatorBuilder.cpp

#include <iostream>

#include "CTerminatorBuilder.h"

#include "CArmsMuscular.h"
#include "CHeadSquare.h"
#include "CLegsFrog.h"

CTerminatorBuilder::CTerminatorBuilder()
{
  std::cout << __PRETTY_FUNCTION__ << ": Constructing." << std::endl;
}

void CTerminatorBuilder::Build()
{
  GetRobot()->iArmsBasePtr = std::make_shared<CArmsMuscular>();
  GetRobot()->iHeadBasePtr = std::make_shared<CHeadSquare>();
  GetRobot()->iLegsBasePtr = std::make_shared<CLegsFrog>();
}

std::shared_ptr<CRobot> CTerminatorBuilder::GetRobot()
{
  return CRobotBuilderBase::GetRobot();
}

CTerminatorBuilder.h

#ifndef CTERMINATORBUILDER_H
#define CTERMINATORBUILDER_H

#include "CRobot.h"
#include "CRobotBuilderBase.h"

class CTerminatorBuilder : public CRobotBuilderBase
{
public:
  CTerminatorBuilder();
  void Build() override;
  std::shared_ptr<CRobot> GetRobot();
};

#endif // CTERMINATORBUILDER_H

main.cpp

#include <iostream>

#include "CGameplay.h"

int main()
{
  CGameplay gameplay;

  gameplay.Run();

  return 0;
}
\$\endgroup\$
  • \$\begingroup\$ You'll probably find compiling your code much easier if you use CMake or even just Make. \$\endgroup\$ – Justin Oct 27 '17 at 18:57
  • \$\begingroup\$ This may be tricky because typically the "correct" way with design patterns is the one that the professor demonstrates in their lectures. So you better double-check with them. Besides, are you sure that the professor is fine with forum consulting? Sometimes they look down at it. Double-check that, as well. \$\endgroup\$ – iksemyonov Oct 28 '17 at 17:55
  • \$\begingroup\$ @iksemyonov: This work is not homework, like it sais in the introduction. \$\endgroup\$ – TradingDerivatives.eu Oct 29 '17 at 21:11
  • \$\begingroup\$ @Justin: I am using a makefile and an IDE, I am sharing the verbose output because it shows info that may be considered relevant, like I am using C++11 and warnings. \$\endgroup\$ – TradingDerivatives.eu Oct 29 '17 at 21:14
  • \$\begingroup\$ @TradingDerivatives.eu Then "correct" becomes rather vague. It depends on the book or course you're using. \$\endgroup\$ – iksemyonov Oct 30 '17 at 17:10

Your Answer

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

Browse other questions tagged or ask your own question.