일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 |
- 설계 패턴
- C언어 소스
- 소프트웨어 접근성
- C# 소스 코드
- C언어 표준 라이브러리 함수 가이드
- C언어
- C++
- math.h
- 소스 구현
- 디딤돌 Java 언어 Part1
- 동적 메모리 할당
- 파일 입출력
- XML.NET
- 소프트웨어 설계
- 디딤돌 C++
- java
- gof의 디자인 패턴
- 무료 Java
- 알고리즘
- C#
- 소스 파일
- C언어 표준 라이브러리 함수 사용법 가이드
- String 클래스
- 소스 코드
- Escort GoF의 디자인 패턴
- 클래스 다이어그램
- 디딤돌 C언어
- StringBuffer 클래스
- JAVA 언어
- C언어 소스 코드
- Today
- 11
- Total
- 98,260
프로그램 소스
[설계 패턴 C#] 22. 상태 패턴(State Pattern) 본문
[설계 패턴 C#] 22. 상태 패턴(State Pattern)
"본문 내용"
[Escort GoF의 디자인 패턴 C#] 22. 상태 패턴(State Pattern)
[Escort GoF의 디자인 패턴 C#] 22. 상태 패턴(State Pattern) 설계
[Escort GoF의 디자인 패턴 C#] 22. 상태 패턴(State Pattern) 구현
▶ Iris.cs
namespace State
{
class Iris
{
int focal_length = 2;
public int Up()
{
if(focal_length<10)
{
focal_length++;
}
return focal_length;
}
public int Down()
{
if(focal_length>2)
{
focal_length--;
}
return focal_length;
}
}
}
▶ Shutter.cs
namespace State
{
class Shutter
{
int speed =0;
public int Up()
{
speed++;
return speed;
}
public int Down()
{
if(speed>0)
{
speed--;
}
return speed;
}
}
}
▶ IState.cs
namespace State
{
interface IState
{
void Up();
void Down();
}
}
▶ OnState.cs
using System;
namespace State
{
class OnState:IState
{
Shutter shutter;
public OnState(Shutter shutter)
{
this.shutter = shutter;
}
public void Up()
{
Console.WriteLine("셔터 스피드 올라감. 현재 스피드:{0}",shutter.Up());
}
public void Down()
{
Console.WriteLine("셔터 스피드 내려감. 현재 스피드:{0}",shutter.Down());
}
}
}
▶ OffState.cs
using System;
namespace State
{
class OffState:IState
{
Iris iris;
public OffState(Iris iris)
{
this.iris = iris;
}
public void Up()
{
Console.WriteLine("F 값 증가되었음. 현재 F값:{0}", iris.Up());
}
public void Down()
{
Console.WriteLine("F 값 감소되었음. 현재 F값:{0}", iris.Down());
}
}
}
▶ Camera.cs
namespace State
{
class Camera
{
Shutter shutter = new Shutter();
Iris iris = new Iris();
IState[] states = new IState[2];
int statenum=0;
public Camera()
{
states[0] = new OffState(iris);
states[1] = new OnState(shutter);
}
public void ToggleInfoButton()
{
statenum = (statenum+1)%2;
}
public void DialUp(){ states[statenum].Up(); }
public void DialDown(){ states[statenum].Down(); }
}
}
▶ Program.cs
namespace State
{
class Program
{
static void Main(string[] args)
{
Camera camera = new Camera();
camera.DialUp();
camera.DialDown();
camera.ToggleInfoButton();
camera.DialUp();
camera.DialDown();
}
}
}
'설계 패턴(C#)' 카테고리의 다른 글
[설계 패턴 C#] 24. 템플릿 메서드 패턴(Template Method Pattern) (0) | 2016.12.09 |
---|---|
[설계 패턴 C#] 23. 전략 패턴(Strategy Pattern) (0) | 2016.12.09 |
[설계 패턴 C#] 22. 상태 패턴(State Pattern) (0) | 2016.12.09 |
[설계 패턴 C#] 21. 감시자 패턴(Observer Pattern) (0) | 2016.12.09 |
[설계 패턴 C#] 20. 메멘토 패턴(Memento Pattern) (0) | 2016.12.08 |
[설계 패턴 C#] 19. 중재자 패턴(Mediator Pattern) (0) | 2016.12.08 |
- Tag
- C#, Escort GoF의 디자인 패턴, gof의 디자인 패턴, State Pattern, 상태 패턴, 설계 패턴, 소스 코드, 소스 파일