Created
June 5, 2021 18:56
-
-
Save alf-p-steinbach/8faeadf8b3a800bde99690ad4932169b to your computer and use it in GitHub Desktop.
Visitor pattern in separate files
This file contains hidden or 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 "Visitable.hpp" | |
struct Cat: Visitable | |
{ | |
void hunt_birds() { throw "Hunting birds..."; } | |
void accept( Doer& doer ) override { doer.deal_with( *this ); } | |
}; |
This file contains hidden or 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 "Cat.hpp" | |
auto create_cat() -> Visitable* { return new Cat; } |
This file contains hidden or 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 "Person.hpp" | |
auto create_person() -> Doer* { return new Person; } |
This file contains hidden or 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
#pragma once | |
struct Cat; | |
struct Phone; | |
struct Doer | |
{ | |
virtual ~Doer() {} | |
virtual void deal_with( Cat& ) = 0; | |
virtual void deal_with( Phone& ) = 0; | |
}; |
This file contains hidden or 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 "Visitable.hpp" | |
extern auto create_person() -> Doer*; | |
extern auto create_cat() -> Visitable*; | |
#include <stdio.h> | |
#include <memory> | |
using std::unique_ptr; | |
auto main() -> int | |
{ | |
using C_str = const char*; | |
try { | |
auto doer = unique_ptr<Doer>( create_person() ); | |
auto object = unique_ptr<Visitable>( create_cat() ); | |
object->accept( *doer ); // "Hunting birds..." | |
} catch( const C_str s ) { | |
printf( "%s\n", s ); | |
} | |
} |
This file contains hidden or 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 "Doer.hpp" // Not really necessary but to be clear. | |
#include "Cat.hpp" | |
#include "Phone.hpp" | |
struct Person: Doer | |
{ | |
void deal_with( Cat& cat ) override { cat.hunt_birds(); } | |
void deal_with( Phone& phone ) override { phone.call_999(); } | |
}; |
This file contains hidden or 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 "Visitable.hpp" | |
struct Phone: Visitable | |
{ | |
void call_999() { throw "Calling 999!"; } | |
void accept( Doer& doer ) override { doer.deal_with( *this ); } | |
}; |
This file contains hidden or 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
#pragma once | |
#include "Doer.hpp" | |
struct Visitable | |
{ | |
virtual ~Visitable() {} | |
virtual void accept( Doer& doer ) = 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment