[설계 패턴 C#] 17. 해석자 패턴(Interpreter Pattern)
"본문 내용"
[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);
}
}
}
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();
}
}
}
'.NET > 설계 패턴(C#)' 카테고리의 다른 글
[설계 패턴 C#] 19. 중재자 패턴(Mediator Pattern) (0) | 2016.12.08 |
---|---|
[설계 패턴 C#] 18. 반복자 패턴(Iterator Pattern) (0) | 2016.12.07 |
[설계 패턴 C#] 16. 명령 패턴(Command Pattern) (0) | 2016.12.06 |
[설계 패턴 C#] 15. 책임 연쇄 패턴(Chain of Responsibility Pattern) (0) | 2016.12.05 |
[설계 패턴 C#]14. 프락스 패턴(Proxy Pattern) - 보호용 프락시 (0) | 2016.06.24 |