디자인 모델 Factory Method

1427 단어
/*****************************************************************
	Factory Method
	
	definition
			Define an interface for creating an object, but let 
			subclasses decide which class to instantiate. Factory 
			Method lets a class defer instantiation to subclasses.  
			
			   http://www.dofactory.com/Patterns/PatternFactory.aspx#_self2
			     http://www.cnblogs.com/webabcd/archive/2008/04/02/1134658.html
*****************************************************************/

using System;
using System.Collections.Generic;

namespace Pattern.FactoryMethod
{
	public class MainApp
	{
		public static void Main()
		{
			Creator[] creators = new Creator[2];
			
			creators[0] = new ConcreteCreatorA();
			creators[1] = new ConcreteCreatorB();
			
			foreach(Creator cr in creators)
			{
				Console.WriteLine(cr.GetType().Name + ", " +cr.FactoryMethod().GetType().Name);
			}
			
			Console.ReadKey();
		}
	}
	
	// Product
	abstract class Product
	{
	}
	
	class ConcreteProductA : Product
	{
	}
	
	class ConcreteProductB : Product
	{
	}
	
	// Creator
	abstract class Creator
	{
		public abstract Product FactoryMethod();
	}
	
	class ConcreteCreatorA : Creator
	{
		public override Product FactoryMethod()
		{
			return new ConcreteProductA();
		}
	}
	
	class ConcreteCreatorB : Creator
	{
		public override Product FactoryMethod()
		{
			return new ConcreteProductB();
		}
	}
	
}

좋은 웹페이지 즐겨찾기