Last active
September 26, 2015 03:48
-
-
Save JeffreyZhao/3f1d36e83a1acb4546fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 已知有如下可用类型 | |
public interface ITextWriter { | |
void Write(string text); | |
void Write(string text, int startIndex, int count); | |
void Write(char[] chars); | |
void Write(char[] chars, int startIndex, int count); | |
} | |
// 需实现以下类型,输出value对应的字符串(不考虑本地化)。 | |
// 例如: 1234567890 => "1234567890" | |
public interface ITextWriterUtils { | |
void Write(ITextWriter writer, int value); | |
} | |
// 参考实现如下。 | |
// 优点:简单。 | |
// 缺点:性能较低。 | |
public class SampletTextWriterUtils : ITextWriterUtils { | |
public void Write(ITextWriter writer, int value) { | |
writer.Write(value.ToString(CultureInfo.InvariantCulture)); | |
} | |
} | |
// 问题: | |
// 1) 为什么说上述实现性能较差? | |
// 2) 请提供一个高效的实现,框架如下。 | |
public class FasterTextWriterUtils : ITextWriterUtils { | |
public void Write(ITextWriter writer, int value) { | |
// ... | |
} | |
} | |
// 补充要求: | |
// 这种工具方法本身自然需要支持在多线程环境中使用,即 | |
// 可以由多个线程同时调用(向多个writer写入数据)。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment