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

推荐订阅源

S
Schneier on Security
A
Arctic Wolf
S
Security Affairs
O
OpenAI News
SecWiki News
SecWiki News
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
N
News | PayPal Newsroom
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
The Hacker News
The Hacker News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
人人都是产品经理
人人都是产品经理
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
The Cloudflare Blog
N
News and Events Feed by Topic
量子位
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Webroot Blog
Webroot Blog

博客园 - 青玄鸟

.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实现类就是一种适配器。