.NET/설계 패턴(C#)

[설계 패턴 C#] 17. 해석자 패턴(Interpreter Pattern)

언제나휴일 2016. 12. 7. 00:55
반응형

[설계 패턴 C#] 17. 해석자 패턴(Interpreter Pattern)


Interpreter.zip



"본문 내용"

[Escort GoF의 디자인 패턴 C#] 17. 해석자 패턴(Interpreter Pattern)

[Escort GoF의 디자인 패턴 C#] 17. 해석자 패턴(Interpreter Pattern) 설계

[Escort GoF의 디자인 패턴 C#] 17. 해석자 패턴(Interpreter Pattern) 구현

     ▶ Picture.cs

using System;

namespace Interpreter

{

    class Picture

    {

        string name;

        int tone;

        int brightness;

        int saturation;

        public Picture(string name,int tone,int brightness,int saturation)

        {

            this.name = name;

            this.tone = tone;

            this.brightness = brightness;

            this.saturation = saturation;

        }

        public void ChangeTone(int tone){    this.tone += tone;    }

        public void ChangeBrightness(int brightness){   this.brightness += brightness;    }

        public void ChangeSaturation(int saturation){    this.saturation += saturation;   }

        public void View()

        {

            Console.WriteLine("사진 파일명:{0}",name);

            Console.WriteLine("    색조:{0} 명도:{1} 채도:{2}", tone, brightness, saturation);

        }

    }

}


▶ Expression.cs

namespace Interpreter

{

    abstract class Expression

    {

        public Expression Next{    get;    set;    }

        public Expression() {   Next = null;   }

        public abstract string Interpret(string context);

        public virtual void DoItWithPicture(Picture picture)

        {

            if(Next!=null)

            {

                Next.DoItWithPicture(picture);

            }

        }

        protected string NextInterpret(string context)

        {

            if(Next!=null)

            {

                return Next.Interpret(context);

            }

            return context;

        }

        protected int GetNumber(string context, int index)

        {

            int index2 = -1;

            index2 = context.IndexOf(";", index);

            string re = context.Substring(index + 1, index2 - index - 1);

            try

            {

                return int.Parse(re);

            }

            catch{    }

            return 0;

        }

    }

}


▶ 
ToneExpression.cs

namespace Interpreter

{

    class ToneExpression:Expression

    {

        int value;

        public override string Interpret(string context)

        {

            value = 0;

            int index = -1;           

            string be = "";

            string af = "";           

            while ((index = context.IndexOf("T")) != -1)

            {               

                value += GetNumber(context, index);               

                if (index > 0)

                {

                    be = context.Substring(0, index);

                }

                else

                {

                    be = "";

                }

                index = context.IndexOf(";", index);

                af = context.Substring(index + 1);

                context = be + af;

            }

            return NextInterpret(context);

        }

        public override void DoItWithPicture(Picture picture)

        {

            picture.ChangeTone(value);

            base.DoItWithPicture(picture);

        }

    }

}


▶ BrightExpression.cs

namespace Interpreter

{

    class BrighExpression:Expression

    {

        int value;

        public override string Interpret(string context)

        {

            value = 0;

            int index = -1;           

            string be = "";

            string af = "";

            while ((index = context.IndexOf("B")) != -1)

            {               

                value += GetNumber(context, index);

                if (index > 0)

                {

                    be = context.Substring(0, index);

                }

                else

                {

                    be = "";

                }

                index = context.IndexOf(";", index);

                af = context.Substring(index + 1);

                context = be + af;

            }

            return NextInterpret(context);

        }

        public override void DoItWithPicture(Picture picture)

        {

            picture.ChangeBrightness(value);

            base.DoItWithPicture(picture);

        }

    }

}


▶ SatuExpression.cs

namespace Interpreter

{

    class SatuExpression : Expression

    {

        int value;

        public override string Interpret(string context)

        {

            value = 0;

            int index = -1;           

            string be = "";

            string af = "";           

            while ((index = context.IndexOf("S")) != -1)

            {               

                value += GetNumber(context, index);

                if (index > 0)

                {

                    be = context.Substring(0, index);

                }

                else

                {

                    be = "";

                }

                index = context.IndexOf(";", index);

                af = context.Substring(index + 1);

                context = be + af;

            }

            return NextInterpret(context);

        }

        public override void DoItWithPicture(Picture picture)

        {

            picture.ChangeSaturation(value);

            base.DoItWithPicture(picture);

        }

    }

}


▶ Macro.cs

namespace Interpreter

{

    class Macro

    {

        Expression head=null;

        Expression tail=null;

        public void AddExpression(Expression expression)

        {

            if(head!=null)

            {

                tail.Next=expression;

                tail = expression;

            }

            else

            {

                head = tail = expression;

            }

        }

        public void ChangePicture(Picture picture)

        {

            head.DoItWithPicture(picture);

        }

        public void AddContext(string context)

        {

            head.Interpret(context);   

        }

    }

}


▶ Program.cs

namespace Interpreter

{

    class Program

    {

        static void Main(string[] args)

        {

            Expression ex1 = new ToneExpression();

            Expression ex2 = new BrighExpression();

            Expression ex3 = new SatuExpression();

            Macro macro = new Macro();

            macro.AddExpression(ex1);

            macro.AddExpression(ex2);

            macro.AddExpression(ex3);

            macro.AddContext("B 20 ;");

            macro.AddContext("B 20 ;T-12 ; S 10 ; B10 ;");

            Picture picture = new Picture("현충사의 봄", 100, 100, 100);

            macro.ChangePicture(picture);

            picture.View();

        }

    }

}

해석자 패턴 예제 실행화면





반응형