본문 바로가기
C# 디자인패턴

03# 추상 팩토리(Abstract Factory) 패턴 Creational Patterns

by NaHyungMin 2019. 5. 22.

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

 

Abstract Factory .NET Design Pattern in C# and VB - dofactory.com

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.     The classes and objects participating in this pattern are: This structural code demonstrates the Abstract Factory pattern creating par

www.dofactory.com