Can someone explain the concept of a plug adapter for me with a good example?

Can someone explain the concept of a plug adapter for me with a good example?

+6
language-agnostic design-patterns adapter
source share
3 answers

From what I understood from a quick read of the results of Google, a plug-in adapter is an adapter that is not hardcoded with respect to a particular adapter. On the surface (the adapterโ€™s own interface), itโ€™s all the same, but it can adapt to various adapters with different interfaces. I found this thread quite explanatory:

Basically, this allows the adapter when the adaptable (receiver) protocol is not known at compile time using reflection. When you create an adapter instance, you pass its name to the adapttee method to invoke, as well as any necessary metadata for translating input types. When the adapter receives a method call to the target interface, it uses reflection to invoke the corresponding method specified on the adapter.

And this :

The primary responsibility of the Viewer is to populate the widget from the domain of the model without any assumptions about the domain itself. The JFace viewer uses the mechanism of delegating objects in an adapter template with a plug-in adapter, the above requirement.

Facehugger in action

Think of it as faces from Alien ; when he hugs his face, all you see is the mucous back of the face. You can stick it with a stick and try to wring your hands (adapter interface). But basically, he can hug the face of any person (adaptable), regardless of the features of the face. Maybe I pushed him a little, but, hey, I love the Alien.

+13
source share

You can read this article about the adapter / plugin template:

The content of 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 โ€œplug-in adapterโ€ to describe classes with a built-in fixture interface. This interesting concept allows classes to be introduced into existing systems that might expect different interfaces to the class. This method can facilitate the reuse of the class modulo and even projects.

Here is a small example:

We have two classes - Foo and Boo, which output some string to the console. The adapter class can adapt the methods of both classes to provide the interface (SaySomething) required by the client. Please note that there is no dependence on the interface name - we can easily adapt the 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(); } } 
+3
source share

A distinctive feature of the plug-in 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 usually be achieved using a delegate, Func or Action in C #

0
source share

All Articles