Created
November 26, 2017 14:54
-
-
Save missingno15/5384aca854d5cae78a34f5f6cb7cf9ee to your computer and use it in GitHub Desktop.
Demonstration of Dynamic Binding through Strategy Pattern in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include "article.h" | |
#include "formattable.h" | |
Article::Article(std::string t, std::vector<std::string> te) { | |
title = t; | |
text = te; | |
}; | |
void Article::publish(Article &article, Formattable &formatter) { | |
std::cout << formatter.generate_content(article) << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ARTICLE_H | |
#define ARTICLE_H | |
#include <string> | |
#include <vector> | |
#include "formattable.h" | |
class Formattable; // defined to resolve circular dependency | |
class Article { | |
public: | |
Article(std::string, std::vector<std::string>); | |
std::string title; | |
std::vector<std::string> text; | |
static void publish(Article&, Formattable&); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FORMATTABLE_H | |
#define FORMATTABLE_H | |
#include <string> | |
#include "article.h" | |
class Article; // defined to resolve circular dependency | |
class Formattable { | |
public: | |
virtual std::string generate_content(Article&){}; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include "article.h" | |
#include "html_formatter.h" | |
HTMLFormatter::HTMLFormatter(){}; | |
std::string HTMLFormatter::generate_content(Article &article) { | |
std::string body = ""; | |
std::string html = ""; | |
for(int i = 0; i < article.text.size(); i++) { | |
body += "<p>" + article.text[i] + "</p>\n"; | |
} | |
html += "<html>\n"; | |
html += "<head>\n"; | |
html += "<title>" + article.title + "<title>\n"; | |
html += "</head>\n"; | |
html += "<body>\n"; | |
html += body; | |
html += "</body>\n"; | |
html += "</html>"; | |
return html; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HTML_FORMATTER_H | |
#define HTML_FORMATTER_H | |
#include <string> | |
#include "formattable.h" | |
#include "article.h" | |
class HTMLFormatter : public Formattable { | |
public: | |
HTMLFormatter(); | |
std::string generate_content(Article&); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "formattable.h" | |
#include "article.h" | |
#include "html_formatter.h" | |
#include "plain_text_formatter.h" | |
int main() { | |
std::vector<std::string> text = { | |
"I keep on dying again.", | |
"Veins collapse, opening like the", | |
"Small fists of sleeping", | |
"Children.", | |
"Memory of old tombs,", | |
"Rotting flesh and worms do", | |
"Not convince me against", | |
"The challenge.", | |
"The years", | |
"And cold defeat live deep in", | |
"Lines along my face.", | |
"They dull my eyes, yet", | |
"I keep on dying,", | |
"Because I love to live." | |
}; // by Mary Angelou | |
Article article = Article("The Lesson", text); | |
HTMLFormatter htmlFormatter = HTMLFormatter(); | |
PlainTextFormatter plainTextFormatter = PlainTextFormatter(); | |
// Strategy for HTML | |
Article::publish(article, htmlFormatter); | |
// Separate output | |
std::cout << std::endl; | |
// Strategy for plain text | |
Article::publish(article, plainTextFormatter); | |
return 0; | |
} | |
// $ g++ -std=c++11 *.cc && ./a.out | |
// | |
// => | |
// <html> | |
// <head> | |
// <title>The Lesson<title> | |
// </head> | |
// <body> | |
// <p>I keep on dying again.</p> | |
// <p>Veins collapse, opening like the</p> | |
// <p>Small fists of sleeping</p> | |
// <p>Children.</p> | |
// <p>Memory of old tombs,</p> | |
// <p>Rotting flesh and worms do</p> | |
// <p>Not convince me against</p> | |
// <p>The challenge.</p> | |
// <p>The years</p> | |
// <p>And cold defeat live deep in</p> | |
// <p>Lines along my face.</p> | |
// <p>They dull my eyes, yet</p> | |
// <p>I keep on dying,</p> | |
// <p>Because I love to live.</p> | |
// </body> | |
// </html> | |
// | |
// The Lesson | |
// ********************************************* | |
// I keep on dying again. | |
// Veins collapse, opening like the | |
// Small fists of sleeping | |
// Children. | |
// Memory of old tombs, | |
// Rotting flesh and worms do | |
// Not convince me against | |
// The challenge. | |
// The years | |
// And cold defeat live deep in | |
// Lines along my face. | |
// They dull my eyes, yet | |
// I keep on dying, | |
// Because I love to live. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "plain_text_formatter.h" | |
#include "article.h" | |
PlainTextFormatter::PlainTextFormatter(){}; | |
std::string PlainTextFormatter::generate_content(Article &article){ | |
std::string body = ""; | |
for(int i = 0; i < article.text.size(); i++) { | |
body += article.text[i] + "\n"; | |
} | |
return | |
article.title + "\n" + | |
"*********************************************\n" + | |
body; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PLAIN_TEXT_FORMATTER_H | |
#define PLAIN_TEXT_FORMATTER_H | |
#include <string> | |
#include "article.h" | |
#include "formattable.h" | |
class PlainTextFormatter: public Formattable { | |
public: | |
PlainTextFormatter(); | |
std::string generate_content(Article&); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment