您如何处理用户偏好?
与大多数软件一样,用户可以指定他们希望如何处理某些事情。就我而言,用户可以指定他们喜欢的格式。有 3 个选项:不格式化、驼峰式大小写或正确大小写。我目前可以使用它,但感觉非常笨重和重复。这是班级的一个要点。
public static class Extensions
{
public static string GetPreferenceFormattedText(this string text, ApplicationPreferences applicationPreferences, bool pluralize)
{
if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.SameAsDatabase))
return text;
string formattedText = text.Replace('_', ' ');
formattedText = formattedText.MakeTitleCase();
formattedText = formattedText.Replace(" ", "");
if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.Prefixed))
return applicationPreferences.Prefix + formattedText;
return applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.CamelCase)
? formattedText.MakeFirstCharLowerCase()
: formattedText;
}
}
该方法本身并不觉得笨重。这就是它的称呼方式。每次我想要获取格式化文本时总是必须传递用户首选项似乎不是最好的方法。我最好创建一个常规类并通过构造函数传递应用程序首选项对象吗?
谢谢。
Like most software, users are able to specify how they'd like to handle certain things. In my case, users can specify what kind of formatting they would prefer. There are 3 options, leave unformatted, camel case or proper case. I currently have it working but it feels very clunky and repetitive. Here is a jist of the class.
public static class Extensions
{
public static string GetPreferenceFormattedText(this string text, ApplicationPreferences applicationPreferences, bool pluralize)
{
if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.SameAsDatabase))
return text;
string formattedText = text.Replace('_', ' ');
formattedText = formattedText.MakeTitleCase();
formattedText = formattedText.Replace(" ", "");
if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.Prefixed))
return applicationPreferences.Prefix + formattedText;
return applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.CamelCase)
? formattedText.MakeFirstCharLowerCase()
: formattedText;
}
}
The method itself doesn't really feel clunky. It's the way it's being called. Always having to pass the user preferences every time I want to obtain the formatted text doesn't seem like the best way to go. Would I be better off making a regular class and pass the application preferences object through the constructor?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是创建某种工厂类,然后您可以使用或从包含首选项的类的实例实例化工厂类。
使用工厂类,您可以获得一个 TextFormatter,返回的格式化程序实例将取决于首选项。
这是一个非常简单的示例,只是为了用一些代码来澄清我的答案。这并不是超级花哨,并且可能会使用更复杂的模式,但希望它是正确的起点。
定义一个界面和一些格式化程序
现在是一个示例首选项类
使用代码
One option would be to create some kind of factory class, you can then instantiate the factory class with or from the instance of the class containing the preferences.
Using the factory class you can get a TextFormatter, the instance of the formatter returned will depend on the preferences.
Here is a very simple example just to clarify my answer with some code. This is not super fancy and can potentially use more sofisticated patterns, but it is hopefully the right starting point.
Define an interface and some formatters
Now a sample preferences class
Using the code