static void Main(string[] args) { //BuilderExample(); FactoryExample();
//Console.WriteLine("Hello World!"); Console.ReadKey(); }
static void FactoryExample() { ContinentFactory africa = new AfricaFactory(); AnimalWorld world = new AnimalWorld(africa); world.RunFoodChain();
ContinentFactory america = new AmericaFactory(); world = new AnimalWorld(america); world.RunFoodChain(); }
main.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory { public class Herbivore { } }
Herbivore.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory { public abstract class Carnivore { public abstract void Eat(Herbivore h); } }
Carnivore.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory { public abstract class ContinentFactory { public abstract Herbivore CreateHerbivore(); //초식동물 public abstract Carnivore CreateCarnivore(); //육식동물 } }
ContinentFactory.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory.Animals.Carnivores { public class Lion : Carnivore { public override void Eat(Herbivore h) { // Eat Wildebeest Console.WriteLine(string.Format("{0} eats {1}", this.GetType().Name, h.GetType().Name)); } } }
Lion.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory.Animals.Carnivores { public class Wolf : Carnivore { public override void Eat(Herbivore h) { // Eat Bison Console.WriteLine(string.Format("{0} eats {1}", this.GetType().Name, h.GetType().Name)); } } }
Wolf.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory.Animals.Herbivores { //초식동물 들소 class Bison : Herbivore { } }
Bison.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory.Animals.Herbivores { //초식동물 (뿔이 휘어져 있는 큰) 영양(羚羊) class Wildebeest : Herbivore { } }
Wildebeest.cs |
using System; using System.Collections.Generic; using System.Text; using DesignPatterns.Patterns.Factory.Animals.Carnivores; using DesignPatterns.Patterns.Factory.Animals.Herbivores;
namespace DesignPatterns.Patterns.Factory { public class AfricaFactory : ContinentFactory { public override Carnivore CreateCarnivore() { return new Lion(); }
public override Herbivore CreateHerbivore() { return new Wildebeest(); } } }
AfricaFactory.cs |
using System; using System.Collections.Generic; using System.Text; using DesignPatterns.Patterns.Factory.Animals.Carnivores; using DesignPatterns.Patterns.Factory.Animals.Herbivores;
namespace DesignPatterns.Patterns.Factory { public class AmericaFactory : ContinentFactory { public override Carnivore CreateCarnivore() { return new Wolf(); }
public override Herbivore CreateHerbivore() { return new Bison(); } } }
AmericaFactory .cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.Factory { public class AnimalWorld { private Herbivore herbivore; private Carnivore carnivore;
public AnimalWorld(ContinentFactory factory) { carnivore = factory.CreateCarnivore(); herbivore = factory.CreateHerbivore(); }
public void RunFoodChain() { carnivore.Eat(herbivore); } } }
AnimalWorld.cs |
참고 사이트
https://www.dofactory.com/net/abstract-factory-design-pattern
'C# 디자인패턴' 카테고리의 다른 글
05# Factory Method 패턴2 Creational Patterns (0) | 2019.05.23 |
---|---|
04# Factory Method 패턴1 Creational Patterns (0) | 2019.05.23 |
02# Builder 패턴 Creational Patterns (0) | 2019.05.22 |
01# 싱글턴(Singleton) 패턴 (0) | 2016.04.18 |
00# 추상 팩토리(Abstract Factory) 패턴 (0) | 2016.04.18 |