Home > Software engineering >  When do we need to create the concrete factory method?
When do we need to create the concrete factory method?

Time:06-14

I am trying to learn the factory design pattern. I have created a code for creating two animals (for now only dogs and a cats).

I have got an Animal (as a product)

public abstract class Animal
{
    public abstract void CreateBody();
    public abstract void CreateLeg();
}

Dog and Cat Implements Animal class:

public class Dog: Animal
{
    public override void CreateBody()
    {
        Console.WriteLine("Dog body created");
    }
    public override void CreateLeg()
    {
        Console.WriteLine("Dog body created");
    }
}

Cat Implements Animal:

public class Cat: Animal
{
    public override void CreateBody()
    {
        Console.WriteLine("Cat body created");
    }
    public override void CreateLeg()
    {
        Console.WriteLine("Cat Leg created");
    }
}

Created a factory class that makes different animal according to the input given in the parameter:

public class AnimalFactory
{
    public static Animal CreateAnimal(string animal)
    {
        Animal animalDetails = null;
        if (animal == "cat")
        {
            animalDetails = new Cat();
        }
        else if (animal == "dog")
        {
            animalDetails = new Dog();
        }

        animalDetails.CreateBody();
        animalDetails.CreateLeg();

        return animalDetails;
    }
}

In the main it is used as:

static void Main(string[] args)
{
    Animal animalDetails = AnimalFactory.CreateAnimal("cat");

    if (animalDetails != null)
    { 
    }
    else
    {
        Console.Write("Invalid Card Type");
    }
    Console.ReadLine();
}

Here in this code, I know exactly what methods I need to call to create an animal, so I called those methods in the factory class.

As I read in the various tutorials and books, most of the implementation of the factory method uses another concrete factory method to create the concrete product?

In what case it is appropriate to create the concrete factory method? Is that concrete factory relevant in this demo too?

CodePudding user response:

My implementation :

Animal interface :

public interface IAnimal
{
    void CreateBody();
    void CreateLeg();
}

Cat & Dog classes:

public class Dog : IAnimal
{
    public void CreateBody()
    {
        Console.WriteLine("Dog body created");
    }

    public void CreateLeg()
    {
        Console.WriteLine("Dog leg created");
    }
}

public class Cat : IAnimal
{
    public void CreateBody()
    {
        Console.WriteLine("Cat body created");
    }

    public void CreateLeg()
    {
        Console.WriteLine("Cat leg created");
    }
}

Animal factory

public static class AnimalFactory
{
    public static IAnimal? GetInstance(Type type)
    {
        if(type.GetInterfaces().Contains(typeof(IAnimal))) // check if type implement IAnimal
        {
            return Activator.CreateInstance(type) as IAnimal;
        }
        return null;
    }
}

Use case

IAnimal? cat = AnimalFactory.GetInstance(typeof(Cat));
IAnimal? dog = AnimalFactory.GetInstance(typeof(Dog));
cat?.CreateBody();
cat?.CreateLeg();

dog?.CreateBody();
dog?.CreateLeg();

CodePudding user response:

this structure can be expanded into an abstractory factory provider. it currently demonstrate a simple factory provider model. The provider accepts the derived class of animal which is either cat or dog and sets a private label called type

public abstract class Animal
    {
        public string _Type;
        public abstract void CreateBody();
        public abstract void CreateLeg();
        public abstract void SetType(string type);
    }
    public class Dog : Animal
    {
        public Dog() {
            SetType("Dog");
        }

        public override void SetType(string type)
        {
            _Type = type;
        }

        public override void CreateBody()
        {
            Console.WriteLine("Dog body created");
        }
        public override void CreateLeg()
        {
            Console.WriteLine("Dog body created");
        }
    }
    public class Cat : Animal
    {
        public Cat()
        {
            SetType("Cat");
        }

        public override void SetType(string type)
        {
            _Type = type;
        }


        public override void CreateBody()
        {
            Console.WriteLine("Cat body created");
        }
        public override void CreateLeg()
        {
            Console.WriteLine("Cat Leg created");
        }
    }

    public class AnimalProviderFactory {
        public AnimalProvider GetAnimalProvider()
        {
            return new AnimalProvider();
        }


    }
    public class AnimalProvider{
        public string GenerateShippingLabelFor(Animal catOrDog)
        {
            return catOrDog._Type;
        }

    }

    public class AnimalType
    {
       private readonly Animal catOrDog;
       private readonly AnimalProviderFactory animalProviderFactory;
        public AnimalType(Animal catOrDog, AnimalProviderFactory animalProviderFactory)
        {
            this.catOrDog = catOrDog;
            this.animalProviderFactory = animalProviderFactory;
        }
        public string Finalize()
        {
            var provider = animalProviderFactory.GetAnimalProvider();

            return provider.GenerateShippingLabelFor(catOrDog);

        }
    }

public void TestAbstractFactory()
        {
            Cat cat = new Cat();
            AnimalType animalType= new AnimalType(cat, new AnimalProviderFactory());
            var type = animalType.Finalize();
            _output.WriteLine(type);
            Assert.True(true);
        }
  • Related