Skip to content

Commit

Permalink
Add first rough version of Template Method.
Browse files Browse the repository at this point in the history
  • Loading branch information
BartVandewoestyne committed Feb 27, 2024
1 parent 9fbab5d commit 2f0420c
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions Behavioral_Patterns/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(Command)
add_subdirectory(Iterator)
add_subdirectory(Observer)
add_subdirectory(Strategy)
add_subdirectory(Template_Method)
49 changes: 49 additions & 0 deletions Behavioral_Patterns/Template_Method/Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "Application.h"

#include "Document.h"
#include "DocumentStore.h"

#include <iostream>

Application::Application() {
_docs = new DocumentStore;
}

Application::~Application() {
delete _docs;
}

void Application::AddDocument() {
std::cout << "Adding document to application." << std::endl;
}

void Application::OpenDocument(const char* name) {
if (!CanOpenDocument(name)) {
// cannot handle this document
return;
}

Document* doc = DoCreateDocument();

if (doc) {
_docs->AddDocument(doc);
AboutToOpenDocument(doc);
doc->Open();
doc->DoRead();
}
}

Document* Application::DoCreateDocument() {
std::cout << "Creating a document." << std::endl;
Document* doc = new Document;
return doc;
}

bool Application::CanOpenDocument(const char* name) {
std::cout << "Checking whether we can open document " << name << "." << std::endl;
return true;
}

void Application::AboutToOpenDocument(const Document* doc) {
std::cout << "About to open document " << doc << "." << std::endl;
}
22 changes: 22 additions & 0 deletions Behavioral_Patterns/Template_Method/Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef APPLICATION_H
#define APPLICATION_H

class Document;
class DocumentStore;

class Application {
public:
Application();
virtual ~Application();

void AddDocument();
void OpenDocument(const char* name);

virtual Document* DoCreateDocument();
virtual bool CanOpenDocument(const char* name);
virtual void AboutToOpenDocument(const Document* doc);
private:
DocumentStore* _docs;
};

#endif // APPLICATION_H
10 changes: 10 additions & 0 deletions Behavioral_Patterns/Template_Method/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set(template_method_SRCS
Application.cpp
Document.cpp
DocumentStore.cpp)

add_library(template_method_lib SHARED ${template_method_SRCS})
target_include_directories(template_method_lib PUBLIC ./)

add_executable(template_method1 main1.cpp)
target_link_libraries(template_method1 template_method_lib)
6 changes: 6 additions & 0 deletions Behavioral_Patterns/Template_Method/Document.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Document.h"

void Document::Save() {}
void Document::Open() {}
void Document::Close() {}
void Document::DoRead() {}
12 changes: 12 additions & 0 deletions Behavioral_Patterns/Template_Method/Document.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef DOCUMENT_H
#define DOCUMENT_H

class Document {
public:
void Save();
void Open();
void Close();
virtual void DoRead();
};

#endif // DOCUMENT_H
7 changes: 7 additions & 0 deletions Behavioral_Patterns/Template_Method/DocumentStore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "DocumentStore.h"

#include <iostream>

void DocumentStore::AddDocument(const Document* doc) {
std::cout << "Adding document " << doc << " to the document store." << std::endl;
}
11 changes: 11 additions & 0 deletions Behavioral_Patterns/Template_Method/DocumentStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef DOCUMENT_STORE_H
#define DOCUMENT_STORE_H

class Document;

class DocumentStore {
public:
void AddDocument(const Document* doc);
};

#endif // DOCUMENT_STORE_H
7 changes: 7 additions & 0 deletions Behavioral_Patterns/Template_Method/main1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Application.h"

int main()
{
Application app;
app. OpenDocument("SomeDocument");
}

0 comments on commit 2f0420c

Please sign in to comment.