首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Adapter -Adapter模式的任何真实示例

Adapter -Adapter模式的任何真实示例
EN

Stack Overflow用户
提问于 2012-06-18 16:55:29
回答 8查看 62.9K关注 0票数 94

我想向我的团队演示Adapter Pattern的用法。我在网上读了很多书和文章。每个人都在举一个例子,这些例子有助于理解概念(形状、存储卡、电子适配器等),但没有真正的案例研究。

您能分享一些Adapter模式的案例研究吗?

附注:我尝试在stackoverflow上搜索现有问题,但没有找到答案,因此将其作为新问题发布。如果你知道这个问题已经有答案了,请重定向。

EN

回答 8

Stack Overflow用户

发布于 2015-08-27 22:15:10

如何把一个法国人变成一个普通人。

代码语言:javascript
复制
 public interface IPerson
    {
        string Name { get; set; }
    }

    public interface IFrenchPerson
    {
        string Nom { get; set; }
    }

    public class Person : IPerson
    {
        public string Name { get; set; }
    }

    public class FrenchPerson : IFrenchPerson
    {
        public string Nom { get; set; }
    }

    // that is a service that we want to use with our French person
    // we cannot or don't want to change the service contract
    // therefore we need 'l'Adaptateur'
    public class PersonService
    {
        public void PrintName(IPerson person)
        {
            Debug.Write(person.Name);
        }
    }

    public class FrenchPersonAdapter : IPerson
    {
        private readonly IFrenchPerson frenchPerson;

        public FrenchPersonAdapter(IFrenchPerson frenchPerson)
        {
            this.frenchPerson = frenchPerson;
        }

        public string Name 
        {
            get { return frenchPerson.Nom; }
            set { frenchPerson.Nom = value; }
        }
    } 

示例

代码语言:javascript
复制
    var service = new PersonService();
    var person = new Person();
    var frenchPerson = new FrenchPerson();

    service.PrintName(person);
    service.PrintName(new FrenchPersonAdapter(frenchPerson));
票数 53
EN

Stack Overflow用户

发布于 2014-07-30 17:41:57

您可以在此处找到用于防御注入攻击的Adapter模式的PHP实现:

http://www.php5dp.com/category/design-patterns/adapter-composition/

Adapter模式的一个有趣方面是,它有两种风格:依赖于多重继承的类适配器和依赖于组合的对象适配器。上面的例子依赖于组合。

票数 2
EN

Stack Overflow用户

发布于 2014-12-09 17:38:21

一个真实的例子是Qt-Dbus。

qt-dbus具有从所提供的xml文件生成适配器和接口代码的实用程序。以下是执行此操作的步骤。

代码语言:javascript
复制
 1. Create the xml file - this xml file should have the interfaces 
that can be viewed by the qdbus-view in the system either on 
the system or session bus.

    2.With the utility - qdbusxml2cpp , you generate the interface adaptor code. 
This interface adaptor does the demarshalling of the data that is 
received from the client. After demarshalling, it invokes the 
user defined - custom methods ( we can say as adaptee).

    3. At the client side, we generate the interface from the xml file. 
This interface is invoked by the client. The interface does the 
marshalling of the data and invokes the adaptor interface. As told 
in the point number 2, the adaptor interface does the demarshalling 
and calls the adaptee - user defined methods.

你可以在这里看到Qt-Dbus的完整示例-

http://www.tune2wizard.com/linux-qt-signals-and-slots-qt-d-bus/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11079605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档