Here, we will explain Composite Factory Design Pattern with example in C#. This pattern allows you to create complex objects and manage hierarchical structures of these objects with ease. Here is a detailed overview.
The Composite Factory Pattern is a combines the principles of the Composite pattern and the Factory pattern. It allows you to create and manage complex tree-like structures of objects, where each node can be either a leaf or a composite of other nodes. The Factory pattern facilitates the creation of these nodes, managing object creation at various levels of the hierarchy.
Example - File Systems
In a file system, directories can contain files and other directories, creating a hierarchical structure. The Composite Factory Pattern helps manage this hierarchy by providing a unified way to create and manage files and directories. We will explain in detail lator part of the article.
In a file system, directories can contain files and other directories, creating a hierarchical structure. The Composite Factory Pattern helps manage this hierarchy by providing a unified way to create and manage files and directories.
Let's consider a file system where File and Directory are components. A Directory can contain other Files or Directories, and the CompositeFactory is used to create these components.
using System;
using System.Collections.Generic;
// Component
public abstract class FileSystemComponent
{
public abstract void Display(int indent = 0);
}
// Leaf
public class File : FileSystemComponent
{
private string _name;
public File(string name)
{
_name = name;
}
public override void Display(int indent = 0)
{
Console.WriteLine(new String(' ', indent) + "File: " + _name);
}
}
// Composite
public class Directory : FileSystemComponent
{
private string _name;
private List<FileSystemComponent> _components = new List<FileSystemComponent>();
public Directory(string name)
{
_name = name;
}
public void Add(FileSystemComponent component)
{
_components.Add(component);
}
public override void Display(int indent = 0)
{
Console.WriteLine(new String(' ', indent) + "Directory: " + _name);
foreach (var component in _components)
{
component.Display(indent + 2);
}
}
}
// Factory
public static class CompositeFactory
{
public static FileSystemComponent CreateFile(string name)
{
return new File(name);
}
public static FileSystemComponent CreateDirectory(string name)
{
return new Directory(name);
}
}
// Example usage
class Program
{
static void Main()
{
// Create files
var file1 = CompositeFactory.CreateFile("file1.txt");
var file2 = CompositeFactory.CreateFile("file2.txt");
// Create directories
var dir1 = (Directory)CompositeFactory.CreateDirectory("dir1");
var dir2 = (Directory)CompositeFactory.CreateDirectory("dir2");
// Add files to directories
dir1.Add(file1);
dir1.Add(file2);
// Create a nested directory
var nestedDir = (Directory)CompositeFactory.CreateDirectory("nestedDir");
nestedDir.Add(CompositeFactory.CreateFile("nestedFile1.txt"));
// Add nested directory to dir2
dir2.Add(nestedDir);
// Display the hierarchy
Console.WriteLine("File System Structure:");
dir1.Display();
dir2.Display();
}
}
Output