3
Answers

Abstract Factory pattern

Jasminder Singh

Jasminder Singh

11y
1.6k
1

Hi all,  was exploring the abstract factory pattern and in every second article, i get this definition :

"an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes." 

I ?created a sample code based on my understanding of this pattern and ideas that came from other articles. But there are certain points that are still confusing me : 


1. What does the definition mentioned above means ?

2. Why would i need to have an abstract factory pattern ? 


I was able to create a sample code, but I am not sure what part of the code/implementation is must to go for an abstract factory pattern. My sample code is :


/// <summary>
/// Master 'Abstract Factory'
/// </summary>
public interface IMasterFactory
{
    IProduct1 Getproduct1();
    Iproduct2 GetProduct2();
}

/// <summary>
/// Abstract Product 1
/// </summary>
public interface IProduct1
{
    String GetProduct1Type();
}

/// <summary>
/// Abstract Product 2
/// </summary>
public interface IProduct2
{
	String GetProduct2Type();
}


public class ManufacturerA : IMasterFactory
{
    public IProduct2 GetProduct2Type()
    {
        return new Product2Type();
    }

    public IProduct1 IProduct1Type()
    {
        return new Product1Type();
    }
}

public class Product1Type: IProduct1Type
{
    public string GetProduct1Type()
    {
        return "Product 1 Type";
    }
}

public class Product2Type:  IProduct2Type
{
    public string GetProduct2Type()
    {
        return "Product 2 Type";
    }
}

Which code i can remove from here to say that it was not required to be written..

Answers (3)