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

推荐订阅源

T
Tor Project blog
月光博客
月光博客
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
The Cloudflare Blog
博客园_首页
NISL@THU
NISL@THU
量子位
A
Arctic Wolf
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Full Disclosure
C
Cisco Blogs
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
PCI Perspectives
PCI Perspectives
S
Security Affairs
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
H
Hacker News: Front Page
S
Securelist
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
罗磊的独立博客
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Security @ Cisco Blogs
The Last Watchdog
The Last Watchdog
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
Last Week in AI
Last Week in AI
爱范儿
爱范儿
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | 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);
               }  
     }

阅读全文