.net design patterns - adapter mode

1. Structural Design Patterns: Focus on the interaction between class and class

Adapter mode: a type adapted to interface to the original, may be combined, you can inherit

The following is a different implementation interfaces and

 1 /// <summary>
 2     /// 数据访问接口
 3     /// </summary>
 4     public interface IHelper
 5     {
 6         void Add<T>();
 7         void Delete<T>();
 8         void Update<T>();
 9         void Query<T>();
10     }
 1  public class MysqlHelper : IHelper
 2     {
 3         public void Add<T>()
 4         {
 5             Console.WriteLine("This is {0} Add", this.GetType().Name);
 6         }
 7         public void Delete<T>()
 8         {
 9             Console.WriteLine("This is {0} Delete", this.GetType().Name);
10         }
11         public void Update<T>()
12         {
13             Console.WriteLine("This is {0} Update", this.GetType().Name);
14         }
15         public void Query<T>()
16         {
17             Console.WriteLine("This is {0} Query", this.GetType().Name);
18         }
19     }
 1  public class OracleHelper : IHelper
 2     {
 3         public void Add<T>()
 4         {
 5             Console.WriteLine("This is {0} Add", this.GetType().Name);
 6         }
 7         public void Delete<T>()
 8         {
 9             Console.WriteLine("This is {0} Delete", this.GetType().Name);
10         }
11         public void Update<T>()
12         {
13             Console.WriteLine("This is {0} Update", this.GetType().Name);
14         }
15         public void Query<T>()
16         {
17             Console.WriteLine("This is {0} Query", this.GetType().Name);
18         }
19     }
 1   public class SqlserverHelper : IHelper
 2     {
 3         public void Add<T>()
 4         {
 5             Console.WriteLine("This is {0} Add", this.GetType().Name);
 6         }
 7         public void Delete<T>()
 8         {
 9             Console.WriteLine("This is {0} Delete", this.GetType().Name);
10         }
11         public void Update<T>()
12         {
13             Console.WriteLine("This is {0} Update", this.GetType().Name);
14         }
15         public void Query<T>()
16         {
17             Console.WriteLine("This is {0} Query", this.GetType().Name);
18         }
19     }

transfer

 1 Console.WriteLine("*****************************");
 2                 {
 3                     IHelper helper = new SqlserverHelper();
 4                     helper.Add<Program>();
 5                     helper.Delete<Program>();
 6                     helper.Update<Program>();
 7                     helper.Query<Program>();
 8                 }
 9                 Console.WriteLine("*****************************");
10                 {
11                     IHelper helper = new MysqlHelper();
12                     helper.Add<Program>();
13                     helper.Delete<Program>();
14                     helper.Update<Program>();
15                     helper.Query<Program>();
16                 }

If you have to support redis, but redisHelper not realize IHelper interface to see how the expansion

 1  /// <summary>
 2     /// 第三方提供的  openstack  servicestack
 3     /// 不能修改
 4     /// </summary>
 5     public class RedisHelper
 6     {
 7         public void AddRedis<T>()
 8         {
 9             Console.WriteLine("This is {0} Add", this.GetType().Name);
10         }
11         public void DeleteRedis<T>()
12         {
13             Console.WriteLine("This is {0} Delete", this.GetType().Name);
14         }
15         public void UpdateRedis<T>()
16         {
17             Console.WriteLine("This is {0} Update", this.GetType().Name);
18         }
19         public void QueryRedis<T>()
20         {
21             Console.WriteLine("This is {0} Query", this.GetType().Name);
22         }
23     }

1. inherited wording

. 1   ///  <Summary> 
2      /// Class Adapter Mode
 3      /// adapter: meet IHelper; additional direct successor RedisHelper
 . 4      ///  </ Summary> 
. 5      public  class RedisHelperInherit: RedisHelper, IHelper
 . 6      {
 . 7          public  void the Add < T> ()
 . 8          {
 . 9              Base .AddRedis <T> ();
 10          }
 . 11          public  void the Delete <T> ()
 12 is          {
 13 is              Base .DeleteRedis <T> ();
 14          }
15         public void Update<T>()
16         {
17             base.UpdateRedis<T>();
18         }
19         public void Query<T>()
20         {
21             base.QueryRedis<T>();
22         }
23     }

2. achieved by a combination of

. 1  ///  <Summary> 
2      /// Object Adapter Mode
 3      /// adapter: meet IHelper; a composition came
 . 4      ///  </ Summary> 
. 5      public  class RedisHelperCombination: IHelper
 . 6      {
 . 7          public RedisHelperCombination ()
 . 8          {}
 . 9  
10          ///  <Summary> 
. 11          /// . 1 field properties in combination default configuration is particularly strong, and write die
 12 is          ///  </ Summary> 
13 is          Private RedisHelper _RedisHelper = new new RedisHelper (); // composition came 
14  
15         Private RedisHelper _RedisHelperCtor = null ;
 16          ///  <Summary> 
. 17          /// 2 constructor will instantiate some combination passed, but the object may be selected
 18 is          ///  </ Summary> 
. 19          ///  <param name = " redisHelper "> </ param> 
20 is          public RedisHelperCombination (redisHelper redisHelper)
 21 is          {
 22 is              the this ._RedisHelperCtor = redisHelper;
 23 is          }
 24  
25          ///  <Summary> 
26 is          /// . 3 method objects can be selected in combination, and dispensable
 27          ///  </ Summary> 
28          /// <param name="redisHelper"></param>
29         public void AddRedisHelper(RedisHelper redisHelper)
30         {
31             this._RedisHelperCtor = redisHelper;
32         }
33 
34 
35         public void Add<T>()
36         {
37             this._RedisHelper.AddRedis<T>();
38         }
39         public void Delete<T>()
40         {
41             this._RedisHelper.DeleteRedis<T>();
42         }
43         public void Update<T>()
44         {
45             this._RedisHelper.UpdateRedis<T>();
46         }
47         public void Query<T>()
48         {
49             this._RedisHelper.QueryRedis<T>();
50         }
51     }

This would like to extend implementation class IHelper

 1  Console.WriteLine("*****************************");
 2                 {
 3                     IHelper helper = new RedisHelperInherit();
 4                     helper.Add<Program>();
 5                     helper.Delete<Program>();
 6                     helper.Update<Program>();
 7                     helper.Query<Program>();
 8                 }
 9                 {
10                     RedisHelperInherit helper = new RedisHelperInherit();
11                     helper.AddRedis<Program>();//强侵入性
12                 }
13                 Console.WriteLine("*****************************");
14                 {
15                     IHelper helper = new RedisHelperCombination();
16                     helper.Add<Program>();
17                     helper.Delete<Program>();
18                     helper.Update<Program>();
19                     helper.Query<Program>();
20                 }

 

Guess you like

Origin www.cnblogs.com/Spinoza/p/11462159.html