我建议将这个单一的类拆分成几个类。总的来说,静态实用程序类会违反许多规则,其中最重要的是关注点分离。虽然是的,这个类中的所有方法都涉及压缩,但它们所关注的是压缩不同的内容。有些方法压缩字节数组,有些压缩字符串,有些压缩文件。我会将这个单一的实用程序拆分成多个实用程序:
public static class ByteCompression
{
public static Byte[] Compress(Byte[] input);
public static Byte[] Compress(Byte[] input, Format format);
public static Byte[] Compress(Byte[] input, Format format, Level level);
public static Byte[] Decompress(Byte[] input);
public static Byte[] Decompress(Byte[] input, Format format);
}
public static class StringCompression
public static String Compress(String input);
public static String Compress(String input, Format format);
public static String Compress(String input, Format format, Level level);
public static String Decompress(String input);
public static String Decompress(String input, Format format);
}
public static class FileCompression
{
public static void Compress(String input_file_path, String output_file_path);
public static void Compress(String input_file_path, String output_file_path, Format format);
public static void Compress(String input_file_path, String output_file_path, Format format, Level level);
public static void Decompress(String input_file_path, String output_file_path);
public static void Decompress(String input_file_path, String output_file_path, Format format);
}
public static FolderCompression
{
public static void Compress(String input_folder_path, String output_file_path);
public static void Compress(String input_folder_path, String output_file_path, Format format);
public static void Compress(String input_folder_path, String output_file_path, Format format, Level level);
public static void Decompress(String input_file_path, String output_file_path);
public static void Decompress(String input_file_path, String output_file_path, Format format);
}
以上的实用类减少了重复代码,更好地封装了目的,与其成员方法更加协调,在意图上更加清晰。你有四个静态实用程序类型而不是一个,但这样做没有违反太多规则/最佳实践。尽量避免单块式的、全功能的实用程序类。如果可以的话,找到一种方法将它们变为实例类,而不是静态类,特别是如果在整个压缩/解压缩方法中使用了任何共享数据,则会提高线程安全性。
编辑:
如andy所评论的那样,更理想的实现方式应该使用扩展方法。文件和文件夹压缩作为扩展实现起来有点困难,但我已经尝试过了。以下示例更好地实现了我想要的:将名词(或主题)与动词(或操作)分离,提供了一个更清洁的API,最终具有更少的重复性,保持关注点的分离,并且被正确地封装。
public static class ByteCompressionExtensions
{
public static byte[] Compress(this byte[] input);
public static byte[] Compress(this byte[] input, Format format);
public static byte[] Compress(this byte[] input, Format format, Level level);
public static byte[] Decompress(this byte[] input);
public static byte[] Decompress(this byte[] input, Format format);
}
byte[] myArray = new byte[] { ... };
byte[] compArray = myArray.Compress();
public static class StringCompressionExtensions
{
public static byte[] Compress(this string input);
public static byte[] Compress(this string input, Format format);
public static byte[] Compress(this string input, Format format, Level level);
public static string Decompress(this byte[] input);
public static string Decompress(this byte[] input, Format format);
}
string myStr = "A string!";
byte[] compArray = myStr.Compress();
myStr = compArray.Decompress();
public static class FileCompressionExtensions
{
public static void Compress(this FileInfo input, FileInfo output);
public static void Compress(this FileInfo input, FileInfo output, Format format);
public static void Compress(this FileInfo input, FileInfo output, Format format, Level level);
public static void Decompress(this FileInfo input, FileInfo output);
public static void Decompress(this FileInfo input, FileInfo output, Format format);
}
FileInfo myFile = new FileInfo(input_file_path);
FileInfo myCompFile = new FileInfo(output_file_path);
myFile.Compress(myCompFile);
myCompFile.Decompress(myFile);
public static class FolderCompressionExtensions
{
public static void Compress(this DirectoryInfo input, DirectoryInfo output);
public static void Compress(this DirectoryInfo input, DirectoryInfo output, Format format);
public static void Compress(this DirectoryInfo input, DirectoryInfo output, Format format, Level level);
public static void Decompress(this DirectoryInfo input, DirectoryInfo output);
public static void Decompress(this DirectoryInfo input, DirectoryInfo output, Format format);
}
DirectoryInfo myDir = new DirectoryInfo(input_folder_path);
DirectoryInfo myCompDir = new DirectoryInfo(output_folder_path);
myDir.Compress(myCompDir);
myCompDir.Decompress(myDir);