One option is to create some kind of factory class, then you can create an instance of the factory class with or from an instance of the class containing the settings.
Using the factory class, you can get a TextFormatter, an instance of the returned formatting will depend on the settings.
Here is a very simple example to clarify my answer with some code. This is not very fantasy and could potentially use more complex patterns, but hopefully this is the right starting point.
Define an interface and some formats
public interface IIdentifierFormatter { string FormatText(string text); } public class UnformattedIdenifierFormatter : IIdentifierFormatter { public string FormatText(string text) { return text; } } public class CamelCaseIdenifierFormatter : IIdentifierFormatter { public string FormatText(string text) {
Now an example of a preference class
enum NamingConvention { Unformatted, CamelCase, ProperCase } public class Preferences { public NamingConvention FieldNamingConvention { get; set; } // .. Other settings // Function to get the formatter depending on the FieldNamingConvention public IIdentifierFormatter GetFieldNameFormatter() { switch (FieldNamingConvention) { case NamingConvention.Unformatted: return new ProperCaseIdenifierFormatter(); case NamingConvention.CamelCase: return new ProperCaseIdenifierFormatter(); case NamingConvention.ProperCase: return new ProperCaseIdenifierFormatter(); default: throw new Exception("Invalid or unsupported field naming convention."); } } }
Using code
// Preferences loaded from some source, // for the example I just initialized it here. Preferences pref = new Preferences(); pref.FieldNamingConvention = NamingConvention.CamelCase; // Get the formatter IIdentifierFormatter formatter = pref.GetFieldNameFormatter(); string formatted = formatter.FormatText("the_name_to_format");
Chris taylor
source share