5/20/2026

Adapter Pattern

 Here is a clean, simple explanation with formatted code and key points added 👇


❌ Without Adapter

Old class:

class OldLogger
{
    public void WriteLog(string message)
    {
        Console.WriteLine(message);
    }
}

Client:

class NewApp
{
    OldLogger logger = new OldLogger();

    public void Process()
    {
        logger.WriteLog("Processing");
    }
}

👉 Problem:
NewApp is directly dependent on OldLogger → tightly coupled → hard to replace or test


✅ With Adapter

Target interface:

interface ILogger
{
    void Log(string message);
}

Old class (unchanged):

class OldLogger
{
    public void WriteLog(string message)
    {
        Console.WriteLine(message);
    }
}

Adapter:

class LoggerAdapter : ILogger
{
    private OldLogger oldLogger;

    public LoggerAdapter(OldLogger oldLogger)
    {
        this.oldLogger = oldLogger;
    }

    public void Log(string message)
    {
        oldLogger.WriteLog(message);
    }
}

Client:

class NewApp
{
    private ILogger logger;

    public NewApp(ILogger logger)
    {
        this.logger = logger;
    }

    public void Process()
    {
        logger.Log("Processing");
    }
}

Usage:

var app = new NewApp(new LoggerAdapter(new OldLogger()));

✅ Why we need Adapter

Used when an existing class cannot be changed but its method does not match what your system expects. Adapter converts one interface into another so both can work together.


✅ Use of Adapter

It helps integrate legacy code, third-party libraries, or different systems without modifying them. It keeps your main code clean because the client works only with an interface, not the actual old class.


✅ Final Idea

Without adapter → client depends on concrete class
With adapter → client depends on interface, adapter handles conversion

👉 Adapter hides incompatibility and reduces coupling in your main logic

Prototype Design Pattern

✅ Definition Prototype Pattern = Create new objects by cloning an existing object instead of creating from scratch, which improves performance by avoiding repeated expensive initialization, reduces CPU work, and allows reuse of preconfigured object state. 🤔 Why use it? Used when object creation is slow, complex, or repeated, and you want to reuse an existing object as a template instead of rebuilding it every time.

📊 Comparison
AspectWithout Prototype (new)With Prototype (clone)
CreationBuild from scratchCopy existing
Speed❌ Slow✅ Fast
CPU❌ High✅ Low
Memory✅ Same✅ Same
Use caseSimple objectsComplex/expensive objects
Risk✅ Safe⚠️ Copy issues

🔁 Shallow vs Deep Copy

TypeDescriptionSpeedRisk
ShallowCopies references✅ Fast❌ Shared data
DeepFull independent copy❌ Slower✅ Safe

var r1 = new Report();
var r2 = new Report(); // expensive again

using System;

class Report : ICloneable
{
    public string Data;

    public Report()
    {
        Console.WriteLine("Expensive creation...");
    }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

// Usage
var original = new Report();
var copy1 = (Report)original.Clone();
var copy2 = (Report)original.Clone();


public object Clone()
{
    return new Report
    {
        Data = this.Data
    };
}