Another example of the decorator design pattern

Implementation

  • IComponent -> IText
  • Client ->TestDecoratorOfText
  • Component -> CipherText
  • Decorator -> PlainText, AsteriskCipherText
namespace ExampleOfDecoratorPattern
{
    public interface IText
    {
        string Text { get; }
    }
}
using System;

namespace ExampleOfDecoratorPattern
{
    public class CipherText : IText
    {
        private readonly string _text;

        public CipherText(string text)
        {
            this._text = text;
        }

        #region IText Members

        public string Text
        {
            get { return EncryptSimple(this._text); }
        }

        #endregion

        private static string EncryptSimple(string text)
        {
            string result = String.Empty;

            int length = text.Length;
            for (int i = 0; i < length; i++)
            {
                result = String.Concat(result, text[i], text[length - i - 1]);
            }

            return result;
        }
    }
}

using System;

namespace ExampleOfDecoratorPattern
{
public class PlainText : IText
{
private readonly IText _cipherText;

public PlainText(IText text)
{
this._cipherText = text;
}

#region IText Members

public string Text
{
get { return DecryptSimple(this._cipherText.Text); }
}

#endregion

private static string DecryptSimple(string text)
{
string result = String.Empty;

int length = text.Length;
for (int i = 0; i < length; i += 2)
{
result = String.Concat(result, text[i]);
}

return result;
}
}
}

using System;

namespace ExampleOfDecoratorPattern
{
public class AsteriskCipherText : IText
{
private readonly IText _cipherText;

public AsteriskCipherText(IText text)
{
this._cipherText = text;
}

#region IText Members

public string Text
{
get
{
string result = String.Empty;

for (int i = 0; i < this._cipherText.Text.Length/2; i++)
{
result = String.Concat(result, "*");
}

return result;
}
}

#endregion
}
}

using System;

namespace ExampleOfDecoratorPattern
{
public class TestDecoratorOfText
{
public const string Text = "it is plain text";

private static void Main()
{
IText cipherText = new CipherText(Text);
IText showCipherPlainText = new AsteriskCipherText(cipherText);
IText plainText = new PlainText(cipherText);

Console.WriteLine("Original: \t\t{0}", Text);
Console.WriteLine("Asterisk: \t\t{0}", showCipherPlainText.Text);
Console.WriteLine("Cipher: \t\t{0}", cipherText.Text);
Console.WriteLine("Again original: \t{0}", plainText.Text);
}
}
}

Output:

Original:               it is plain text
Asterisk:               ****************
Cipher:                 ittx eits  npilaalipn  stie xtti
Again original:         it is plain text
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s