Southwind : Gof Patterns example of Template method and Strategy patterns

Southwind C++ (codeblocks ide) project for GoF patterns practicing. It takes as example a possible template method from the trader company "Southwind" and the use of 2 different Strategies (Kanban and JustInTime).

Files in project:

main.cpp

:

#include
#include "templatemethod.h"
#include

using namespace std;

int main()
{
const static vector * catalog = {"Tablets offer", "SmartPhones offer", "Javas offer", "Androids offer", "Creatives offer"};
const static vector * locations = {"KanbanCity","JustInTimeCity"};

int status = -1;
map> * tradingdata;
Strategy * strategy = NULL;
Context* context = NULL;
TraderCompany* southwind = NULL;


vector * preorder = createPreOrder(catalog);
string* location = getLocation(locations);

if(location->compare(locations->at(0))== 0))
/* we work with Kanban city */
strategy = new KanbanStrategy();
else
strategy = new JustinStrategy();

context = new Context(strategy);


southwind = new Southwind(context);
/* call to the template method */
status = southwind->trade(preorder);


tradingdata = southwind->getTraderdata();


return status;






}



vector* createPreOrder(vector* catalog) {

vector * selecteds(-1,5);
vector * preOrder ;

vector::iterator it = catalog->begin();
int i = 1;
int j = 0;
bool exit = false;

cout << "Welcome to Southwind company please choose our offers : ";
cout << endl << endl;
cout << "Catalog :" << endl;
while(it!= catalog->end()) {
cout << "Number: " << i << " " << "Product: " << *it << " " << endl;
it++;
i++;
}

i = 100;
cout << "Choose your offers : (input correct offer's number (1-" << catalog->size() << ") or 0 number for exit) " << endl;

while(i!= 0 && !exit){
cout << "Offer's number: " << endl;
cin >> i;

if(i>0 && i< catalog->size())
selecteds->push_back( i--);
else{
j++;
cout << " Input number is incorrect (1-6)" << endl;
i = 100;
if(j==10){
cout << "Too many incorrect input, exiting..." << endl;
exit = true;
}

}

it = selecteds->begin();
while(it!= selecteds->end()){
preOrder->push_back( catalog[ *(it++)]);

}

return preOrder;
}

string * getLocation( vector* locations) {


string * loc;

vector::iterator it = locations->begin();
int i = 1;
int j = 0;
int k = 0;

cout << "Welcome to Southwind, this are the possible locations:";
cout << endl << endl;
cout << "Location :" << endl;
while(it!= locations->end()) {
cout << "Number: " << i << " " << "Location : " << *it << " " << endl;
it++;
i++;
}

i = 100;

cout << "Choose your location : (input correct location's number (1-" << locations->size() << ") or 0 number for exit) " << endl;


while(i!= 0 && k>1){
cout<< "Location's number: " << endl;
cin >> i;

if(i>0 && i< locations->size()){
selecteds->push_back( i--);
k++;
}
else{
j++;
cout << " Input number is incorrect (1-" << localtions->size() << ")" << endl;
i = 100;
if(j==10){
cout << "Too many incorrect input, exiting..." << endl;
exit = true;
}

}

}

it = selecteds->begin();
while(it!= selecteds->end())
*loc = locations[ *(it++)]);


return loc;

}








templatemethod.h

: It contains template method implementation and strategy pattern dependency.

#ifndef TEMPLATEMETHOD_H_INCLUDED
#define TEMPLATEMETHOD_H_INCLUDED

#include
#include
#include "strategy.h"
#include
#include


/**
Southwind trading company

*/

typedef map> TradeData;

class TraderCompany {
public:
TraderCompany(Context* c);
~TraderCompany();
int trade(vector* preorder);
virtual int takeOrder();
virtual int prepareOrder();
virtual int computePrice();
virtual int doShipping();
virtual int doConfirm();
map>* getTraderdata();
int doReport();
private:
Context* _context;
map> * _tradedata;
vector _preorder;
};

TraderCompany::TraderCompany(Context* c){
this->_context = c;
}
int TraderCompany::trade(vector * preorder) {
this->_preorder = preorder;
this->takeOrder();
this->prepareOrder();
this->computePrice();
this->doShipping();

return this->doConfirm();

}
int TraderCompany::doConfirm() {
cout << " Operation confirm!!";
return this->doReport();
}
map>* TraderCompany::getTraderdata() {
return this->_tradedata;
}

class Southwind: public TraderCompany {
public:
Southwind(Context * c);
~Southwind();
int trade(vector * preorder);
virtual int takeOrder();
virtual int prepareOrder();
virtual int computePrice();
virtual int doShipping();
virtual int doConfirm();
int doReport();
private:
Context* _context;
map> * _tradedata;
vector * _preorder;
};


#endif // TEMPLATEMETHOD_H_INCLUDED

strategy.h

: specific strategies to be implemented

#ifndef STRATEGY_H_INCLUDED
#define STRATEGY_H_INCLUDED
#include
#include
#include
#include

using namespace std;

typedef map> * TradeData;

class Strategy {
public:
Strategy();
~Strategy();
virtual int create(vector * input);
virtual int prepare();
virtual int ship();
virtual int confirm();
map> * getData();
private:
map> * _data;
};


class JustinStrategy: public Strategy {

};

class KanbanStrategy: public Strategy {


};


class Context {
public:
Context(Strategy * s);
~Context();
virtual int createOrder(vector * input);
virtual int prepareOrder();
virtual int ship();
virtual int confirm();
map> * getOrder();
private:
Strategy * _strategy;
map>* _order;

};



#endif // STRATEGY_H_INCLUDED

Download project from Github

Comments

Popular posts from this blog

3D with POV-Ray, isosurfaces example

Algorithms: Backpack problem, Greedy , Dynamic Programming techniques (C++)

Classic Email, Web mail application refactory for visual effects (software engineering-efficiency-UX)