> For the complete documentation index, see [llms.txt](https://grofit.gitbook.io/development-for-winners/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://grofit.gitbook.io/development-for-winners/development/general/design-patterns/factory.md).

# Factory

The factory pattern is one of the simpler patterns you will come across. Its purpose is to hide the underlying creation mechanism for a class so you can create something in an isolated way and get an implementation you need without having to worry about actually initializing it yourself.

## Getting started

For example if we had some classes like:

```csharp
public enum AnimalType
{
    Unknown = 0,
    Dog = 1,
    Cat = 2
}

public interface Animal
{
    string MakeSound();
}

public class Dog : Animal
{
    public string MakeSound()
    { return "woof woof"; }
}

public class Cat : Animal
{
    public string MakeSound()
    { return "meow meow"; }
}
```

Then if we wanted to get an animal based upon the type we could make a factory like so:

```csharp
public class AnimalFactory()
{
    public IAnimal CreateAnimal(AnimalType type)
    {
        switch(type)
        {
            case AnimalType.Dog: { return new Dog(); }
            default: { return new Cat(); }
        }
    }
}
```

That is a basic factory which would generate the implementations you require without you having to worry about how it does it.

## Example usage

```
var animalFactory = new AnimalFactory();
var myAnimal = animalFactory.CreateAnimal(AnimalType.Dog);

Console.WriteLine(myAnimal.MakeSound());
```

> Historically factory patterns used to be used a lot to abstract away hardware resources, like textures, vertex buffers etc. As when you were making a cross platform engine and needed to work with OpenGL, DirectX (or other rendering technologies) the developer rarely cared about the underlying hardware, they just wanted a texture of a certain size to use, or a vertex buffer of a certain size to provide to the renderer, so this pattern helped abstract away these concerns and exposing high level objects to represend the underlying resources.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://grofit.gitbook.io/development-for-winners/development/general/design-patterns/factory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
