Currently, in order to learn design patterns, I am reading "Introduction to Design Patterns Learned in the Augmented and Revised Java Language", which is highly regarded as a book on design patterns. However, literally, the design patterns of this book are written in Java, so I rewrote the Java code in this book to C ++ for my own learning that mainly uses C ++. The original Java code can be downloaded from here. This time, I wrote the C ++ version code of Template Method. (* 2018/11/21 Fixed a bug that caused a memory leak in the abstractDisplay destructor *)
TemplateMethod is a pattern in which classes with similar functions are grouped together and the superclass determines the processing flow. For example, the template function of PowerPoint has the same work of creating the same slide, and I think that each template has a slightly different design.
~~ Actually, the argument type of the constructor should be char, but it's annoying and the essence does not change, so I set it to std :: string. It's omission to do so. ~~ Fixed to work properly with char type.
CharDisplay.cpp
#include"CharDisplay.h"
CharDisplay::CharDisplay(const char *ch) : ch(ch){}
void CharDisplay::open() {
std::cout << "<<" ;
}
void CharDisplay::print() {
std::cout << ch;
}
void CharDisplay::close() {
std::cout << ">>" << std::endl;
}
CharDisplay.h
#pragma once
#include"AbstractDisplay.h"
class CharDisplay : public AbstractDisplay {
private:
const char *ch;
public:
CharDisplay(const char *ch);
void open();
void print();
void close();
};
StringDisplay.cpp
#include"StringDisplay.h"
StringDisplay::StringDisplay(std::string string) :string(string), width(string.size()) {}
void StringDisplay::open()
{
printLine();
}
void StringDisplay::print()
{
std::cout << "|" + string + "|" << std::endl;
}
void StringDisplay::close()
{
printLine();
}
void StringDisplay::printLine()
{
std::cout << "+";
for (int i = 0; i < width; i++) {
std::cout << "-";
}
std::cout << "+" << std::endl;
}
StringDisplay.h
#pragma once
#include"AbstractDisplay.h"
#include<string>
class CharDisplay : public AbstractDisplay {
private:
std::string ch;
public:
CharDisplay(std::string Newch);
void open();
void print();
void close();
};
Here we used unique_ptr to reproduce Java garbage collection. Also, since final is used in the original Java code, we also use final added from C ++ 11.
AbstractDisplay.cpp
#include"AbstractDisplay.h"
#include"StringDisplay.h"
#include"CharDisplay.h"
void AbstractDisplay::display()
{
open();
for (int i = 0; i < 5; i++) {
print();
}
close();
}
int main() {
std::unique_ptr<AbstractDisplay> d1(new CharDisplay("H"));
std::unique_ptr<AbstractDisplay> d2(new StringDisplay("hello world"));
std::unique_ptr<AbstractDisplay> d3(new StringDisplay("Hello"));
d1->display();
d2->display();
d3->display();
}
AbstractDisplay.h
#pragma once
#include<iostream>
#include<string>
#include<memory>
class AbstractDisplay {
public:
virtual ~AbstractDisplay() {};
virtual void open() = 0;
virtual void print() = 0;
virtual void close() = 0;
virtual void display() final;
};
Since this article focused on rewriting the original code in C ++, the explanation of the design pattern itself has been cut off. If you want to know more about the design pattern and the explanation of the original code, we recommend "Introduction to Design Patterns Learned in the Augmented and Revised Java Language" that contains the original code. We hope this article helps C ++ learners struggling with design patterns.
Recommended Posts