Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
84 lines (65 sloc) 2.89 KB
// Decorator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
class IComponent
{
public:
virtual float Cost() = 0;
virtual std::string Ingridients() { return ""; };
};
class Pizza : public IComponent
{
public:
float Cost()
{
return 10.f;
}
};
class PizzaDecorator : public IComponent
{
protected:
std::shared_ptr<IComponent> component;
public:
PizzaDecorator(const std::shared_ptr<IComponent>& component )
{
this->component = component;
}
};
class SalamiDecorator : public PizzaDecorator
{
public:
using PizzaDecorator::PizzaDecorator;
float Cost()
{
return component->Cost() + 4.5f;
}
std::string Ingridients()
{
return component->Ingridients() + "Salami ";
}
};
class CheeseDecorator : public PizzaDecorator
{
public:
using PizzaDecorator::PizzaDecorator;
float Cost()
{
return component->Cost() + 3.0f;
}
std::string Ingridients()
{
return component->Ingridients() + "Cheese ";
}
};
int main()
{
std::shared_ptr<IComponent> pizza ( new Pizza () );
pizza = std::shared_ptr<IComponent>( new SalamiDecorator( pizza ) );
pizza = std::shared_ptr<IComponent>( new CheeseDecorator( pizza ) );
pizza = std::shared_ptr<IComponent>( new CheeseDecorator( pizza ) );
std::cout<<pizza->Cost()<<std::endl;
std::cout << pizza->Ingridients() << std::endl;
std::cin.get();
}
You can’t perform that action at this time.