08_PTRN: Weitere Änderungen am Grundgerüst
This commit is contained in:
parent
dd69b74ea2
commit
b37de1bd06
|
@ -1 +1 @@
|
|||
add_executable(08_PTRN_MP MP/Money.cpp MP/Money.h MP/Currency.cpp MP/Currency.h MP/maín.cpp MP/BankAccount.cpp MP/BankAccount.h MP/Storage.cpp MP/Storage.h)
|
||||
add_executable(08_PTRN_MP MP/Money.cpp MP/Money.h MP/Currency.cpp MP/Currency.h MP/main.cpp MP/BankAccount.cpp MP/BankAccount.h MP/Storage.cpp MP/Storage.h)
|
|
@ -1,5 +1,7 @@
|
|||
#include <utility>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "BankAccount.h"
|
||||
|
||||
BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(std::move(money)){}
|
||||
BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(money){}
|
||||
|
|
|
@ -12,7 +12,7 @@ private:
|
|||
Money money;
|
||||
|
||||
public:
|
||||
BankAccount(std::string owner, Money money);
|
||||
BankAccount(std::string owner = "none", Money money = Money(0, Storage().USD));
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ public:
|
|||
float rate;
|
||||
Currency(std::string name, float rate);
|
||||
bool operator==(Currency ¤cy);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -9,8 +9,12 @@
|
|||
|
||||
class Storage {
|
||||
public:
|
||||
std::unordered_set<Currency> currencies;
|
||||
std::unordered_map<std::string, BankAccount> accounts;
|
||||
static const Currency USD = Currency("US Dollar", 1);
|
||||
static const Currency EUR = Currency("Euro", 0.879699);
|
||||
static const Currency GBP = Currency("British Pound", 0.783667);
|
||||
static const Currency AUD = Currency("Australian Dollar", 1.36962);
|
||||
static const Currency CAD = Currency("Canadian Dollar", 1.33023);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,71 +1,13 @@
|
|||
#include "Car.hpp"
|
||||
#include "Truck.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include "VehicleFactory.hpp"
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Currency.h"
|
||||
#include "BankAccount.h"
|
||||
#include "Storage.h"
|
||||
|
||||
void workOnCopy_naive(std::vector<Vehicle*> vehicles){
|
||||
std::vector<Vehicle*> tempVec;
|
||||
for (int i=0; i<vehicles.size(); i++){
|
||||
Vehicle * currentVehicle = vehicles[i];
|
||||
Car* carToCopy = dynamic_cast<Car*>(currentVehicle); // RTTI type check
|
||||
Truck* truckToCopy = dynamic_cast<Truck*>(currentVehicle);// may be nullptr
|
||||
if(carToCopy){
|
||||
tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car
|
||||
}else if(truckToCopy){
|
||||
tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy
|
||||
}else{
|
||||
// TODO: what do we do here?
|
||||
// Did someone add a new class to the code base?
|
||||
return; // BUG: copies aren't free'd
|
||||
}
|
||||
}
|
||||
// work on copies ...
|
||||
// ...
|
||||
for(auto vehi : tempVec){
|
||||
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
|
||||
}
|
||||
for (int i=0; i<tempVec.size(); i++){
|
||||
delete tempVec[i];
|
||||
}
|
||||
tempVec.clear();
|
||||
}
|
||||
Storage storage;
|
||||
|
||||
void workOnCopy_smart(std::vector<Vehicle*> vehicles){ // uses RAII, virtual ctor
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
class RAIIvector { // local RAII helper class
|
||||
public:
|
||||
std::vector<Vehicle*> tempVec;
|
||||
~RAIIvector(){
|
||||
for(auto vehi : tempVec){
|
||||
delete vehi;
|
||||
}
|
||||
tempVec.clear();
|
||||
}
|
||||
};
|
||||
RAIIvector rv;
|
||||
|
||||
for(auto vehi : vehicles){
|
||||
rv.tempVec.push_back(vehi->clone());
|
||||
}
|
||||
// work on copies ...
|
||||
// ...
|
||||
for(auto vehi : rv.tempVec){
|
||||
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
|
||||
}
|
||||
// compiler WILL invoke RAIIvector::~RAIIvector() here
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
std::vector<Vehicle*> vehicles;
|
||||
vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction
|
||||
vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000));
|
||||
Logger::getInstance()->log("work naively", __FILE__, __LINE__);
|
||||
workOnCopy_naive(vehicles);
|
||||
Logger::getInstance()->log("work smartly", __FILE__, __LINE__);
|
||||
workOnCopy_smart(vehicles);
|
||||
return 0;
|
||||
storage.accounts.insert(BankAccount("Max Maier", Money(50, Storage().EUR)));
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
#include "Car.hpp"
|
||||
#include "Truck.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include "VehicleFactory.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void workOnCopy_naive(std::vector<Vehicle*> vehicles){
|
||||
std::vector<Vehicle*> tempVec;
|
||||
for (int i=0; i<vehicles.size(); i++){
|
||||
Vehicle * currentVehicle = vehicles[i];
|
||||
Car* carToCopy = dynamic_cast<Car*>(currentVehicle); // RTTI type check
|
||||
Truck* truckToCopy = dynamic_cast<Truck*>(currentVehicle);// may be nullptr
|
||||
if(carToCopy){
|
||||
tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car
|
||||
}else if(truckToCopy){
|
||||
tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy
|
||||
}else{
|
||||
// TODO: what do we do here?
|
||||
// Did someone add a new class to the code base?
|
||||
return; // BUG: copies aren't free'd
|
||||
}
|
||||
}
|
||||
// work on copies ...
|
||||
// ...
|
||||
for(auto vehi : tempVec){
|
||||
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
|
||||
}
|
||||
for (int i=0; i<tempVec.size(); i++){
|
||||
delete tempVec[i];
|
||||
}
|
||||
tempVec.clear();
|
||||
}
|
||||
|
||||
void workOnCopy_smart(std::vector<Vehicle*> vehicles){ // uses RAII, virtual ctor
|
||||
|
||||
class RAIIvector { // local RAII helper class
|
||||
public:
|
||||
std::vector<Vehicle*> tempVec;
|
||||
~RAIIvector(){
|
||||
for(auto vehi : tempVec){
|
||||
delete vehi;
|
||||
}
|
||||
tempVec.clear();
|
||||
}
|
||||
};
|
||||
RAIIvector rv;
|
||||
|
||||
for(auto vehi : vehicles){
|
||||
rv.tempVec.push_back(vehi->clone());
|
||||
}
|
||||
// work on copies ...
|
||||
// ...
|
||||
for(auto vehi : rv.tempVec){
|
||||
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
|
||||
}
|
||||
// compiler WILL invoke RAIIvector::~RAIIvector() here
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
std::vector<Vehicle*> vehicles;
|
||||
vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction
|
||||
vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000));
|
||||
Logger::getInstance()->log("work naively", __FILE__, __LINE__);
|
||||
workOnCopy_naive(vehicles);
|
||||
Logger::getInstance()->log("work smartly", __FILE__, __LINE__);
|
||||
workOnCopy_smart(vehicles);
|
||||
return 0;
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Currency.h"
|
||||
#include "BankAccount.h"
|
||||
#include "Storage.h"
|
||||
|
||||
Storage storage;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
storage.currencies.insert("USD", Currency("US Dollar", 1));
|
||||
storage.currencies.insert("EUR", Currency("Euro", 0.879699));
|
||||
storage.currencies.insert("GBP", Currency("British Pound", 0.783667));
|
||||
storage.currencies.insert("AUD", Currency("Australian Dollar", 1.36962));
|
||||
storage.currencies.insert("CAD", Currency("Canadian Dollar", 1.33023));
|
||||
|
||||
|
||||
storage.accounts.insert(BankAccount("Max Maier", Money(50, storage.currencies["EUR"])));
|
||||
}
|
Loading…
Reference in New Issue