惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
V2EX
小众软件
小众软件
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
T
Threatpost
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
U
Unit 42
Spread Privacy
Spread Privacy
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
云风的 BLOG
云风的 BLOG
The Register - Security
The Register - Security
AWS News Blog
AWS News Blog
月光博客
月光博客
Security Latest
Security Latest
H
Heimdal Security Blog
S
Secure Thoughts
博客园 - 聂微东
PCI Perspectives
PCI Perspectives
博客园 - 叶小钗
Scott Helme
Scott Helme
O
OpenAI News
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
NISL@THU
NISL@THU
S
Securelist
Latest news
Latest news
P
Proofpoint News Feed
博客园 - 【当耐特】

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient partial update HttpClient 基本使用 值对象的封装 只读集合类型属性实现 模板模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
适配器模式
青玄鸟 · 2020-04-08 · via 博客园 - 青玄鸟

定义

将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。

UML 类图

实现

场景: 你有一个绘制柱状图组件,其他组件(客户)调用该组件完成柱状图的显示,有一天你希望使用功能更加丰富的的第三方图表组件,而第三方的图表组件API与你自己的柱状图组件不太相同,这时候可以使用适配器模式,将第三方绘图组件封装成其他组件(客户)需要的API类型。

目标接口

    public interface IBarChart
    {
        string Title { get; set; }
        List<string> XData { get; set; }
        List<int> YData { get; set; }
        void GenerateChart();
    }

目标接口实现

    public interface IBarChart
    {
        string Title { get; set; }
        List<string> XData { get; set; }
        List<int> YData { get; set; }
        void GenerateChart();
    }

不兼容的第三方图表组件

    public class ThirdPatryBarChart
    {
        public void DrawChart(string title, List<string> xData, List<int> yData)
        {
            Console.WriteLine($"ThirdParty Chart:Title:{title},X:{string.Join(",",xData)},Y:{string.Join(",",yData)}");
        }
    }

适配器

    public class BarChartAdapter : IBarChart
    {
        ThirdPatryBarChart _chart;

        public BarChartAdapter(ThirdPatryBarChart chart)
        {
            _chart = chart;
        }
        public string Title { get; set; }
        public List<string> XData { get; set; }
        public List<int> YData { get; set; }

        public void GenerateChart()
        {
            _chart.DrawChart(Title,XData,YData);
        }
    }

client

    class Program
    {
        static void Main(string[] args)
        {
            IBarChart myBarChart = new MyBar
            {
                Title = "MyChart",
                XData = new List<string> { "Level A", "Level B", "Level C" },
                YData = new List<int> { 100, 80, 65 }
            };

            myBarChart.GenerateChart();

            IBarChart thirdPartyBarChart = new BarChartAdapter(new ThirdPatryBarChart())
            {
                Title = "ThirdPartyChart",
                XData = new List<string> { "Level A", "Level B", "Level C" },
                YData = new List<int> { 90, 75, 60 }
            };

            thirdPartyBarChart.GenerateChart();
        }
    }

运行结果

    My Bar Chart:Title:MyChart,X:3,Y:3
    ThirdParty Chart:Title:ThirdPartyChart,X:Level A,Level B,Level C,Y:90,75,60

其他应用场景

开发中Service层依赖IRepository接口,每种数据对于数据访问的API不同,针对不同数据库IRepository实现类就是一种适配器。