Limitation of Simple Factory pattern
- Too - Many Sub-Classes: We have to add a case statement in the factory class for each subclass to be added. If the number of subclass increases more than 5, it makes the switch statement lengthy.
- Multiple Deciding Parameters: Simple Factory Pattern is used to return an object of sub-class based on a single parameter. If the number of filtering parameters increases, then the switch statement is not useful.
Solution. Factory Method Pattern
Components
- Client: Class
- Abstract Product: Interface / Abstract Class
- Concrete Product: Class
- Abstract Factory:- Interface / Abstract Class
- Concrete Factory: Class
Abstract Product
It is an interface or abstract class. The purpose is to define what their implementation should contain.
public interface IDocumentType
{
public void OpenDocument();
public void CloseDocument();
public void SaveDocument();
}
Concrete Product
It is a class & it provides an implementation of the parent interface.
We have two classes that provide open, close & save functionality in PDF & word documents.
public class PdfDocument : IDocumentType
{
public void OpenDocument()
{
Console.Writeline("PDF file opened");
}
public void CloseDocument()
{
Console.Writeline("PDF file closed");
}
public void SaveDocument()
{
Console.Writeline("PDF file saved");
}
}
public class WordDocument : IDocumentType
{
public void OpenDocument()
{
Console.Writeline("Word file opened");
}
public void CloseDocument()
{
Console.Writeline("Word file closed");
}
public void SaveDocument()
{
Console.Writeline("Word file saved");
}
}
Abstract Factory
It can be an abstract class or an interface.
In the abstract factory, we define the common signature of the method required in concrete factory classes.
public abstract OperatingSystemFactory
{
public abstract IDocumentType CreateDocument();
}
public abstract WindowFactory: OperatingSystemFactory
{
public virtual IDocumentType CreateDocument();
}
public abstract MacFactory: OperatingSystemFactory
{
public virtual IDocumentType CreateDocument();
}
public abstract LinuxFactory: OperatingSystemFactory
{
public virtual IDocumentType CreateDocument();
}
Concrete Factory Class
1 Factory for each Document type based on their operating system.
public class WindowPdfFactory : WindowFactory
{
public override IDocumentType CreateDocumentType()
{
return new PdfDcoument();
}
}
public class WindowWordFactory : WindowFactory
{
public override IDocumentType CreateDocumentType()
{
return new WordDcoument();
}
}
public class MacPdfFactory : MacFactory
{
public override IDocumentType CreateDocumentType()
{
return new PdfDcoument();
}
}
public class MacWordFactory : MacFactory
{
public override IDocumentType CreateDocumentType()
{
return new WordDcoument();
}
}
Client Class
public class Client
{
OperatingSystemFactory windowPDFFactory = new WindowPdfFactory();
windowPDFFactory.OpenDocument();
windowPDFFactory.SaveDocument();
windowPDFFactory.CloseDocument();
}
Output
PDF file opened.
PDF file saved.
PDF file closed.