Scan with Phone

Scan to instantly open and share this page on your mobile device.

Link copied to clipboard!

Decorator

Wrap object to extend behavior while preserving interface.

C#

interface IText
{
    string Show();
}
class Plain : IText
{
    public string Show()
    {
        return "hello";
    }
}
abstract class Deco : IText
{
    protected IText inner;
    protected Deco(IText t)
    {
        inner = t;
    }
    public abstract string Show();
}
class Bold : Deco
{
    public Bold(IText t) : base(t) { }
    public override string Show()
    {
        return "" + inner.Show() + "";
    }
}
IText txt = new Bold(new Plain());
string s = txt.Show(); // hello
next