适配器模式

Posted HowieGo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适配器模式相关的知识,希望对你有一定的参考价值。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace DesignPattern.StructuralPattern
 8 {
 9     #region 适配器模式
10 
11     //定义:接口转换,方法复用
12     //把一个类的接口变换为客户所期待的另一种接口,从而使原本接口不匹配无法一起工作的两个类能够一起工作。
13     //类适配器模式(继承) 优点:使用集成可以更改被适配类的方法实现 缺点:继承增加耦合
14     //对象适配器模式(组合)优点:使用组合的方式,更符合组合复用原则,也更利于扩展,耦合度低,建议使用的方式
15 
16     //实例
17     //RCW  但为了使.NET程序象使用.NET对象一样使用COM组件,微软在处理方式上采用了Adapter模式,对COM对象进行包装,这个包装类就是RCW(Runtime Callable Wrapper)
18     //DataAdapter 
19     #endregion
20     public class AdapterPattern : AdapteeInheritance, ITarget
21     {
22         //类适配器模式
23         public void InheritanceMethod()
24         {
25             this.ReuseMethod();
26         }
27 
28         //对象适配器模式 1.直接作为内部变量 2.作为构造函数参数传入
29         AdapteeCombination adaptee = new AdapteeCombination();
30         public void CombinationMethod()
31         {
32             adaptee.ReuseMethod();
33         }
34     }
35 
36     public interface ITarget
37     {
38         void InheritanceMethod();
39         void CombinationMethod();
40     }
41 
42     public class AdapteeInheritance
43     {
44         public void ReuseMethod() 
45         {
46             Console.WriteLine("类适配器模式待复用的方法");
47         }
48     }
49 
50     public class AdapteeCombination
51     {
52         public void ReuseMethod()
53         {
54             Console.WriteLine("对象适配器模式待复用的方法");
55         }
56     } 
57 }

 

以上是关于适配器模式的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从回收器适配器发送到片段 |如何从 recyclerview 适配器调用片段函数

如何从片段适配器启动活动

片段中ListView的android自定义适配器

设计模式之单例模式

片段内带有基本适配器的列表视图

片段中gridview的Android文本和图像适配器