Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# Factory Design Pattern



The Factory Design Pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. This pattern is particularly useful when the exact type of object that needs to be created isn’t known until runtime.

When to Use Factory Pattern

  • When creating an instance involves a complex process that needs to be centralized.

  • When a system needs to be independent of how its objects are created.

  • When a system must support the creation of multiple types of objects.

Implementation of Factory Pattern in C#

Let' break down a simple implementation of the Factory Pattern step-by-step:

1. Create an Abstract Product

Define an abstract class or interface for the products that the factory will create.

csharp
public interface IShape
{
    void Draw();
}

2. Create Concrete Product Classes

Implement the abstract class or interface for different product types.

csharp
public class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Circle.");
    }
}

public class Square : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Square.");
    }
}

3. Create a Factory Class

Define a factory class that will create instances of different products based on the provided input.

csharp
public class ShapeFactory
{
    public IShape GetShape(string shapeType)
    {
        if (shapeType == null)
        {
            return null;
        }
        if (shapeType.Equals("Circle", StringComparison.OrdinalIgnoreCase))
        {
            return new Circle();
        }
        else if (shapeType.Equals("Square", StringComparison.OrdinalIgnoreCase))
        {
            return new Square();
        }

        return null;
    }
}

Usage Example

Here' how you can use the Factory Pattern to create objects and invoke their methods:

csharp
class Program
{
    static void Main()
    {
        // Create a ShapeFactory instance
        ShapeFactory shapeFactory = new ShapeFactory();

        // Get different shapes using the factory
        IShape shape1 = shapeFactory.GetShape("Circle");
        shape1?.Draw();

        IShape shape2 = shapeFactory.GetShape("Square");
        shape2?.Draw();
    }
}

Benefits of Factory Pattern

  • Encapsulation: Encapsulates the creation logic and hides the implementation details from the client.

  • Loose Coupling: Promotes loose coupling by reducing dependency on concrete classes.

  • Scalability: Makes it easy to introduce new types of objects without changing the existing code.

Considerations

  • Complexity: Can introduce additional complexity and add more interface and class definitions.

  • Maintenance: May require maintenance of multiple factory methods for different object types.

By using the Factory Pattern, you gain flexibility and manageability in your code creation process, especially when dealing with a variety of object types. If you need more in-depth examples or have specific use cases in mind, let me know!




All rights reserved | Privacy Policy | Sitemap