Last active
June 5, 2016 20:01
-
-
Save Facon/5c4eafd737332654672f03de57190104 to your computer and use it in GitHub Desktop.
Policy-based design example
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 "Notification.h" | |
#include <string> | |
#include <iostream> | |
class MailNotification : public Notification | |
{ | |
public: | |
void notify(const std::string& mSubject) override | |
{ | |
std::string show("Mail "); | |
show.append(mSubject); | |
std::cout << show << "\n"; | |
} | |
}; |
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 NOTIFICATION_H | |
#define NOTIFICATION_H | |
#include <string> | |
class Notification | |
{ | |
public: | |
virtual void notify(const std::string& mSubject) = 0; | |
}; | |
#endif // NOTIFICATION_H |
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 "User.h" | |
#include "WAppNotification.h" | |
#include "MailNotification.h" | |
int main() | |
{ | |
User<WAppNotification> user; | |
user.send("primer mensaje"); | |
User<MailNotification> user2; | |
user2.send("segundo mensaje"); | |
} |
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 USER_H | |
#include "Notification.h" | |
#include <string> | |
template<class T> | |
class User | |
{ | |
private: | |
T _notification; | |
public: | |
User() | |
{ | |
static_assert(std::is_base_of<Notification, T>::value, "T tiene que heredar de Notificación."); | |
} | |
~User() | |
{ | |
} | |
void send(const std::string& mSubject) | |
{ | |
//Solo envia las notificaciones bien a Mail | |
_notification.notify(mSubject); | |
} | |
}; | |
#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 WAPPNOTIFICATION_H | |
#define WAPPNOTIFICATION_H | |
#include "Notification.h" | |
#include <string> | |
#include <iostream> | |
class WAppNotification : public Notification | |
{ | |
public: | |
void notify(const std::string& mSubject) override | |
{ | |
std::string show("Whatsapp "); | |
show.append(mSubject); | |
std::cout << show << "\n"; | |
} | |
}; | |
#endif // WAPPNOTIFICATION_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment