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

推荐订阅源

C
Check Point Blog
H
Help Net Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
云风的 BLOG
云风的 BLOG
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
Docker
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
G
Google Developers Blog
Jina AI
Jina AI
P
Proofpoint News Feed
J
Java Code Geeks
I
InfoQ
博客园 - 司徒正美
D
DataBreaches.Net
博客园 - 叶小钗
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
L
LangChain Blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
人人都是产品经理
人人都是产品经理
V
V2EX
F
Full Disclosure
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
V
Visual Studio Blog

博客园 - 李明飞

C#设计模式之我见(四)Ⅱ C#设计模式之我见(四)Ⅰ C#设计模式之我见(三)Ⅲ C#设计模式之我见(三)Ⅱ C#设计模式之我见(二)Ⅱ C#设计模式之我见(二)Ⅰ C#设计模式之我见(一) 面向对象编程之重用、继承、多态、抽象 初探SilverLight 2.0的安装和部署 工作小札,生活一杯羹! “管家婆”的好帮手——我的理财小管家 类库DLL属性说明和正确使用App_Code中类的静态成员 冬日糊辣汤 以前的面试题(二) 以前的面试题 SQL Server 2005新特性之感悟 招聘高级网页设计师(北京) 知识点滴(打油篇) Web Services 的设计和模式
C#设计模式之我见(三)Ⅰ
李明飞 · 2011-06-28 · via 博客园 - 李明飞

    下面章节将介绍结构型模式的适配器模式(Adapter Pattern)、桥接模式(Bridge Pattern)、装饰模式(Decorator Pattern)、组合模式(Composite Pattern)、外观模式(Façade Pattern)、享元模式(Flyweight Pattern)、代理模式(Proxy Pattern)。

    下面介绍一下适配器模式(Adapter Pattern),适配器让类与类之间不至于因为接口不兼容而不能协同工作。类适配器可以使用多重继承来适配一个接口到另一个接口。
在以下情形,考虑使用适配器模式:

  • 你要使用一个现成的类,但是它的接口不完全符合你的需求。
  • 你要创建一个可复用的类来和无关的或者不可预见的其它类协同工作,也就是说,这个可复用的类未必一定要有兼容的接口。
    (选择对象适配器的理由) 你需要使用若干现成的子类,但是对每一个子类进行派生来达到适配的目的会显得不太现实,成本太高。这时候选择对象适配器就比较明智,因为对象适配器可以使用父类的接口进行适配。下面是具体代码实例:

namespace Adapter.DesignPattern
{
     using System;
     class FrameworkXTarget 
     {
          virtual public void SomeRequest(int x)
              {

              }
      }

      class FrameworkYAdaptee
      {
           public void QuiteADifferentRequest(string str) 
                {
                     Console.WriteLine("QuiteADifferentRequest = {0}", str);
                }  
      }

     class OurAdapter : FrameworkXTarget
     {
          private FrameworkYAdaptee adaptee = new FrameworkYAdaptee();
          override public void SomeRequest(int a)
               {
                    string b;
                    b = a.ToString();
                    adaptee.QuiteADifferentRequest(b);
               }  
     }

阅读全文