适配器模式(Adapter):Match interfaces of different classes合成模式(Composite):A tree structure of simple and composite objects装饰模式(Decorator):Add responsibilities to objects dynamically代理模式(Proxy):An object representing another object享元模式(Flyweight):A fine-grained instance used for efficient sharing门面模式(Facade):A single class that represents an entire subsystem桥梁模式(Bridge):Separates an object interface from its implementation
// "Adaptee" class Adaptee { // Methods public void SpecificRequest() { Console.WriteLine("Called SpecificRequest()" ); } }
// "Adapter" class Adapter : Adaptee, ITarget { // Implements ITarget interface public void Request() { // Possibly do some data manipulation // and then call SpecificRequest this.SpecificRequest(); } }
/**//// <summary> /// Client test /// </summary> public class Client { public static void Main(string[] args) { // Create adapter and place a request ITarget t = new Adapter(); t.Request(); } }
// Adapter pattern -- Structural example using System;
// "Target" class Target { // Methods virtual public void Request() { // Normal implementation goes here } }
// "Adapter" class Adapter : Target { // Fields private Adaptee adaptee = new Adaptee();
// Methods override public void Request() { // Possibly do some data manipulation // and then call SpecificRequest adaptee.SpecificRequest(); } }
// "Adaptee" class Adaptee { // Methods public void SpecificRequest() { Console.WriteLine("Called SpecificRequest()" ); } }
/**//// <summary> /// Client test /// </summary> public class Client { public static void Main(string[] args) { // Create adapter and place a request Target t = new Adapter(); t.Request(); } }