Skip to content

Instantly share code, notes, and snippets.

@Facon
Last active June 5, 2016 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Facon/5c4eafd737332654672f03de57190104 to your computer and use it in GitHub Desktop.
Save Facon/5c4eafd737332654672f03de57190104 to your computer and use it in GitHub Desktop.
Policy-based design example
#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";
}
};
#ifndef NOTIFICATION_H
#define NOTIFICATION_H
#include <string>
class Notification
{
public:
virtual void notify(const std::string& mSubject) = 0;
};
#endif // NOTIFICATION_H
#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");
}
#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
#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