重构两个基础类
您将如何重构这两个类以抽象出相似之处? 抽象类? 简单继承? 重构后的类会是什么样子?
public class LanguageCode
{
/// <summary>
/// Get the lowercase two-character ISO 639-1 language code.
/// </summary>
public readonly string Value;
public LanguageCode(string language)
{
this.Value = new CultureInfo(language).TwoLetterISOLanguageName;
}
public static LanguageCode TryParse(string language)
{
if (language == null)
{
return null;
}
if (language.Length > 2)
{
language = language.Substring(0, 2);
}
try
{
return new LanguageCode(language);
}
catch (ArgumentException)
{
return null;
}
}
}
public class RegionCode
{
/// <summary>
/// Get the uppercase two-character ISO 3166 region/country code.
/// </summary>
public readonly string Value;
public RegionCode(string region)
{
this.Value = new RegionInfo(region).TwoLetterISORegionName;
}
public static RegionCode TryParse(string region)
{
if (region == null)
{
return null;
}
if (region.Length > 2)
{
region = region.Substring(0, 2);
}
try
{
return new RegionCode(region);
}
catch (ArgumentException)
{
return null;
}
}
}
How would you refactor these two classes to abstract out the similarities? An abstract class? Simple inheritance? What would the refactored class(es) look like?
public class LanguageCode
{
/// <summary>
/// Get the lowercase two-character ISO 639-1 language code.
/// </summary>
public readonly string Value;
public LanguageCode(string language)
{
this.Value = new CultureInfo(language).TwoLetterISOLanguageName;
}
public static LanguageCode TryParse(string language)
{
if (language == null)
{
return null;
}
if (language.Length > 2)
{
language = language.Substring(0, 2);
}
try
{
return new LanguageCode(language);
}
catch (ArgumentException)
{
return null;
}
}
}
public class RegionCode
{
/// <summary>
/// Get the uppercase two-character ISO 3166 region/country code.
/// </summary>
public readonly string Value;
public RegionCode(string region)
{
this.Value = new RegionInfo(region).TwoLetterISORegionName;
}
public static RegionCode TryParse(string region)
{
if (region == null)
{
return null;
}
if (region.Length > 2)
{
region = region.Substring(0, 2);
}
try
{
return new RegionCode(region);
}
catch (ArgumentException)
{
return null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除非您有充分的理由进行重构(因为您将在不久的将来添加更多类),否则为这样一个小而人为的示例更改设计的代价将克服这种情况下维护或开销的收益。 无论如何,这是一个基于泛型和 lambda 表达式的可能设计。
Unless you have a strong reason for refactoring (because you are going to add more classes like those in near future) the penalty of changing the design for such a small and contrived example would overcome the gain in maintenance or overhead in this scenario. Anyhow here is a possible design based on generic and lambda expressions.
这取决于,如果他们不打算做更多的事情,那么我可能会让他们保持原样 - 恕我直言,在这种情况下,分解东西可能会更复杂。
It depends, if they are not going to do much more, then I would probably leave them as is - IMHO factoring out stuff is likely to be more complex, in this case.
我确信有更好的基于泛型的解决方案。 但还是试了一下。
编辑:正如评论所说,静态方法不能被重写,因此一种选择是保留它并使用 TwoLetterCode 对象并强制转换它们,但是,正如其他人已经指出的那样,这是相当无用的。
这个怎么样?
I'm sure there is a better generics based solution. But still gave it a shot.
EDIT: As the comment says, static methods can't be overridden so one option would be to retain it and use TwoLetterCode objects around and cast them, but, as some other person has already pointed out, that is rather useless.
How about this?
就目前而言,这两个方法由于静态方法而无法很好地重构。
您要么最终在基类上使用某种工厂方法,该方法返回该基类的类型(随后需要转换),要么您需要某种额外的帮助程序类。
考虑到额外的代码量以及随后转换为适当的类型,这是不值得的。
These two, as they stand, aren't going to refactor well because of the static methods.
You'd either end up with some kind of factory method on a base class that returns an a type of that base class (which would subsequently need casting) or you'd need some kind of additional helper class.
Given the amount of extra code and subsequent casting to the appropriate type, it's not worth it.
AbstractCode
)添加抽象方法,例如
在基类中重写
最后,对
string GetIsoName(string code)
执行相同的操作,例如这将重构两者。 克里斯·金普顿确实提出了一个重要问题:这种努力是否值得。
AbstractCode<T>
)add abstract methods like
override in base classes like
Finally, do the same with
string GetIsoName(string code)
, egThat will refactor the both. Chris Kimpton does raise the important question as to whether the effort is worth it.
您可以将它们组合成一个
Locale
类,该类存储语言代码和区域代码,具有区域和语言的访问器以及一个解析函数,该函数还允许像“en_gb”这样的字符串...这就是如何我见过各种框架中处理语言环境的情况。
You could maybe combine them into a
Locale
class, which stores both Language code and Region code, has accessors for Region and Language plus one parse function which also allows for strings like "en_gb"...That's how I've seen locales be handled in various frameworks.
这是一个相当简单的问题,对我来说,这就像一个家庭作业。
您显然可以看到代码中的公共位,并且我很确定您可以通过将这些东西放入超类中来自己尝试。
This is a rather simple question and to me smells awefully like a homework assignment.
You can obviously see the common bits in the code and I'm pretty sure you can make an attempt at it yourself by putting such things into a super-class.