7.适配器模式
1 #region 7.适配器模式(Adapter):将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作。 2 /// <summary> 3 /// 抽象适配类 4 /// </summary> 5 public interface IScoreOperation 6 { 7 // 成绩排序 8 int[] Sort(int[] array); 9 // 成绩查找 10 int Search(int[] array, int key); 11 } 12 13 /// <summary> 14 /// 适配器:成绩操作适配器类 15 /// </summary> 16 public class OperationAdapter : IScoreOperation 17 { 18 private QuickSortHelper sortTarget; 19 private BinarySearchHelper searchTarget; 20 21 public OperationAdapter() 22 { 23 sortTarget = new QuickSortHelper(); 24 searchTarget = new BinarySearchHelper(); 25 } 26 27 public int Search(int[] array, int key) 28 { 29 return searchTarget.BinarySearch(array, key); 30 } 31 32 public int[] Sort(int[] array) 33 { 34 return sortTarget.QuickSort(array); 35 } 36 } 37 38 #region 适配者 39 40 /// <summary> 41 /// 适配者A:快速排序类 42 /// </summary> 43 public class QuickSortHelper 44 { 45 public int[] QuickSort(int[] array) 46 { 47 Sort(array, 0, array.Length - 1); 48 return array; 49 } 50 51 public void Sort(int[] array, int p, int r) 52 { 53 int q = 0; 54 if (p < r) 55 { 56 q = Partition(array, p, r); 57 Sort(array, p, q - 1); 58 Sort(array, q + 1, r); 59 } 60 } 61 62 public int Partition(int[] array, int p, int r) 63 { 64 int x = array[r]; 65 int j = p - 1; 66 67 for (int i = p; i <= r - 1; i++) 68 { 69 if (array[i] <= x) 70 { 71 j++; 72 Swap(array, j, i); 73 } 74 } 75 76 Swap(array, j + 1, r); 77 return j + 1; 78 } 79 80 public void Swap(int[] array, int i, int j) 81 { 82 int t = array[i]; 83 array[i] = array[j]; 84 array[j] = t; 85 } 86 } 87 88 public class BinarySearchHelper 89 { 90 public int BinarySearch(int[] array, int key) 91 { 92 int low = 0; 93 int high = array.Length - 1; 94 95 while (low <= high) 96 { 97 int mid = (low + high) / 2; 98 int midVal = array[mid]; 99 100 if (midVal < key) 101 { 102 low = mid + 1; 103 } 104 else if (midVal > key) 105 { 106 high = mid - 1; 107 } 108 else 109 { 110 return 1; // 找到元素返回1 111 } 112 } 113 114 return -1; // 未找到元素返回-1 115 } 116 } 117 118 #endregion 119 120 121 122 123 #endregion
8.桥接模式
1 #region 8.桥接模式(Bridge)。将抽象部分与其实现部分分离,使得他们都可以独立地变化。桥接模式主要使用抽象关联取代传统的多重继承,将类之间的静态继承关系转换为动态地对象组合关系。 2 3 /// <summary> 4 /// 抽象抽象类 5 /// </summary> 6 public abstract class Image 7 { 8 protected ImageImplementor imageImpl; 9 public void SetImageImplementor(ImageImplementor imageImpl) 10 { 11 this.imageImpl = imageImpl; 12 } 13 14 public abstract void ParstFile(string fileName); 15 } 16 17 18 public class PngImage : Image 19 { 20 public override void ParstFile(string fileName) 21 { 22 imageImpl.DoPaint(); 23 Console.WriteLine("{0} : 格式为PNG",fileName); 24 } 25 } 26 27 public class BMPImage : Image 28 { 29 public override void ParstFile(string fileName) 30 { 31 imageImpl.DoPaint(); 32 Console.WriteLine("{0} : 格式为BMP",fileName); 33 } 34 } 35 36 public class GifImage : Image 37 { 38 public override void ParstFile(string fileName) 39 { 40 imageImpl.DoPaint(); 41 Console.WriteLine("{0} : 格式为Gif",fileName); 42 } 43 } 44 45 46 47 /// <summary> 48 /// 抽象实现类 49 /// </summary> 50 public abstract class ImageImplementor 51 { 52 public abstract void DoPaint(); 53 } 54 55 #region 具体实现类 56 57 public class WindowsImplementor : ImageImplementor 58 { 59 public override void DoPaint() 60 { 61 Console.WriteLine("在Windows系统中显示图像"); 62 } 63 } 64 65 public class LinuxImplementor : ImageImplementor 66 { 67 public override void DoPaint() 68 { 69 Console.WriteLine("在Linux系统中显示图像"); 70 } 71 } 72 73 public class UnixImplementor : ImageImplementor 74 { 75 public override void DoPaint() 76 { 77 Console.WriteLine("在Unix系统中显示图像"); 78 } 79 } 80 #endregion 81 82 83 84 85 86 87 #endregion
9.组合模式
1 #region 9.组合模式(Composite)。 组合多个对象形成树形结构以表示具有"整体-部分"关系的层次结构。 2 3 /// <summary> 4 /// 抽象构件类 5 /// </summary> 6 public abstract class AbstractFile 7 { 8 protected string name; 9 protected AbstractFile(string name) 10 { 11 this.name = name; 12 Console.WriteLine("抽象类的构造方法"); 13 } 14 public abstract void Add(AbstractFile file); 15 public abstract void Remove(AbstractFile file); 16 public abstract AbstractFile GetChild(int index); 17 public abstract void KillVirus(); 18 19 } 20 21 #region 叶子构件 22 23 public class ImageFile : AbstractFile 24 { 25 public ImageFile(string name) : base(name) 26 { 27 Console.WriteLine("实现类的构造方法"); 28 } 29 30 public override void Add(AbstractFile file) 31 { 32 Console.WriteLine("对不起,系统不支持该方法! "); 33 } 34 35 public override AbstractFile GetChild(int index) 36 { 37 Console.WriteLine("对不起,系统不支持该方法! "); 38 return null; 39 } 40 41 public override void KillVirus() 42 { 43 Console.WriteLine("-------对图像文件'{0}进行杀毒'",name); 44 } 45 46 public override void Remove(AbstractFile file) 47 { 48 Console.WriteLine("对不起,系统不支持该方法! "); 49 } 50 } 51 52 public class TextFile : AbstractFile 53 { 54 public TextFile(string name) : base(name) 55 { 56 } 57 58 public override void Add(AbstractFile file) 59 { 60 Console.WriteLine("对不起,系统不支持该方法! "); 61 } 62 63 public override AbstractFile GetChild(int index) 64 { 65 Console.WriteLine("对不起,系统不支持该方法! "); 66 return null; 67 } 68 69 public override void KillVirus() 70 { 71 Console.WriteLine("-------对文本文件'{0}进行杀毒'", name); 72 } 73 74 public override void Remove(AbstractFile file) 75 { 76 Console.WriteLine("对不起,系统不支持该方法! "); 77 } 78 } 79 80 public class VideoFile : AbstractFile 81 { 82 public VideoFile(string name) : base(name) 83 { 84 } 85 86 public override void Add(AbstractFile file) 87 { 88 Console.WriteLine("对不起,系统不支持该方法! "); 89 } 90 91 public override AbstractFile GetChild(int index) 92 { 93 Console.WriteLine("对不起,系统不支持该方法! "); 94 return null; 95 } 96 97 public override void KillVirus() 98 { 99 Console.WriteLine("-------对音频文件'{0}进行杀毒'", name); 100 } 101 102 public override void Remove(AbstractFile file) 103 { 104 Console.WriteLine("对不起,系统不支持该方法! "); 105 } 106 } 107 108 #endregion 109 /// <summary> 110 /// 容器构件 111 /// </summary> 112 public class Folder : AbstractFile 113 { 114 public Folder(string name) : base(name) 115 { 116 } 117 private IList<AbstractFile> fileList = new List<AbstractFile>(); 118 119 public override void Add(AbstractFile file) 120 { 121 fileList.Add(file); 122 } 123 124 public override AbstractFile GetChild(int index) 125 { 126 return fileList[index]; 127 } 128 129 public override void KillVirus() 130 { 131 Console.WriteLine("----对文件夹'{0}'进行杀毒",name); 132 foreach (var item in fileList) 133 { 134 item.KillVirus(); 135 } 136 } 137 138 public override void Remove(AbstractFile file) 139 { 140 fileList.Remove(file); 141 } 142 } 143 144 public class NothingFile : ImageFile 145 { 146 public NothingFile(string name) : base(name) 147 { 148 Console.WriteLine("NothingFile构造函数"); 149 } 150 } 151 152 153 #endregion
10.装饰模式
1 #region 10.装饰模式(Decorator)。动态地给一个对象增加一些额外的职责。 2 3 /// <summary> 4 /// 抽象构件 5 /// </summary> 6 public abstract class Component 7 { 8 public abstract void Dispaly(); 9 } 10 11 #region 具体构件 12 public class Window : Component 13 { 14 public override void Dispaly() 15 { 16 Console.WriteLine("显示窗体"); 17 } 18 } 19 20 public class TextBox : Component 21 { 22 public override void Dispaly() 23 { 24 Console.WriteLine("显示文本框"); 25 } 26 } 27 28 public class ListBox : Component 29 { 30 public override void Dispaly() 31 { 32 Console.WriteLine("显示列表框"); 33 } 34 } 35 36 #endregion 37 38 /// <summary> 39 /// 抽象装饰 40 /// </summary> 41 public class ComponentDecorator : Component 42 { 43 private Component component; 44 45 public ComponentDecorator(Component component) 46 { 47 this.component = component; 48 49 } 50 public override void Dispaly() 51 { 52 component.Dispaly(); 53 } 54 } 55 56 /// <summary> 57 /// 具体装饰 58 /// </summary> 59 public class ScrollBarDecorator : ComponentDecorator 60 { 61 public ScrollBarDecorator(Component component) : base(component) 62 { 63 64 } 65 public override void Dispaly() 66 { 67 this.SetScrollBar(); 68 base.Dispaly(); 69 } 70 public void SetScrollBar() 71 { 72 Console.WriteLine("为构件增加滚动条"); 73 } 74 } 75 76 public class BlackBorderDecorator : ComponentDecorator 77 { 78 public BlackBorderDecorator(Component component) : base(component) 79 { 80 81 } 82 public override void Dispaly() 83 { 84 this.SetBlacktBorder(); 85 base.Dispaly(); 86 } 87 88 public void SetBlacktBorder() 89 { 90 Console.WriteLine("为构件增加黑色边框。!"); 91 } 92 93 } 94 95 96 #endregion
11.外观模式
1 #region 11.外观模式(Facade)。外部与一个子系统的通信通过一个统一的外观角色进行,为子系统中的一组接口提供一个一致的入口。 2 3 #region 子系统类 4 /// <summary> 5 /// 文件读取类:子系统A 6 /// </summary> 7 public class FileReader 8 { 9 public string Read(string fileNameSrc) 10 { 11 Console.WriteLine("读取文件,获取明文:"); 12 string result = string.Empty; 13 using (System.IO.FileStream fsRead = new System.IO.FileStream(fileNameSrc, System.IO.FileMode.Open)) 14 { 15 int fsLen = (int)fsRead.Length; 16 byte[] heByte = new byte[fsLen]; 17 int r = fsRead.Read(heByte, 0, heByte.Length); 18 result = System.Text.Encoding.UTF8.GetString(heByte); 19 } 20 21 return result; 22 } 23 } 24 25 /// <summary> 26 /// 数据加密类:子系统B 27 /// </summary> 28 public class CipherMachine 29 { 30 public string Encrypt(string plainText) 31 { 32 Console.WriteLine("数据加密,将明文转换为密文:"); 33 StringBuilder result = new StringBuilder(); 34 35 for (int i = 0; i < plainText.Length; i++) 36 { 37 string ch = Convert.ToString(plainText[i] % 7); 38 result.Append(ch); 39 } 40 41 string encryptedResult = result.ToString(); 42 Console.WriteLine(encryptedResult); 43 return encryptedResult; 44 } 45 } 46 47 /// <summary> 48 /// 文件保存类:子系统C 49 /// </summary> 50 public class FileWriter 51 { 52 public void Write(string encryptedStr, string fileNameDes) 53 { 54 Console.WriteLine("保存密文,写入文件:"); 55 byte[] myByte = System.Text.Encoding.UTF8.GetBytes(encryptedStr); 56 using (System.IO.FileStream fsWrite = new System.IO.FileStream(fileNameDes, System.IO.FileMode.Append)) 57 { 58 fsWrite.Write(myByte, 0, myByte.Length); 59 }; 60 61 Console.WriteLine("写入文件成功:100%"); 62 } 63 } 64 #endregion 65 66 /// <summary> 67 /// 外观类 68 /// </summary> 69 public class EncryptFacade 70 { 71 private FileReader reader; 72 private CipherMachine cipher; 73 private FileWriter writer; 74 75 public EncryptFacade() 76 { 77 reader = new FileReader(); 78 cipher = new CipherMachine(); 79 writer = new FileWriter(); 80 } 81 82 public void FileEncrypt(string fileNameSrc, string fileNameDes) 83 { 84 string plainStr = reader.Read(fileNameSrc); 85 string encryptedStr = cipher.Encrypt(plainStr); 86 writer.Write(encryptedStr, fileNameDes); 87 } 88 } 89 90 91 92 #endregion
12.享元模式
1 #region 12.享元模式(Flyweight)。 运用共享技术有效地支持大量细粒度对象的复用。 2 3 public class Coordinates 4 { 5 public int X { get; set; } 6 public int Y { get; set; } 7 public Coordinates() { } 8 public Coordinates(int x, int y) 9 { 10 this.X = x; 11 this.Y = y; 12 } 13 } 14 15 /// <summary> 16 /// 抽象享元类 17 /// </summary> 18 public abstract class IgoChessman 19 { 20 public abstract string GetColor(); 21 public void Display(Coordinates coord) 22 { 23 Console.WriteLine("妻子颜色:{0},旗子位置:{1}", GetColor(), coord.X + "," + coord.Y); 24 } 25 } 26 /// <summary> 27 /// 具体享元类 28 /// </summary> 29 public class BlackChessman : IgoChessman 30 { 31 public override string GetColor() 32 { 33 return "黑色"; 34 } 35 } 36 37 /// <summary> 38 /// 具体享元类 39 /// </summary> 40 public class WhiteChessman : IgoChessman 41 { 42 public override string GetColor() 43 { 44 return "白色"; 45 } 46 } 47 48 /// <summary> 49 /// 享元工厂类 50 /// </summary> 51 public class IgoChessmanFactory 52 { 53 private static readonly IgoChessmanFactory instance = new IgoChessmanFactory(); 54 private static Hashtable ht;//使用Hashtable来存储享元对象,充当享元池。 55 56 private IgoChessmanFactory() 57 { 58 ht = new Hashtable(); 59 IgoChessman blackChesssman = new BlackChessman(); 60 IgoChessman whiteChessman = new WhiteChessman(); 61 ht.Add("b", blackChesssman); 62 ht.Add("w", whiteChessman); 63 } 64 65 public static IgoChessmanFactory GetInstance() 66 { 67 return instance; 68 } 69 70 public IgoChessman GetIgoChessman(string color) 71 { 72 IgoChessman chess = ht[color] as IgoChessman; 73 return chess; 74 } 75 76 } 77 78 79 #endregion
13.代理模式
1 #region 13.代理模式(Proxy)。给某一个对象提供一个代理,并由该代理对象控制对原对象的引用。 2 3 /// <summary> 4 /// 抽象代理 5 /// </summary> 6 public interface ISearcher 7 { 8 string DoSearch(string userID, string keyword); 9 } 10 11 /// <summary> 12 /// 被代理类 13 /// </summary> 14 public class RealSearcher 15 { 16 public string DoSearch(string userID, string keyword) 17 { 18 Console.WriteLine("{0} 使用关键字 {1}",userID,keyword); 19 return "返回具体内容"; 20 } 21 } 22 23 /// <summary> 24 /// 具体代理类. 25 /// </summary> 26 public class ProxySearcher : ISearcher 27 { 28 private RealSearcher searcher = new RealSearcher(); 29 30 public string DoSearch(string userID, string keyword) 31 { 32 string result = searcher.DoSearch(userID, keyword); 33 return result; 34 } 35 } 36 37 38 39 40 #endregion