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



The Decorator Design Pattern is a structural pattern that allows behavior to be added to individual objects, dynamically, without affecting the behavior of other objects from the same class. It's a flexible alternative to subclassing for extending functionality.

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

csharp
using System;

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

// Concrete Component
public class ConcreteComponent : IComponent
{
    public void Operation()
    {
        Console.WriteLine("ConcreteComponent Operation");
    }
}

// Base Decorator
public abstract class Decorator : IComponent
{
    protected IComponent _component;

    public Decorator(IComponent component)
    {
        _component = component;
    }

    public virtual void Operation()
    {
        if (_component != null)
        {
            _component.Operation();
        }
    }
}

// Concrete Decorator A
public class ConcreteDecoratorA : Decorator
{
    public ConcreteDecoratorA(IComponent component) : base(component) { }

    public override void Operation()
    {
        base.Operation();
        Console.WriteLine("ConcreteDecoratorA Operation");
    }
}

// Concrete Decorator B
public class ConcreteDecoratorB : Decorator
{
    public ConcreteDecoratorB(IComponent component) : base(component) { }

    public override void Operation()
    {
        base.Operation();
        AdditionalBehavior();
    }

    void AdditionalBehavior()
    {
        Console.WriteLine("ConcreteDecoratorB Additional Behavior");
    }
}

// Client code
class Program
{
    static void Main(string[] args)
    {
        IComponent component = new ConcreteComponent();
        IComponent decoratorA = new ConcreteDecoratorA(component);
        IComponent decoratorB = new ConcreteDecoratorB(decoratorA);

        decoratorB.Operation();

        // Outputs:
        // ConcreteComponent Operation
        // ConcreteDecoratorA Operation
        // ConcreteDecoratorB Additional Behavior
    }
}

Explanation:

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

  2. Concrete Component: This is the class that we want to add new behavior to. It implements the IComponent interface.

  3. Base Decorator (Decorator): This abstract class implements the IComponent interface and has a reference to an IComponent object. The base decorator delegates the Operation method to the wrapped component.

  4. Concrete Decorators (ConcreteDecoratorA, ConcreteDecoratorB): These classes extend the base decorator and add their own behavior before or after calling the base decorator's Operation method.

  5. Client Code: The client creates a ConcreteComponent object and wraps it with ConcreteDecoratorA and ConcreteDecoratorB. The final call to Operation executes all the operations in the order of wrapping.

The Decorator Design Pattern is useful when you need to add responsibilities to objects dynamically and independently without affecting other objects.




All rights reserved | Privacy Policy | Sitemap