Prototype Design Pattern in .NET

The Prototype pattern is used to clone an existing object rather than creating a new instance from scratch. It provides a way to copy objects while maintaining performance, especially when object creation is expensive or complex.

 

Purpose

To reduce the cost and complexity of creating new objects by copying an existing one instead of building it step by step or with multiple dependencies.

 

Key Characteristics

  • Cloning existing objects using a Clone or Copy method
  • Typically involves deep or shallow copying
  • Useful when object construction is expensive or slow

 

Pros

  • Reduces object creation overhead
  • Simplifies duplicating complex objects
  • Adds flexibility by allowing modifications to a clone

 

Cons

  • Cloning may be complex for deep object graphs
  • Requires careful management of mutable shared references

 

Use Cases

  • Cloning template-based documents, reports, or invoices
  • Creating a copy of a pre-configured loan application, user profile, or form
  • Duplicating investment portfolios or asset allocation strategies

 

Real-Time Finance Example: Cloning a Pre-Filled Loan Template

You want to allow users to duplicate a previous loan application or use a pre-filled template with some default values. The Prototype pattern helps clone and customize it.

 

1. Define the Prototype Interface

public interface IPrototype<T>

{

    T Clone();

}

 

2. Implement the Prototype in a Class

public class LoanTemplate : IPrototype<LoanTemplate>

{

    public string LoanType { get; set; }

    public decimal DefaultAmount { get; set; }

    public int TenureMonths { get; set; }

    public decimal InterestRate { get; set; }

 

    public LoanTemplate Clone()

    {

        return (LoanTemplate)this.MemberwiseClone(); // shallow copy

    }

 

    public void Show()

    {

        Console.WriteLine($"Loan Type: {LoanType}");

        Console.WriteLine($"Amount: ₹{DefaultAmount}");

        Console.WriteLine($"Tenure: {TenureMonths} months");

        Console.WriteLine($"Interest Rate: {InterestRate}%");

    }

}

 

3. Client Code Usage

class Program

{

    static void Main()

    {

        // Original template for Home Loan

        var homeLoanTemplate = new LoanTemplate

        {

            LoanType = "Home Loan",

            DefaultAmount = 2500000,

            TenureMonths = 240,

            InterestRate = 7.5m

        };

 

        // Clone the template for another customer

        var clonedLoan = homeLoanTemplate.Clone();

        clonedLoan.DefaultAmount = 3000000; // customize if needed

 

        Console.WriteLine("Original Loan Template:");

        homeLoanTemplate.Show();

 

        Console.WriteLine();

 

        Console.WriteLine("Cloned & Customized Loan:");

        clonedLoan.Show();

    }

}

 

Output

Original Loan Template:

Loan Type: Home Loan

Amount: ₹2500000

Tenure: 240 months

Interest Rate: 7.5%

 

Cloned & Customized Loan:

Loan Type: Home Loan

Amount: ₹3000000

Tenure: 240 months

Interest Rate: 7.5%

 

Summary

  • The Prototype pattern is perfect for scenarios where object creation is expensive or standardized
  • In finance applications, it's ideal for duplicating forms, applications, configurations, or templates
  • It helps you avoid repetitive setup and allows quick customization

Comments

Popular posts from this blog

Multiline to singleline IN C# - CODING

EF Core interview questions for beginners

EF Core interview questions for experienced