Add factory example

This commit is contained in:
Shaun Reed 2021-05-11 15:04:58 -04:00
parent 9bb2f9867d
commit 54426cbe5b
6 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,21 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: A project for practicing the factory C++ design pattern ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
#
cmake_minimum_required(VERSION 3.15)
project(
#[[NAME]] Factory
VERSION 1.0
DESCRIPTION "An example of the factory design pattern in C++"
LANGUAGES CXX
)
add_compile_options("-Wall")
add_library(parts "parts.cpp")
add_library(factory "factory.cpp")
add_executable(factory-test "main.cpp")
target_link_libraries(factory-test factory parts)

View File

@ -0,0 +1,42 @@
#include <iostream>
#include "factory.hpp"
Part* GearFactory::requestPart() {
// Create a new part
Part * newPart = makePart();
// Increment the QTY for part in AbstractFactory::inventory base class
// + If the item is not in the inventory map, this will also add it first.
inventory[newPart->getName()]++;
// Make the requested part
return newPart;
}
Part* GearFactory::requestPart(std::string partName, float price)
{
Part * newPart = makePart(partName, price);
inventory[newPart->getName()]++;
return newPart;
}
void GearFactory::showStock() const
{
for (const auto &item : inventory) {
if (item.first.empty()) continue; // Don't show an empty item
std::cout << item.first << " QTY in stock: " << item.second << std::endl;
}
}
/*****************************************************************************/
// Gear
Part* GearFactory::makePart() {
return new Gear();
}
Part* GearFactory::makePart(std::string name, float price)
{
return new Gear(name, price);
}

View File

@ -0,0 +1,33 @@
#ifndef FACTORY_HPP
#define FACTORY_HPP
#include <array>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "parts.hpp"
// Gear Concrete Factory
class GearFactory {
public:
explicit GearFactory(std::string name_="GearFactory") :
name(std::move(name_)) {}
Part* requestPart();
Part* requestPart(std::string partName, float price);
void showStock() const;
std::string getName() const { return name;}
protected:
Part* makePart();
Part* makePart(std::string name, float price);
private:
std::string name;
std::unordered_map<std::string, int> inventory;
};
#endif // FACTORY_HPP

View File

@ -0,0 +1,24 @@
#include <array>
#include <cstdlib>
#include <iostream>
#include <vector>
#include "factory.hpp"
int main(const int argc, const char * argv[]) {
// Testing GearFactory
GearFactory gearFactory;
const int gearsRequired = 5;
std::cout << "Testing " << gearFactory.getName() <<"...\nMaking 5 Gears...\n";
for (int i = 0; i < gearsRequired; i++) gearFactory.requestPart();
std::cout << std::endl << gearFactory.getName() << " inventory:\n";
gearFactory.showStock();
// Making custom gears
for (int i = 0; i < gearsRequired; i++) gearFactory.requestPart("Big Gear", 2.5f);
std::cout << std::endl << gearFactory.getName() << " inventory:\n";
gearFactory.showStock();
}

View File

@ -0,0 +1,10 @@
#include "parts.hpp"
/*****************************************************************************/
// Gear
Gear::Gear(std::string name, float price) {
partName = name;
partPrice = price;
}

View File

@ -0,0 +1,26 @@
#ifndef PARTS_HPP
#define PARTS_HPP
#include <string>
class Part {
public:
std::string getName() const { return partName;}
float getPrice() const { return partPrice;}
protected:
std::string partName; // The name of the part
float partPrice; // The partPrice of the part
};
/*****************************************************************************/
// Gear
class Gear : public Part {
public:
explicit Gear(std::string name="Gear", float price = 1.0f);
};
#endif // PARTS_HPP