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# Composite Design Pattern



The Composite Design Pattern is a structural pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.

Here's an example of how you can implement the Composite Design Pattern in C#:

csharp
using System;
using System.Collections.Generic;

// Component interface
public interface IComponent
{
    void Operation();
}

// Leaf class
public class Leaf : IComponent
{
    private string _name;

    public Leaf(string name)
    {
        _name = name;
    }

    public void Operation()
    {
        Console.WriteLine("Leaf " + _name + " operation.");
    }
}

// Composite class
public class Composite : IComponent
{
    private List<IComponent> _children = new List<IComponent>();
    private string _name;

    public Composite(string name)
    {
        _name = name;
    }

    public void Add(IComponent component)
    {
        _children.Add(component);
    }

    public void Remove(IComponent component)
    {
        _children.Remove(component);
    }

    public void Operation()
    {
        Console.WriteLine("Composite " + _name + " operation.");
        foreach (var child in _children)
        {
            child.Operation();
        }
    }
}

// Client code
class Program
{
    static void Main(string[] args)
    {
        IComponent leaf1 = new Leaf("A");
        IComponent leaf2 = new Leaf("B");
        
        Composite composite1 = new Composite("Composite1");
        composite1.Add(leaf1);
        composite1.Add(leaf2);
        
        Composite composite2 = new Composite("Composite2");
        composite2.Add(composite1);
        
        composite2.Operation();

        // Outputs:
        // Composite Composite2 operation.
        // Composite Composite1 operation.
        // Leaf A operation.
        // Leaf B operation.
    }
}

Explanation:

  1. Component Interface (IComponent): This interface defines the Operation method that all components (both composite and leaf) must implement.

  2. Leaf Class: This class represents the leaf objects in the composition. It implements the IComponent interface and defines the behavior for the leaf.

  3. Composite Class: This class represents the composite objects that can have children. It implements the IComponent interface and defines methods to add and remove children. The Operation method of the composite iterates over its children and calls their Operation method.

  4. Client Code: The client creates leaf and composite objects and builds a tree structure. It then calls the Operation method on the root composite, which in turn calls the Operation method of its children, demonstrating the part-whole hierarchy.

The Composite Design Pattern is useful when you need to work with tree structures and treat individual objects and compositions of objects uniformly.




All rights reserved | Privacy Policy | Sitemap