zoukankan      html  css  js  c++  java
  • 适配器模式(Adapter)

    适配器模式比较简单常用,他封装不匹配的接口,提供合适的接口。

    重要概念

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

    2. 概念:当需要的东西就在面前,却不能使用,而短时间又无法改造它,于是我们就想办法适配它。适配器使一个方法适合另一个方法。

    3. 系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用与希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。

    4. 类适配器模式和对象适配器模式,由于C#不支持多继承,所以这里说明的是对象适配器

    image

    5.使用一个已经存在的类,但如果他的接口,也就是他的方法和你的要求不相同时,就应该考虑用适配器模式对其进行封装。

    6.两个类所作的事情相同或相似,但是具有不同的接口时要使用它。

    7.在软件维护的时候才使用适配器模式,双方都不太容易修改的时候再使用适配器模式。

    8.如果能实现预防接口不同的问题,不匹配就不会发生,在有小的接口不统一问题发生时,及时重构,问题不至于扩大,只有碰到无法改变原有设计和代码的情况才考虑适配器。

    9.事后控制不如事中控制,事中控制不如事前控制。

    示例代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 适配器模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Target target = new Adapter();
                target.Request();
    
                Console.Read();
    
            }
        }
    
        class Target
        {
            public virtual void Request()
            {
                Console.WriteLine("普通请求");
            }
        }
    
        class Adaptee
        {
            public void SpecificRequest()
            {
                Console.WriteLine("特殊请求");
            }
        }
    
        class Adapter : Target
        {
            private Adaptee adaptee = new Adaptee();
    
            public override void Request()
            {
                adaptee.SpecificRequest();
            }
        }
    }

    NBA翻译

    image

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 适配器模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Player b = new Forwards("巴蒂尔");
                b.Attack();
    
                Player m = new Guards("麦克格雷迪");
                m.Attack();
    
                //Player ym = new Center("姚明");
                Player ym = new Translator("姚明");
                ym.Attack();
                ym.Defense();
    
                Console.Read();
            }
        }
    
        //篮球运动员
        abstract class Player
        {
            protected string name;
            public Player(string name)
            {
                this.name = name;
            }
    
            public abstract void Attack();
            public abstract void Defense();
        }
    
        //前锋
        class Forwards : Player
        {
            public Forwards(string name)
                : base(name)
            {
            }
    
            public override void Attack()
            {
                Console.WriteLine("前锋 {0} 进攻", name);
            }
    
            public override void Defense()
            {
                Console.WriteLine("前锋 {0} 防守", name);
            }
        }
    
        //中锋
        class Center : Player
        {
            public Center(string name)
                : base(name)
            {
            }
    
            public override void Attack()
            {
                Console.WriteLine("中锋 {0} 进攻", name);
            }
    
            public override void Defense()
            {
                Console.WriteLine("中锋 {0} 防守", name);
            }
        }
    
        //后卫
        class Guards : Player
        {
            public Guards(string name)
                : base(name)
            {
            }
    
            public override void Attack()
            {
                Console.WriteLine("后卫 {0} 进攻", name);
            }
    
            public override void Defense()
            {
                Console.WriteLine("后卫 {0} 防守", name);
            }
        }
    
        //外籍中锋
        class ForeignCenter
        {
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public void 进攻()
            {
                Console.WriteLine("外籍中锋 {0} 进攻", name);
            }
    
            public void 防守()
            {
                Console.WriteLine("外籍中锋 {0} 防守", name);
            }
        }
    
        //翻译者
        class Translator : Player
        {
            private ForeignCenter wjzf = new ForeignCenter();
    
            public Translator(string name)
                : base(name)
            {
                wjzf.Name = name;
            }
    
            public override void Attack()
            {
                wjzf.进攻();
            }
    
            public override void Defense()
            {
                wjzf.防守();
            }
        }
    }
    冯瑞涛
  • 相关阅读:
    Git修改注释
    数组和切片的区别
    SpringBoot启动报:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    eclipse中web.xml报:The content of element type "web-app" must match "(icon?,display- name?,
    eclipse错误: 在类xxx中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
    idea Cannot resolve method ‘setAttribute(java.lang.String, java.lang.String)/不能使用pageContext.setAttribute()
    解决Nginx启动报nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    数据仓库开发规范
    详解会话技术cookie、session和token
    Requests爬虫包及解析工具 xpath、正则、Beautiful Soup
  • 原文地址:https://www.cnblogs.com/finehappy/p/1616610.html
Copyright © 2011-2022 走看看