• 适配器模式


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

    在系统的数据和行为都正确,但是接口不符合时,我们应该考虑使用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。它主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。通常在双方都不太容易修改的时候再使用适配器模式适配。

    下面举例假设有中国球员和美国球员两个类,都有进攻和防守两个方法,但方法名不一样

         /// <summary>
        /// 抽象基类用于规定客户端想要使用的接口
        /// </summary>
        abstract class Player
        {
            protected string Name { get; set; }
    
            public abstract void Attack();
            public abstract void Defense();
        }
    
        /// <summary>
        /// 符合客户端接口的类
        /// </summary>
        class USAPlayer:Player
        {
            public USAPlayer(string name)
            {
                Name = name;
            }
    
            public override void Attack()
            {
                Console.WriteLine("USAPlayer {0} Attack", Name);
            }
    
            public override void Defense()
            {
                Console.WriteLine("USAPlayer {0} Defense", Name);
            }
        }
    
        /// <summary>
        /// 不符合客户端接口的类,需要使用适配器进行适配
        /// </summary>
        public class ChinaPlayer 
        {
            public string Name { get; set; }
    
            public ChinaPlayer()
            {
            }
    
            public ChinaPlayer(string name)
            {
                Name = name;
            }
    
            public void JinGong()
            {
                Console.WriteLine("ChinaPlayer {0} Attack", Name);
            }
    
            public void FangShou()
            {
                Console.WriteLine("ChinaPlayer {0} Defense", Name);
            }
        }
    

      客户端想要按照美国球员的方法进行调用,定义一个适配器类,使用美国球员的接口调用中国球员的方法:

        /// <summary>
        /// 适配器类,符合客户端统一的接口,将不符合接口的类封装起来
        /// </summary>
        class Translator:Player
        {
            private ChinaPlayer chinaPlayer = new ChinaPlayer();
    
            public Translator(string playerName)
            {
                chinaPlayer.Name = playerName;
            }
    
            public override void Attack()
            {
                chinaPlayer.JinGong();
            }
    
            public override void Defense()
            {
                chinaPlayer.FangShou();
            }
        }
    

      客户端使用适配器调用中国球员的方法:

                //符合接口的类
                USAPlayer usaPlayer = new USAPlayer("科比");
                //符合接口的适配器
                Translator translator = new Translator("姚明");
    
                usaPlayer.Attack();
                translator.Attack();
                usaPlayer.Defense();
                translator.Defense();
    

      执行结果如下:

    USAPlayer 科比 Attack
    ChinaPlayer 姚明 Attack
    USAPlayer 科比 Defense
    ChinaPlayer 姚明 Defense

  • 相关阅读:
    How to convert VirtualBox vdi to KVM qcow2
    (OK)(OK) adb -s emulator-5554 shell
    (OK)(OK) using adb with a NAT'ed VM
    (OK) How to access a NAT guest from host with VirtualBox
    (OK) Creating manually one VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK)(OK) Creating VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK) Creating_VMs_from_an_existing_VDI_file.txt
    (OK) Creating VMs from an existing VDI file —— in OS X
    (OK) install_IBM_SERVER.txt
    (OK) install chrome & busybox in android-x86_64 —— uninstall chrome
  • 原文地址:https://www.cnblogs.com/angela217/p/5439476.html
Copyright © 2020-2023  润新知