有人可以用很好的例子向我解释可插拔适配器的概念吗?

发布于 2024-07-12 02:50:23 字数 32 浏览 5 评论 0原文

有人可以用很好的例子向我解释可插拔适配器的概念吗?

Can anybody explain the concept of pluggable adapter to me with good example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风柔一江水 2024-07-19 02:50:23

根据我对 Google 结果的快速阅读了解,可插拔适配器是一种不针对特定适配器进行硬编码的适配器。 从表面上看(适配器自己的接口),都是一样的,但是可以适应不同接口的不同适配器。 我发现此线程非常解释:

基本上,它允许您输入
适配器当被适配者(接收者)
编译时协议未知
通过使用反射。 当你创建时
适配器实例,您将其传递给
要调用的适配器方法的名称,
以及任何必要的元数据
翻译输入类型。 当。。。的时候
适配器接收到的方法调用
目标接口,它使用反射
调用对应的方法
在适配器上指定。

还有这个

浏览者的主要责任
是从域填充小部件
不做任何假设的模型
关于域本身。 JFace 查看器使用
中的委托对象机制
要实现的可插拔适配器模式
以上要求。

Facehugger 在action

将其视为来自外星人的抱脸者; 当它拥抱一张脸时,你只能看到拥抱者粘糊糊的背部。 您可以用棍子戳它并尝试撬开它的手臂(适配器接口)。 但它基本上可以拥抱任何人(适应者)的脸,无论面部特征如何。 也许我有点过分了,但是,嘿,我喜欢《外星人》。

From what I understood from a quick reading of Google results, a pluggable adapter is an adapter that isn't hard-coded against a specific adaptee. On the surface (the adapter's own interface), it's all the same but it can adapt to different adaptees with different interfaces. I found this thread pretty explanatory:

Basically, it allows you to put in an
adapter when the adaptee (receiver)
protocol is not known at compile time
by using reflection. When you create
the adapter instance, you pass it the
name of the adaptee's method to call,
and also any metadata that's necessary
to translate input types. When the
adapter receives a method call of the
target interface, it uses reflection
to call the corresponding method
specified on the adaptee.

And this:

The main responsibility of the Viewer
is to populate a widget from a domain
model without making any assumptions
about domain itself. JFace viewer uses
the Delegating Objects mechanism in
Pluggable Adapter Pattern to implement
the above requirement.

Facehugger in action

Think of it as a facehugger from Alien; when it hugs a face, all you see is the slimy back of the facehugger. You can poke it with a stick and try to pry off its arms (the adapter interface). But it basically can hug the face of any human (the adaptee), regardless of the face features. Maybe I'm pushing it a bit, but, hey, I love Alien.

尹雨沫 2024-07-19 02:50:23

您可以阅读这篇关于适配器/可插拔模式的文章

目录在这篇文章中:

* 1 Design Patterns
* 2 Intent of Adapter
* 3 Motivation
* 4 Structure
* 5 Applicability
* 6 Consequences
* 7 Implementation
      o 7.1 Known Uses and Sample Code
      o 7.2 Related Patterns
* 8 Conclusions
* 9 Appendix
      o 9.1 References
      o 9.2 Glossary

引用:

Smalltalk 引入了一个概念
“可插拔适配器”来描述
具有内置接口的类
适应。 这个有趣的概念
允许引入类
到现有的系统中,可能
期望不同的接口
班级。 这项技术可以帮助促进
跨模块甚至类的重用
项目。

这是一个小例子:

我们有两个类 - Foo 和 Foo。 Boo 向控制台输出一些字符串。 适配器类可以调整这两个类的方法来提供客户端所需的接口(SaySomething)。 请注意,不依赖于接口名称 - 我们可以轻松调整 SayHey 和 Bark 方法。

class Foo 
{
    public static void SayHey() { Console.WriteLine("Hey!"); }
}

class Boo 
{
    public static void Bark() { Console.WriteLine("Woof!"); }
}

class Adapter 
{
    public Action SaySomething { get; private set;} // "pluggable" adapter

    public Adapter(Action saySomethingAction) 
    {
        SaySomething = saySomethingAction;
    }
}

class Program
{
    static void Main(string[] args)
    {
        (new Adapter(Foo.SayHey)).SaySomething();
        (new Adapter(Boo.Bark)).SaySomething();
    }
}

You can read this article about adapter/pluggable pattern:

Table of content in this article:

* 1 Design Patterns
* 2 Intent of Adapter
* 3 Motivation
* 4 Structure
* 5 Applicability
* 6 Consequences
* 7 Implementation
      o 7.1 Known Uses and Sample Code
      o 7.2 Related Patterns
* 8 Conclusions
* 9 Appendix
      o 9.1 References
      o 9.2 Glossary

Quote:

Smalltalk introduced the concept of a
"pluggable adapter" to describe
classes with built-in interface
adaptation. This interesting concept
allows for classes to be introduced
into existing systems that might
expect different interfaces to the
class. This technique can help promote
class reuse across modules and even
projects.

Here is a small example:

We have two classes - Foo & Boo that outputs some string to console. Adapter class can adapt methods from both classes to provide interface (SaySomething) required by client. Note that there is no dependency on interface name - we can easily adapt both SayHey and Bark methods.

class Foo 
{
    public static void SayHey() { Console.WriteLine("Hey!"); }
}

class Boo 
{
    public static void Bark() { Console.WriteLine("Woof!"); }
}

class Adapter 
{
    public Action SaySomething { get; private set;} // "pluggable" adapter

    public Adapter(Action saySomethingAction) 
    {
        SaySomething = saySomethingAction;
    }
}

class Program
{
    static void Main(string[] args)
    {
        (new Adapter(Foo.SayHey)).SaySomething();
        (new Adapter(Boo.Bark)).SaySomething();
    }
}
捂风挽笑 2024-07-19 02:50:23

Pluggable Adapter 的一个显着特点是客户端调用的方法和接口中存在的方法可以不同。

 interface Ilegacy
    {
        float calculate(int a, int b);
    }
    class Legacy : Ilegacy
    {
        public float calculate(int a, int b)
        {
            return a * b;
        }
    }
    class Adapter
    {
        public Func<int, int, float> legacyCalculator;
        public Adapter()
        {
            this.legacyCalculator = new Legacy().calculate;
        }
    }
    class Client
    {
        static void Main()
        {
            float result = new Adapter().legacyCalculator(5, 6);
        }
    }

这通常可以通过在 C# 中使用 delegate、Func 或 Action 来实现

A distinguish Feature of the Pluggable Adapter is that the method called by the client and the method existing in the interface can be different.

 interface Ilegacy
    {
        float calculate(int a, int b);
    }
    class Legacy : Ilegacy
    {
        public float calculate(int a, int b)
        {
            return a * b;
        }
    }
    class Adapter
    {
        public Func<int, int, float> legacyCalculator;
        public Adapter()
        {
            this.legacyCalculator = new Legacy().calculate;
        }
    }
    class Client
    {
        static void Main()
        {
            float result = new Adapter().legacyCalculator(5, 6);
        }
    }

This can normally acheived with the use of delegate,Func or Action in C#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文