[설계 패턴 C#] 20. 메멘토 패턴(Memento Pattern)
"본문 내용"
[Escort GoF의 디자인 패턴 C#] 20. 메멘토 패턴(Memento Pattern)
[Escort GoF의 디자인 패턴 C#] 20. 메멘토 패턴(Memento Pattern) 설계
[Escort GoF의 디자인 패턴 C#] 20. 메멘토 패턴(Memento Pattern) 구현
▶ Snapshot.cs
namespace Memento
{
class Snapshot
{
public int Tone{ get; private set; }
public int Brightness{ get; private set; }
public int Saturation{ get; private set; }
public Snapshot(int tone, int brightness, int saturation)
{
Tone = tone;
Brightness = brightness;
Saturation = saturation;
}
}
}
▶ Picture.cs
using System;
namespace Memento
{
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 Change(int tone,int brightness,int saturation)
{
this.tone += tone;
this.brightness += brightness;
this.saturation += saturation;
}
public void View()
{
Console.WriteLine("사진 파일명:{0}",name);
Console.WriteLine(" 색조:{0} 명도:{1} 채도:{2}",tone,brightness,saturation);
}
public Snapshot MakeSnapshot()
{
return new Snapshot(tone,brightness,saturation);
}
public void SetBySnapshot(Snapshot snapshot)
{
this.tone = snapshot.Tone;
this.brightness = snapshot.Brightness;
this.saturation = snapshot.Saturation;
}
}
}
▶ App.cs
using System;
namespace Memento
{
class App
{
Picture picture;
Snapshot snapshot = null;
public App(Picture picture)
{
SetPicture(picture);
}
public void SetPicture(Picture picture)
{
End();
Console.WriteLine("사진을 설정합니다.");
this.picture = picture;
Start();
}
void Start()
{
picture.View();
snapshot = picture.MakeSnapshot();
}
public void Change(int tone,int brightness,int saturation)
{
picture.Change(tone,brightness,saturation);
picture.View();
}
public void Cancel()
{
if(snapshot!=null)
{
picture.SetBySnapshot(snapshot);
}
End();
}
public void End()
{
if(snapshot!=null)
{
picture.View();
}
snapshot = null;
}
}
}
▶ Program.cs
namespace Memento
{
class Program
{
static void Main(string[] args)
{
Picture picture = new Picture("천호지의 들풀", 100, 100, 100);
Picture picture2 = new Picture("외도 가는 길", 100, 100, 100);
App app = new App(picture);
app.Change(10, 20, -20);
app.Change(10, 20, -20);
app.Cancel();
app.SetPicture(picture2);
app.Change(10, 20, -20);
app.Change(10, 20, -20);
app.End();
}
}
}
'.NET > 설계 패턴(C#)' 카테고리의 다른 글
[설계 패턴 C#] 22. 상태 패턴(State Pattern) (0) | 2016.12.09 |
---|---|
[설계 패턴 C#] 21. 감시자 패턴(Observer Pattern) (0) | 2016.12.09 |
[설계 패턴 C#] 19. 중재자 패턴(Mediator Pattern) (0) | 2016.12.08 |
[설계 패턴 C#] 18. 반복자 패턴(Iterator Pattern) (0) | 2016.12.07 |
[설계 패턴 C#] 17. 해석자 패턴(Interpreter Pattern) (0) | 2016.12.07 |