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 8109589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:28:18+00:00 2026-06-06T01:28:18+00:00

I’m trying to implement a preferences system where the programmer can specify preference names,

  • 0

I’m trying to implement a preferences system where the programmer can specify preference names, types (boolean, integer, string, etc.), and optionally their default values. What I’ve been dwelling on for a while and can’t come up with is a generic solution for storing to / loading from disk. I want the design to be generic enough to handle multiple formats (i.e. text files, the Windows Registry, or Apple’s Property Lists). The simple solution would be to make transformers for each storage format and somewhere along the line when the storage format is already chosen, iterate through preferences and switch-case on their types.

I’ve been told doing a switch-case on a type isn’t a good solution and I understand why: if I add a type, I need to go and modify all those switch-case blocks. So what should I do instead? The usual answer is to have the objects whose types were being checked implement a common interface and know how to perform the actions themselves.

But that seems ridiculous to me, to have an preference value know how to store itself in a text file, in the Windows registery, and in an Apple property List. That just moves the problem. Instead of adding a new type requiring me to go and modify the transformers, if I add a new transformer, I need to go and modify all the types!

I imagine this is a common design problem but I can’t find any good solutions out there.

  • 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-06-06T01:28:19+00:00Added an answer on June 6, 2026 at 1:28 am

    Classic use of the visitor pattern. Though you sometimes see the pattern expressed more generically, here is some psuedo-code of a persistance layer via the Visitor pattern.

    Note that the preferences dont need to know how to save themselves, they just need to pass themselves to the storage mechanism:

    abstract BasePreference
    {
        abstract Persist(PreferenceStorage ps);
    }
    
    
    NumericPreference : BasePreference
    {
       string Name;
       int Value;
       int Default
    
       Persist(PreferenceStorage ps)
       {
          ps.Save(this);
       }
    
    }
    
    
    StringPreference : BasePreference
    {
       string Name;
       string Value;
       string Default
    
       Persist(PreferenceStorage ps)
       {
          ps.Save(this);
       }   
    }
    
    
    DateRangePreference : BasePreference
    {
       string Name;
       DateTime StartOfRange;
       DateTime EndOfRange;
       DateTime DefaultStartOfRange;
       DateTime DefaultEndOfRange;
    
       Persist(PreferenceStorage ps)
       {
          ps.Save(this);
       }   
    }
    

    Next, the PreferenceStorage utilizes method overloading to allow seperate code to be run for each pref subtype. No switch blocks on types:

    abstract PreferenceStorage 
    {
       abstract void Save(NumericPreference pref);
       abstract void Save(StringPreference pref);
       abstract void Save(DateRangePreference pref);   
    }
    

    And finally, the subclasses of PreferenceStorage take care of the mechanics or saving the data:

    XmlPreferenceStorage : PreferenceStorage
    {
       void Save(NumericPreference pref)
       {
          // save number to xml 
       }
    
       void Save(StringPreference pref)
       {
          // save string to xml 
       }   
    
       void Save(DateRangePreference pref)
       {
          // save date range to xml 
       }      
    }
    
    
    
    RegistryPreferenceStorage : PreferenceStorage
    {
       void Save(NumericPreference pref)
       {
          // save number to registry 
       }
    
       void Save(StringPreference pref)
       {
          // save string to registry 
       }   
    
       void Save(DateRangePreference pref)
       {
          // save date range to registry 
       }      
    }
    

    (Uggg, why is this formtating so terribly? Sorry for the bad indenting, normally great code formatting on SO)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.