Skip to content

Latest commit

 

History

History
202 lines (168 loc) · 3.85 KB

File metadata and controls

202 lines (168 loc) · 3.85 KB

Code using factory method design pattern

Using the factory method pattern, we have the following objects:

IAnimal.cs

/// <summary>
/// Product
/// </summary>
public interface IAnimal
{
    void MakeSound();
}

Cat.cs

/// <summary>
/// Concrete Product
/// </summary>
public class Cat : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Miau Miau");
    }
}

Dog.cs

/// <summary>
/// Concrete Product
/// </summary>
public class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Au Au");
    }
}

Duck.cs

/// <summary>
/// Concrete Product
/// </summary>
public class Duck : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Quack Quack");
    }
}

IAnimalFactory.cs

/// <summary>
/// Creator
/// </summary>
public interface IAnimalFactory
{
    IAnimal CreateAnimal();
}

RandomAnimalFactory.cs

/// <summary>
/// Concrete Creator
/// </summary>
public class RandomAnimalFactory : IAnimalFactory
{
    /// <summary>
    /// Creates a new IAnimal every time it is invoked (does not have state)
    /// </summary>
    /// <returns></returns>
    /// <exception cref="InvalidOperationException"></exception>
    public IAnimal CreateAnimal()
    {
        var random = new Random().Next(0, 2);

        return random switch
        {
            0 => new Cat(),
            1 => new Dog(),
            2 => new Duck(),
            _ => throw new InvalidOperationException()
        };
    }
}

BalancedAnimalFactory.cs

/// <summary>
/// Concrete Creator
/// </summary>
public class BalancedAnimalFactory : IAnimalFactory
{
    private short _counter;

    /// <summary>
    /// Creates a new IAnimal but in a balanced way (it has state)
    /// </summary>
    /// <returns></returns>
    /// <exception cref="InvalidOperationException"></exception>
    public IAnimal CreateAnimal()
    {
        switch (_counter)
        {
            case 0:
                IncrementCounter();
                return new Cat();
            
            case 1:
                IncrementCounter();
                return new Dog();
            
            case 2:
                ResetCounter();
                return new Duck();
        }

        throw new InvalidOperationException();
    }

    private void IncrementCounter()
    {
        _counter++;
    }
    
    private void ResetCounter()
    {
        _counter = 0;
    }
}

Program.cs

var randomAnimalFactory = new RandomAnimalFactory();
randomAnimalFactory.CreateAnimal().MakeSound();
randomAnimalFactory.CreateAnimal().MakeSound();
randomAnimalFactory.CreateAnimal().MakeSound();
randomAnimalFactory.CreateAnimal().MakeSound();
randomAnimalFactory.CreateAnimal().MakeSound();
randomAnimalFactory.CreateAnimal().MakeSound();

var balancedAnimalFactory = new BalancedAnimalFactory();
balancedAnimalFactory.CreateAnimal().MakeSound();
balancedAnimalFactory.CreateAnimal().MakeSound();
balancedAnimalFactory.CreateAnimal().MakeSound();
balancedAnimalFactory.CreateAnimal().MakeSound();
balancedAnimalFactory.CreateAnimal().MakeSound();
balancedAnimalFactory.CreateAnimal().MakeSound();

Output generated by the random animal factory

Au Au
Au Au
Au Au
Au Au
Miau Miau
Miau Miau

Output generated by the balanced animal factory

Miau Miau
Au Au
Quack Quack
Miau Miau
Au Au
Quack Quack

Try it yourself

You can run the console application to see the output in the console.

To do that, you can use your IDE buttons/options or via terminal, executing the following command:

before executing the command, cd to this repository root directory

dotnet run --project ./FactoryMethodPattern/FactoryMethodPattern.WithPattern/FactoryMethodPattern.WithPattern.csproj

📄 Code without factory method pattern