HiveBrain v1.2.0
Get Started
← Back to all entries
principlecppMinor

GOF composite design pattern implementation using modern C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
designgofusingcompositeimplementationmodernpattern

Problem

After reading about the composite pattern from Design Patterns, I thought to re-implement the code mentioned in the motivation section using the modern C++ concept/idioms:

```
#include
#include
#include
#include
#include

/*/
template
void display(const T& val) {
std::cout ;
virtual ~Equipment() = default;
std::string Name() { return _name; }
virtual Watt Power() { return _power; }
virtual Currency NetPrice() { return _price; }
// fixed 20% percent discount
virtual Currency DiscountPrice() { return (0.8*NetPrice()); }
virtual void Add(EquipmentSmartPtr x) { }
virtual void Remove(EquipmentSmartPtr x) { }
protected:
Equipment(std::string n) :_name(n){ }
private:
std::string _name;
Watt _power{ 440 };
Currency _price{ 200 };
};

/*/
class FloopyDisk : public Equipment {
public:
FloopyDisk(std::string n) :Equipment(n) {}
virtual ~FloopyDisk() = default;
};

/*/
class Card : public Equipment {
public:
Card(std::string n) :Equipment(n) {}
virtual ~Card() = default;
};

/*/
//To find out whether items are in the composite objects
class Name_Equal {
private:
Equipment::EquipmentSmartPtr val;
public:
Name_Equal(const Equipment::EquipmentSmartPtr& v) :val(v) { }
bool operator()(const Equipment::EquipmentSmartPtr& x) const {
return (x->Name() == val->Name());
}
};

/*/
class CompositeEquipment : public Equipment {
public:
virtual ~CompositeEquipment() = default;
virtual Watt Power() override;
virtual Currency NetPrice() override;
virtual void Add(EquipmentSmartPtr) override;
virtual void Remove(EquipmentSmartPtr) override;
protected:
CompositeEquipment(std::string n) : Equipment(n) { }
private:
void find_equipment(std::vector&, EquipmentSmartPtr&);

Solution

-
These #includes:

#include
#include
#include
#include
#include


look nicer with a space after each #include directive:

#include 
#include 
#include 
#include 
#include 


-
This may also be nit-picky, but the "border" comments are a bit tacky. You could instead use that space to provide documentation on the usage. You could also put these implementations into separate files if they're best organized in that way instead.

-
This particular display() function serves no useful purpose and is only used in one place. Beyond that, the name is ambiguous because it doesn't state what is to be displayed specifically. Just remove this function and do the same displaying inline.

-
This doesn't look like a good alias:

using Currency = double;


It's preferred not to use such types with currency due to their precision with regards to values. Instead, use an integer type such as int. There are many resources online that more specifically state why this is an issue.

Code Snippets

#include<iostream>
#include<vector>
#include<string>
#include<memory>
#include<algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <algorithm>
using Currency = double;

Context

StackExchange Code Review Q#67761, answer score: 3

Revisions (0)

No revisions yet.