Book/Head First Design Pattern
Strategy Pattern
doublerabbits
2023. 8. 24. 19:47
Head First Design Pattern 개정판 전략 패턴 내용을 C# Console 프로젝트로 구현
Program.cs
using System;
namespace StrategyPattern
{
internal class Program
{
static void ShowStrategyPattern()
{
Console.WriteLine("[ Strategy Pattern ]");
Console.WriteLine("객체의 행동을 쉽게 확장하거나, 변경할 수 있는 클래스들의 집합으로 캡슐화하는 방식");
Console.WriteLine("알고리즘군을 정의하고 캡슐화해서 각각의 알고리즘군을 수정해서 쓸 수 있게 해 줍니다.");
Console.WriteLine("전략 패턴을 사용하면 클라이언트로부터 알고리즘을 분리해서 독립적으로 변경할 수 있습니다.");
}
static void Main(string[] args)
{
ShowStrategyPattern();
Console.WriteLine("");
Duck mallard = new MallardDuck();
mallard.Display();
mallard.DoQuack();
mallard.DoFly();
Console.WriteLine("");
Duck model = new ModelDuck();
model.Display();
model.DoFly();
model.FlyInterface = new FlyRocketPowered();
model.DoFly();
Console.WriteLine("");
DuckCaller caller = new DuckCaller();
caller.Display();
caller.QuackInterface = new Quack();
caller.DoQuack();
caller.QuackInterface = new Squeak();
caller.DoQuack();
caller.QuackInterface = new MuteQuack();
caller.DoQuack();
Console.WriteLine("");
Console.ReadKey();
}
}
}
Duck.cs
using System;
namespace StrategyPattern
{
public abstract class Duck
{
private IFly flyInterface;
public IFly FlyInterface
{
private get { return flyInterface; }
set
{
flyInterface = null;
flyInterface = value;
}
}
private IQuack quackInterface;
public IQuack QuackInterface
{
private get { return quackInterface; }
set
{
quackInterface = null;
quackInterface = value;
}
}
public Duck() { }
public void DoFly()
{
FlyInterface.Fly();
}
public void DoQuack()
{
QuackInterface.Quack();
}
public void Swim()
{
Console.WriteLine("모드 오리는 물에 뜹니다. 가짜 오리도 뜨죠");
}
public abstract void Display();
}
}
IFly.cs
namespace StrategyPattern
{
public interface IFly
{
void Fly();
}
}
FlyNoWay.cs
using System;
namespace StrategyPattern
{
public class FlyNoWay : IFly
{
public void Fly()
{
Console.WriteLine("저는 못 날아요");
}
}
}
FlyWithWing.cs
using System;
namespace StrategyPattern
{
public class FlyWithWing : IFly
{
public void Fly()
{
Console.WriteLine("날고 있어요!!");
}
}
}
FlyRocketPowered.cs
using System;
namespace StrategyPattern
{
public class FlyRocketPowered : IFly
{
public void Fly()
{
Console.WriteLine("로켓 추진으로 날아갑니다.");
}
}
}
IQuack.cs
namespace StrategyPattern
{
public interface IQuack
{
void Quack();
}
}
Quack.cs
using System;
namespace StrategyPattern
{
public class Quack : IQuack
{
void IQuack.Quack()
{
Console.WriteLine("꽥");
}
}
}
MuteQuack.cs
using System;
namespace StrategyPattern
{
public class MuteQuack : IQuack
{
public void Quack()
{
Console.WriteLine("<< 조용~ >>");
}
}
}
Squeak.cs
using System;
namespace StrategyPattern
{
public class Squeak : IQuack
{
public void Quack()
{
Console.WriteLine("삑");
}
}
}
MallardDuck.cs
using System;
namespace StrategyPattern
{
internal class MallardDuck : Duck
{
public MallardDuck()
{
QuackInterface = new Quack();
FlyInterface = new FlyWithWing();
}
public override void Display()
{
Console.WriteLine("저는 물오리입니다.");
}
}
}
ModelDuck.cs
using System;
namespace StrategyPattern
{
internal class ModelDuck : Duck
{
public ModelDuck()
{
FlyInterface = new FlyNoWay();
QuackInterface = new Quack();
}
public override void Display()
{
Console.WriteLine("저는 모형 오리입니다.");
}
}
}
DuckCaller.cs
using System;
namespace StrategyPattern
{
public class DuckCaller
{
private IQuack quackInterface;
public IQuack QuackInterface
{
private get { return quackInterface; }
set
{
quackInterface = null;
quackInterface = value;
}
}
public void DoQuack()
{
quackInterface.Quack();
}
public void Display()
{
Console.WriteLine("오리 호출기");
}
}
}