Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998406
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:31:17+00:00 2026-05-20T07:31:17+00:00

Let’s suppose I need to save a text in my application into a file,

  • 0

Let’s suppose I need to save a text in my application into a file, but allowing the user to have more than one format (.pdf, .word, .txt, ...) to select.

A first approach could be:

if (extension == ".pdf")
  ExportToPdf(file);
else if (extension == ".txt")
  ExportToTxt(file);
...

but I usually encapsulate the above like this:

abstract class Writer
{
  abstract bool CanWriteTo(string file);
  abstract void Write(string text, string file);
}

class WritersHandler
{
  List<Writer> _writers = ... //All writers here

  public void Write(string text, string file) 
  {
    foreach (var writer in _writers) 
    {
      if (writer.CanWriteTo(file) 
      {
        writer.Write(text, file);
        return;
      {
    }
    throw new Exception("...");
  }
}

Using it, if I need to add a new extension/format, all I have to do is create a new class (that inherits from Writer) for that writer and implement the CanWriteTo(..) and Write(..) methods, and add that new writer to the list of writers in WritersHandler (maybe adding a method Add(Writer w) or manually, but that’s not the point now).

I also use this in other situations.

My question is:

What’s the name of this pattern? (maybe it’s a modification of a pattern, don’t know).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:31:18+00:00Added an answer on May 20, 2026 at 7:31 am

    I would do it a bit differently than you.

    The main difference would be the way of storing handlers and picking the right one.
    In fact I think that chain of responsibility is a bad choice here. Moreover iterating through the ist of handlers may be time consuming (if there are more of them). Dictionary provides O(1) writer retrieval.
    If I were to guess I’d tell that my pattern is called Strategy.

    abstract class Writer
    {
      abstract string SupportedExtension {get;}
      abstract void Write(string text, string file);
    }
    
    class WritersHandler
    {
      Dictionary<string,Writer> _writersByExtension = ... //All writers here
    
      public void Init(IEnumerable<Writer> writers)
      {
         foreach ( var writer in writers )
         {
            _writersByExtension.Add( writer.SupportedExtension, writer );
         }
      }
    
      public void Write(string text, string file) 
      {
        Writer w = _writersByExtension.TryGetValue( GetExtension(file) );
        if (w == null)
        {
           throw new Exception("...");
        }
        w.Write(text, file);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm building a data access layer for an application. Typically I have
Let me try to explain what I need. I have a server that is
I have a French site that I want to parse, but am running into
Let's say that I'm currently designing an application where I will need to use
let's say i have three tables, each one relates to another, when i need
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.