定义:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰者模式相比生成子类更加灵活。
UML类图:
C++实现:
#include<iostream>
#include<string>
using namespace std;
class Beverage
{
public:
string description = "Unkonw Beverage";
virtual string getDescription()
{
return description;
}
virtual double cost() = 0;
virtual ~Beverage() {}
};
class ComdimentDecorator :public Beverage
{
public :
virtual ~ComdimentDecorator() {}
};
class Espresso : public Beverage
{
public:
Espresso()
{
description = "Espressp Coffee";
}
double cost() override
{
return 1.99;
}
virtual ~Espresso() {}
};
class HouseBlend : public Beverage
{
public:
HouseBlend()
{
description = "House Blend Coffee";
}
double cost() override
{
return 0.89;
}
virtual ~HouseBlend() {}
};
class DarkRoast : public Beverage
{
public:
DarkRoast()
{
description = "Dark Roast Coffee";
}
double cost() override
{
return 0.99;
}
virtual ~DarkRoast() {}
};
class Decaf : public Beverage
{
public:
Decaf()
{
description = "Decaf Coffee";
}
double cost() override
{
return 1.05;
}
virtual ~Decaf() {}
};
class Mocha : public ComdimentDecorator
{
Beverage* beverage;
public:
Mocha(Beverage* beverage)
{
this->beverage = beverage;
}
double cost() override
{
return this->beverage->cost() + 0.2;
}
string getDescription() override
{
return this->beverage->getDescription() + ", Mocha";
}
virtual ~Mocha() {}
};
class Milk : public ComdimentDecorator
{
Beverage* beverage;
public:
Milk(Beverage* beverage)
{
this->beverage = beverage;
}
double cost() override
{
return this->beverage->cost() + 0.1;
}
string getDescription() override
{
return this->beverage->getDescription() + ", Milk";
}
virtual ~Milk() {}
};
class Soy : public ComdimentDecorator
{
Beverage* beverage;
public:
Soy(Beverage* beverage)
{
this->beverage = beverage;
}
double cost() override
{
return this->beverage->cost() + 0.15;
}
string getDescription() override
{
return this->beverage->getDescription() + ", Soy";
}
virtual ~Soy() {}
};
class Whip :public ComdimentDecorator
{
Beverage* beverage;
public:
Whip(Beverage* beverage)
{
this->beverage = beverage;
}
double cost() override
{
return this->beverage->cost() + 0.1;
}
string getDescription() override
{
return this->beverage->getDescription() + ", Whip";
}
virtual ~Whip() {}
};
int main()
{
Beverage* beverage = new Espresso();
cout << beverage->getDescription() << "\n" << beverage->cost() << endl;
Beverage* beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
cout << beverage2->getDescription() << "\n" << beverage2->cost() << endl;
Beverage* beverage3 = new HouseBlend();
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
cout << beverage3->getDescription() << "\n" << beverage3->cost() << endl;
return 0;
}
C#实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static System.Console;
namespace ConsoleApp1
{
public abstract class Beverage
{
public String description = "Unkonw Beverage";
virtual public String getDescription()
{
return description;
}
public abstract double cost();
}
public abstract class ComdimentDecorator :Beverage
{
}
public class Espresso : Beverage
{
public Espresso()
{
description = "Espressp Coffee";
}
public override double cost()
{
return 1.99;
}
}
public class HouseBlend : Beverage
{
public HouseBlend()
{
description = "House Blend Coffee";
}
public override double cost()
{
return 0.89;
}
}
public class DarkRoast : Beverage
{
public DarkRoast()
{
description = "Dark Roast Coffee";
}
public override double cost()
{
return 0.99;
}
}
public class Decaf : Beverage
{
public Decaf()
{
description = "Decaf Coffee";
}
public override double cost()
{
return 1.05;
}
}
public class Mocha : ComdimentDecorator
{
Beverage beverage;
public Mocha(Beverage beverage)
{
this.beverage = beverage;
}
public override double cost()
{
return this.beverage.cost()+0.2;
}
public override string getDescription()
{
return this.beverage.getDescription() + ", Mocha";
}
}
public class Milk : ComdimentDecorator
{
Beverage beverage;
public Milk(Beverage beverage)
{
this.beverage = beverage;
}
public override double cost()
{
return this.beverage.cost() + 0.1;
}
public override string getDescription()
{
return this.beverage.getDescription() + ", Milk";
}
}
public class Soy : ComdimentDecorator
{
Beverage beverage;
public Soy(Beverage beverage)
{
this.beverage = beverage;
}
public override double cost()
{
return this.beverage.cost() + 0.15;
}
public override string getDescription()
{
return this.beverage.getDescription() + ", Soy";
}
}
public class Whip : ComdimentDecorator
{
Beverage beverage;
public Whip(Beverage beverage)
{
this.beverage = beverage;
}
public override double cost()
{
return this.beverage.cost() + 0.1;
}
public override string getDescription()
{
return this.beverage.getDescription() + ", Whip";
}
}
class Program
{
static void Main(string[] args)
{
Beverage beverage = new Espresso();
WriteLine(beverage.getDescription() + "\n" + beverage.cost()+"\n");
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
WriteLine(beverage2.getDescription() + "\n" + beverage2.cost()+"\n");
Beverage beverage3 = new HouseBlend();
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
WriteLine(beverage3.getDescription() + "\n" + beverage3.cost()+"\n");
}
}
}