-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactory.cpp
More file actions
54 lines (46 loc) · 1.78 KB
/
AbstractFactory.cpp
File metadata and controls
54 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @cite Abstract Factory Pattern states that if the objects being intialized are implementation of some abstract class then their corresponding factories should also have a Abstract Factory that they implement.
* The abstract factory makes the intialization independent from the type of the object.
* Aims at increasing extensibility by providing a highly modular API.
*
* @brief Abstract Factory Pattern can be exemplified by an Extractor class that can extract data from different file formats (like json, csv etc.).
*/
#include "iostream"
#include "map"
#include "FileFactory.h"
/**
* @brief Extractor makes use of the different File Factories in order to instantiate different File handlers.
*/
class Extractor
{
// stores the supported file formats and their corresponding factories.
std::map<std::string, FileFactory*> file_factories;
public:
Extractor() {
// Initialize all the factories
file_factories["json"] = new JsonFileFactory();
file_factories["csv"] = new CsvFileFactory();
}
/**
* @brief Takes the filename and instantiates the File object based on the extension.
*/
File* extract(std::string filename) {
auto ext = filename.substr(filename.find_last_of('.')+1);
if(!file_factories[ext]) {
throw std::runtime_error("Extension ." + ext + " not recognized .. ");
}
auto file = file_factories[ext]->make(filename);
return file;
}
};
int main() {
// Highly simplified API that takes only the filename and returns the required File.
Extractor extractor;
// CSV file loaded
auto csv = extractor.extract("data.csv");
csv->extract_data();
// JSON file loaded
auto json = extractor.extract("data.json");
json->extract_data();
return 0;
}