Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
abstract-factory/main.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the gobelijn software.
3  * Gobelijn is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the
5  * Free Software Foundation, either version 3 of the License, or any later
6  * version. Gobelijn is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.
9  * See the GNU General Public License for details. You should have received
10  * a copy of the GNU General Public License along with the software. If not,
11  * see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2016, Jan Broeckhove.
14  */
20 // This program illustrates the abstract factory pattern.
21 // The abstract factory class template in 'factory.h' has three implementations:
22 //
23 // * ConstantFactory, which always returns the same pre-defined value.
24 // ('constant-factory.h')
25 //
26 // * StdinEmployeeFactory, which uses standard output to ask the user
27 // specific questions, and constructs a new Employee instance based on
28 // the user's answers, which are read from standard input.
29 // ('stdin-employee-factory.h', 'stdin-employee-factory.cpp')
30 //
31 // * PtreeEmployeeFactory, which extracts data from Boost property trees,
32 // and uses that to build Employee instances. ('ptree-employee-factory.h',
33 // 'ptree-employee-factory.cpp')
34 //
35 // These implementations are used in this program to construct employees,
36 // and print their data.
37 
38 #include "constant-factory.h"
39 #include "employee.h"
40 #include "ptree-employee-factory.h"
41 #include "stdin-employee-factory.h"
42 
43 #include <boost/property_tree/xml_parser.hpp>
44 #include <iostream>
45 
46 using namespace AbstractFactory;
47 
52 void printEmployee(const Employee& employee)
53 {
54  std::cout << "name: " << employee.Name << std::endl
55  << "department: " << employee.DepartmentName << std::endl
56  << "salary: " << employee.Salary << std::endl;
57 }
58 
63 void printEmployee(Factory<Employee>& factory) { printEmployee(factory.Create()); }
64 
68 boost::property_tree::ptree toPtree(const Employee& employee)
69 {
70  boost::property_tree::ptree result;
71  result.put(PtreeEmployeeFactory::EmployeeNameKey, employee.Name);
73  result.put(PtreeEmployeeFactory::EmployeeSalaryKey, employee.Salary);
74  return result;
75 }
76 
77 int main()
78 {
79  // Create a constant employee.
80  std::cout << "Predefined employee:" << std::endl;
81 
82  Employee constEmployee;
83  constEmployee.Name = "John Doe";
84  constEmployee.DepartmentName = "IT";
85  constEmployee.Salary = 2500;
86 
87  ConstantFactory<Employee> constFactory(constEmployee);
88  printEmployee(constFactory);
89 
90  // Create an employee from standard input.
91  std::cout << "Build your own employee:" << std::endl;
92  StdinEmployeeFactory stdinFactory;
93  auto userEmployee = stdinFactory.Create();
94  printEmployee(userEmployee);
95 
96  // Now, we'll read an employee from an XML file. To keep things simple,
97  // we'll just serialize the employee the user just described, write it
98  // to disk as XML, and then read that again.
99 
100  std::cout << "XML employee:" << std::endl;
101  boost::property_tree::xml_parser::write_xml("tmp_config.xml", toPtree(userEmployee));
102  boost::property_tree::ptree pt;
103  boost::property_tree::xml_parser::read_xml("tmp_config.xml", pt);
104  PtreeEmployeeFactory ptreeFactory(pt);
105  printEmployee(ptreeFactory);
106 }
double Salary
Stores the employee's monthly wage.
Definition: employee.h:34
void printEmployee(const Employee &employee)
Writes to standard output all information that pertains to the given employee.
Employee Create() final
Instructs this factory to create a value.
std::string DepartmentName
Stores the name of the employee's department.
Definition: employee.h:33
An employee factory implementation that uses standard input to construct employee instances...
std::string Name
Stores the person's name.
Definition: employee.h:32
int main()
boost::property_tree::ptree toPtree(const Employee &employee)
Creates a Boost property tree that represents the given employee instance.
static constexpr const char * EmployeeNameKey
The Boost property tree key for the employees' names.
Employee POD.
A factory class template that always returns a constant, pre-defined value.
A data structure that represents an employee's data.
Definition: employee.h:28
static constexpr const char * EmployeeDepartmentNameKey
The Boost property tree key for the employees' department names.
Concrete ptree based factory.
An employee factory implementation that uses Boost property trees to construct employee instances...
Concrete stdin input based factory.
static constexpr const char * EmployeeSalaryKey
The Boost property tree key for the employees' salaries.
Constant Factory.
virtual TResult Create(TArgs...args)=0
Instructs this factory to create a value.