임베디드에서 서버까지
팩토리 메소드 패턴(Factory Method Pattern) [C#] 본문
GOF 디자인패턴에 대해 하나씩 살펴보자.
생성패턴 중 Factory Method Pattern 은 간단하면서도 이해하기 쉬운 패턴이다. 대부분 전략패턴(Strategy Pattern)과 같이 쓰이게 되고, 같은 인터페이스를 상속하는 객체들의 생성을 담당하는 클래스를 Factory 클래스로 관리한다.
장점 : 생성과 관련된 내용을 Factory 클래스에서 관리할 수 있다.
스타크래프트에서 빨간색 마린과 파란색 마린을 생산하는 예제를 살펴보자. 팩토리 메소드 패턴에서는 추상 팩토리 패턴에서와 다르게 Factory의 인스턴스가 꼭 필요하지 않다. FactoryMarine 클래스의 CreateMarine 메소드를 static 으로 선언하여 인스턴스가 아닌 클래스가 IMarine 객체들을 생성하도록 설계하였다.
다음 포스팅에는 이와 비슷하면서도 다른 추상 팩토리 패턴(Abstract Factory Pattern)에 대해서 살펴볼 예정이다.
# 예제 <GitHub>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Factory Method Pattern |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FactoryMethodPattern | |
{ | |
public class BlueMarine : IMarine | |
{ | |
string Me = "Blue Marine"; | |
public BlueMarine() | |
{ | |
Console.WriteLine($"{Me} Created"); | |
} | |
public void Move() | |
{ | |
Console.WriteLine($"{Me} Move"); | |
} | |
public void Shot() | |
{ | |
Console.WriteLine($"{Me} Shot"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FactoryMethodPattern | |
{ | |
public class FactoryMarine | |
{ | |
public static IMarine CreateMarine<T>() where T : IMarine, new() | |
{ | |
return new T(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FactoryMethodPattern | |
{ | |
public interface IMarine | |
{ | |
void Shot(); | |
void Move(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FactoryMethodPattern | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
IMarine marine1 = FactoryMarine.CreateMarine<BlueMarine>(); | |
marine1.Shot(); | |
IMarine marine2 = FactoryMarine.CreateMarine<RedMarine>(); | |
marine2.Shot(); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FactoryMethodPattern | |
{ | |
public class RedMarine : IMarine | |
{ | |
string Me = "Red Marine"; | |
public RedMarine() | |
{ | |
Console.WriteLine($"{Me} Created"); | |
} | |
public void Move() | |
{ | |
Console.WriteLine($"{Me} Move"); | |
} | |
public void Shot() | |
{ | |
Console.WriteLine($"{Me} Shot"); | |
} | |
} | |
} |
'Design Pattern' 카테고리의 다른 글
디자인 패턴(Design Pattern) (0) | 2018.12.11 |
---|---|
추상 팩토리 패턴 (Abstract Factory Pattern) (0) | 2018.12.07 |