zoukankan      html  css  js  c++  java
  • 适配者模式(Adaptee)

    模式定义

    适配器模式(Adapter Pattern) :将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。适配器模式既可以作为类结构型模式,也可以作为对象结构型模式。如果适配者(三方类库修改)只需要修改适配器

    UML类图

    • 目标抽象类(Target) 客户需要用的特定接口
    • 适配器类(Adapter) 关联适配者类,实现目标抽象类接口
    • 适配者类(Adaptee) 被适配的角色,它定义了一个已经存在的接口,这个接口需要适配(一般只三方类库)
    • 客户端(Client) 针对目标抽象类进行编程

    代码结构

    public static class AdapteeApp
    	{
    		public static void Run()
    		{
    			Target target = new Adapter();
    			target.Request();
    		}
    	}
    	public class Target
    	{
    		public virtual void Request()
    		{
    			Console.WriteLine("Called Target Request()");
    		}
    	}
    	public class Adapter :Target
    	{
    		private Adaptee _adaptee = new Adaptee();
    
    		public override void Request()
    		{
    			_adaptee.SpecificRequest();
    		}
    
    	}
    	public class Adaptee
    	{
    		public void SpecificRequest()
    		{
    			Console.WriteLine("Called specificRequest()");
    		}
    	}
    

    情景模式

    在开发中用到适配者模式,一般指引入第三方类库,且第三方类库的接口并非完全一直。这里以缓存操作为例,项目刚开始缓存存在内存,随着项目数据的增多决定引入Redis做缓存,然而Redis的缓存操作接口并非与本系统操作接口完全一致,这里通过适配者模式解决(这点也体现依赖倒置:高层不应依赖底层,应依赖接口)。

    	public static class AdapteeApp
    	{
    		public static void Run()
    		{
    			CacheManage cache1 = new DictionaryCache();
    			cache1.Set("name", "LoveTomato");
    			Console.WriteLine(cache1.Get("name").ToString());
    
    			CacheManage cache2 = new RedisCacheAdapter();
    			cache2.Set("name", "LoveTomato");
    			Console.WriteLine(cache2.Get("name").ToString());
    		}
    	}
    	public interface CacheManage
    	{
    		object Get(string key);
    		void Set(string key, object value);
    		bool IsExist(string key);
    		void Remove(string key);
    		void Clear();
    	}
    
    	public class DictionaryCache : CacheManage
    	{
    		private Dictionary<string, object> _dic = new Dictionary<string, object>();
    		public void Clear()
    		{
    			_dic.Clear();
    		}
    		public object Get(string key)
    		{
    			object obj = null;
    			if (this.IsExist(key))
    			{
    				obj = _dic[key];
    			}
    			return obj;
    		}
    		public bool IsExist(string key)
    		{
    			bool b = false;
    			if (_dic.Keys.Contains(key))
    			{
    				b = true;
    			}
    			return b;
    		}
    		public void Remove(string key)
    		{
    			if (this.IsExist(key))
    			{
    				_dic.Remove(key);
    			}
    		}
    		public void Set(string key, object value)
    		{
    			_dic[key] = value;
    		}
    	}
    
    	public class RedisCacheAdapter : CacheManage
    	{
    		private RedisCache redisCache = new RedisCache();
    		public void Clear()
    		{
    			redisCache.Empty();
    		}
    		public object Get(string key)
    		{
    			return redisCache.Get(key);
    		}
    		public bool IsExist(string key)
    		{
    			return redisCache.IsSet(key);
    		}
    		public void Remove(string key)
    		{
    			redisCache.Delete(key);
    		}
    		public void Set(string key, object value)
    		{
    			redisCache.Set(key, value);
    		}
    	}
    	public class RedisCache
    	{
    		public void Empty()
    		{ }
    		public object Get(string key)
    		{
    			return null;
    		}
    		public void Set(string key, object value)
    		{ }
    		public bool IsSet(string key)
    		{
    			return false;
    		}
    		public void Delete(string key)
    		{ }
    	}
    
  • 相关阅读:
    Educational Codeforces Round 85 D. Minimum Euler Cycle(模拟/数学/图)
    Educational Codeforces Round 85 C. Circle of Monsters(贪心)
    NOIP 2017 提高组 DAY1 T1小凯的疑惑(二元一次不定方程)
    Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)
    Educational Codeforces Round 85 A. Level Statistics(水题)
    IOS中的三大事件
    用Quartz 2D画小黄人
    strong、weak、copy、assign 在命名属性时候怎么用
    用代码生成UINavigationController 与UITabBarController相结合的简单QQ框架(部分)
    Attempting to badge the application icon but haven't received permission from the user to badge the application错误解决办法
  • 原文地址:https://www.cnblogs.com/LoveTomato/p/8360987.html
Copyright © 2011-2022 走看看