zoukankan      html  css  js  c++  java
  • 设计模式之适配器模式(七)

    设计模式之适配器模式

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

    这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。

    介绍

    意图:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

    主要解决:主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。

    何时使用: 1、系统需要使用现有的类,而此类的接口不符合系统的需要。 2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。 3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)

    如何解决:继承或依赖(推荐)。

    关键代码:适配器继承或依赖已有的对象,实现想要的目标接口。

    应用实例: 1、美国电器 110V,中国 220V,就要有一个适配器将 110V 转化为 220V。 

    优点: 1、可以让任何两个没有关联的类一起运行。 2、提高了类的复用。 3、增加了类的透明度。 4、灵活性好。

    缺点: 1、过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。

    使用场景:有动机地修改一个正常运行的系统的接口,这时应该考虑使用适配器模式。

    注意事项:适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。

    类的适配器模式

    下面代码中我们使用了 插座类 这是我们需要实现接口,数据线类 这是我们现有的类 ,电源适配器类 将现有的类进行适配成我们想要的接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        class Program
        {
            static void Main(string[] args)
            {
                //类的适配器模式
                ChaZuo chaZuo = new PowerAdapter();
                chaZuo.GongDian();
                Console.ReadKey();
    
                //对象的适配器模式
                //NChaZuo chaZuo = new NPowerAdapter();
                //chaZuo.GongDian();
                //Console.ReadKey();
            }
        }
    }
    主程序
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        /// <summary>
        /// 类适配器   插座接口
        /// </summary>
        public interface ChaZuo
        {
            void GongDian();
        }
    }
    插座接口
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        /// <summary>
        /// 数据线类
        /// </summary>
        public abstract class DataLine
        {
            public void ChongDian() {
                Console.WriteLine("用数据线来给手机充电");
            }
        }
    }
     
    数据线类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        /// <summary>
        /// 类适配器 电源适配器
        /// </summary>
        public class PowerAdapter : DataLine, ChaZuo
        {
            public void GongDian()
            {
                this.ChongDian();
            }
        }
    }
    电源适配器

    对象的适配器模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        class Program
        {
            static void Main(string[] args)
            {
                //类的适配器模式
                //ChaZuo chaZuo = new PowerAdapter();
                //chaZuo.GongDian();
                //Console.ReadKey();
    
                //对象的适配器模式
                NChaZuo chaZuo = new NPowerAdapter();
                chaZuo.GongDian();
                Console.ReadKey();
            }
        }
    }
    主程序
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        /// <summary>
        ///  对象适配器的数据线类
        /// </summary>
        public class NDataLine
        {
            public void ChongDian() {
                Console.WriteLine("我可以给手机充电");
            }
        }
    }
    数据线类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        /// <summary>
        /// 对象的适配器模式
        /// </summary>
        public class NChaZuo
        {
            public virtual void GongDian() {
                
            }
        }
    }
    插座类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Adapter
    {
        /// <summary>
        /// 对象适配器的   电源适配器类
        /// </summary>
        public class NPowerAdapter:NChaZuo
        {
            public NDataLine dataLine = new NDataLine();
            public override void GongDian()
            {
                // base.GongDian();
                dataLine.ChongDian();
            }
        }
    }
    电源适配器

    运行结果:我可以给手机充电

    注:类的适配器  就是对原有类进行继承, 对象适配器 就是对原有对象添加引用 new 一个对象出来

  • 相关阅读:
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    How to build and run ARM Linux on QEMU from scratch
    How to debug Android Native Application with eclipse
  • 原文地址:https://www.cnblogs.com/zkhbalance/p/9393567.html
Copyright © 2011-2022 走看看