设计模式--适配器模式/代理模式
1.适配器模式:把一个类适配到原有的接口上,可以组合 可以继承
一.继承的方式:
class RedisHelperInhert : RedisHelper, IHelper { public void Add<T>() { base.AddRedis<T>(); } public void Delete<T>() { base.DeleteRedis<T>(); } public void Query<T>() { base.QueryRedis<T>(); } public void Update<T>() { base.UpdateRedis<T>(); } }
二.组合的方式:
class RedisHelperCombination : IHelper { /// <summary> /// 1.字段属性方式组合 默认构造 特别强烈,而且写死了 /// </summary> private RedisHelper _RedisHelper = new RedisHelper(); private RedisHelper _RedisHelperCtor = null; /// <summary> /// 2.构造函数方式组合 ,实例化会一定传入,但是对象可以选择 /// </summary> /// <param name="redisHelper"></param> public RedisHelperCombination(RedisHelper redisHelper) { this._RedisHelper = redisHelper; } public RedisHelperCombination() { } /// <summary> /// 3.方法方式组合 对象可以选择,而且可有可无 /// </summary> /// <param name="redisHelper"></param> public void AddRedisHelper(RedisHelper redisHelper) { this._RedisHelperCtor = redisHelper; } public void Add<T>() { _RedisHelper.AddRedis<T>(); } public void Delete<T>() { _RedisHelper.DeleteRedis<T>(); } public void Query<T>() { _RedisHelper.QueryRedis<T>(); } public void Update<T>() { _RedisHelper.UpdateRedis<T>(); } }
调用如下:
Console.WriteLine("**************"); { IHelper helper = new RedisHelperInhert(); helper.Add<Program>(); helper.Delete<Program>(); helper.Query<Program>(); helper.Update<Program>(); } Console.WriteLine("**************"); { IHelper helper = new RedisHelperCombination(); helper.Add<Program>(); helper.Delete<Program>(); helper.Query<Program>(); helper.Update<Program>(); }
2.代理模式:通过代理类来访问业务类,在不修改业务类的前提下可以扩展功能
原有类:
class RealSubject:ISubject { public RealSubject() { Thread.Sleep(2000); long lResult = 0; for(int i = 0; i < 1000000; i++) { lResult += i; } Console.WriteLine("RealSubject被构造。。。"); } public bool GetSomething() { Console.WriteLine("坐车去火车站看看余票信息"); Thread.Sleep(3000); Console.WriteLine("到火车站,看到是有票的"); return true; } public void DoSomething() { Console.WriteLine("开始排队。。。"); Thread.Sleep(2000); Console.WriteLine("终于买到票了。。。"); } }
/// <summary> /// 代理:只能传达原有逻辑,不能新增业务逻辑 /// 包一层: /// 加日志:修改代理类完成 日志代理 避免对业务类修改 /// 单例: 修改代理类完成,搞个static(并不是强制单例,因为其他地方也可以new) 避免对业务类修改 /// 缓存:修改代理类完成,加个缓存逻辑 避免对业务类修改 /// </summary> public class ProxySubject:ISubject { private ISubject _iSubject = new RealSubject(); //private static ISubject _iSubject = new RealSubject(); 单例的例子 //private static Dictionary<string, bool> ProxySubjectDictionary = new Dictionary<string, bool>(); //缓存的例子 //public bool GetSomething() //{ // Console.WriteLine("before GetSomething"); // bool _BooleanResult = false; // string key = "GetSomething"; // if (ProxySubjectDictionary.ContainsKey(key)) // { // _BooleanResult = ProxySubjectDictionary[key]; // } // else // { // _BooleanResult = _iSubject.GetSomething(); // ProxySubjectDictionary.Add(key, _BooleanResult); // } // Console.WriteLine("after GetSomething"); // return _BooleanResult; //} /// <summary> /// 火车站查询火车票 /// </summary> /// <returns></returns> public bool GetSomething() { Console.WriteLine("before GetSomething"); bool _BooleanResult = _iSubject.GetSomething(); Console.WriteLine("after GetSomething"); return _BooleanResult; } public void DoSomething() { Console.WriteLine("before DoSomething"); _iSubject.DoSomething(); Console.WriteLine("after Dosomething"); } }
调用如下:
static void Main(string[] args) { { ISubject subject = new RealSubject(); subject.GetSomething(); subject.DoSomething(); } { ISubject subject = new ProxySubject(); subject.GetSomething(); subject.DoSomething(); } }
3.装饰者模式::可以动态为类型添加功能,甚至调整功能顺序,不改变原有的业务类
基类:继承加组合,结合了两者的优势
public class BaseStudentDecorator:AbstractStudent { private AbstractStudent _Student = null; public BaseStudentDecorator(AbstractStudent student) { this._Student = student; } public override void Study() { this._Student.Study(); //Console.WriteLine("视频代码"); } }
public class StudentPayDecorator:BaseStudentDecorator { public StudentPayDecorator(AbstractStudent student) : base(student) { } public override void Study() { Console.WriteLine("付费"); base.Study(); } }
public class StudentHomeworkDecorator:BaseStudentDecorator { public StudentHomeworkDecorator(AbstractStudent student) : base(student) { } public override void Study() { base.Study(); Console.WriteLine("巩固练习"); } }
public class StudentCommentDecorator : BaseStudentDecorator { public StudentCommentDecorator(AbstractStudent student) : base(student) { } public override void Study() { base.Study(); Console.WriteLine("点评"); } }
调用如下:
{ AbstractStudent student = new StudentVip() { Id = 123, Name = "ee" }; student = new StudentPayDecorator(student); student = new StudentHomeworkDecorator(student); student = new StudentCommentDecorator(student); //不修改业务类,可以随意的增加功能 装饰器 student.Study(); }