Design Patterns: Adapter. Structural templates: Adapter Ready code for adapters in java

    Adapter (Design Pattern)/Code Examples- Main article: Adapter (design pattern) An example of a pattern implementation in C# using System; namespace Adapter ( class MainApp ( static void Main() ( ... Wikipedia

    Proxy pattern (design pattern)

    Design Pattern- This term has other meanings, see Pattern. In software development, a design pattern is a repeatable architectural design that represents a solution to a problem... ... Wikipedia

    Interface (design pattern)- Design pattern Interface Described in Design Patterns No In computer science, an interface pattern is not a special pattern among design patterns. It is a general method for structuring computer programs for that... Wikipedia

    Deputy (design pattern)- Proxy Pattern (Deputy) Design pattern. Provides an access-controlling object, intercepting all calls to it. Contents 1 Goal 1.1 Problem 1.2 Solution 2 Pros 3 ... Wikipedia

    Guardian (design pattern)- Design Pattern Guardian Memento Type: Behavioral Described in Design Patterns Yes Guardian (also known as Memento, Token, Token) is a behavioral design pattern. Allows you to fix without breaking encapsulation... Wikipedia

    Iterator (design pattern)- Design Pattern Iterator Iterator Type: behavioral Described in Design Patterns Yes Iterator Pattern (also known as Cursor) A design pattern that refers to behavioral patterns. It is an object that allows you to get ... Wikipedia

    Interpreter (design pattern)- Design pattern Interpreter Type: behavioral Purpose: solves a frequently occurring, change-prone problem Described in Design Patterns Yes Interpreter Pattern (English ... Wikipedia

    Linker (design pattern)- Design pattern Composite Type: structural Described in Design Patterns Yes Composite pattern is a design pattern, refers to structural patterns, combines an object ... Wikipedia

    State (design pattern)- State design pattern Type: behavioral Described in Design Patterns Yes State is a design pattern. It is used in cases where, during program execution, an object ... Wikipedia

    Main article: Adapter (design pattern) Example implementation of the pattern in C# using System; namespace Adapter ( class MainApp ( static void Main() ( ... Wikipedia

    This term has other meanings, see Pattern. In software development, a design pattern is a repeatable architectural design that represents a solution to a problem... ... Wikipedia

    Interface Design Pattern Interface Described in Design Patterns No In computer science, the interface pattern is not a special pattern among design patterns. It is a general method for structuring computer programs in order to... Wikipedia

    Proxy Pattern Design pattern. Provides an access-controlling object, intercepting all calls to it. Contents 1 Goal 1.1 Problem 1.2 Solution 2 Pros 3 ... Wikipedia

    Design Pattern Guardian Memento Type: Behavioral Described in Design Patterns Yes Guardian (also known as Memento, Token, Token) is a behavioral design pattern. Allows you to fix without breaking encapsulation... Wikipedia

    Design Pattern Iterator Iterator Type: Behavioral Described in Design Patterns Yes Iterator Pattern (also known as Cursor) A design pattern that refers to behavioral patterns. It is an object that allows you to get ... Wikipedia

    Design pattern Interpreter Type: behavioral Purpose: solves a frequently occurring, change-prone problem Described in Design Patterns Yes Interpreter pattern (English ... Wikipedia

    Design pattern Composite Type: structural Described in Design Patterns Yes Composite pattern is a design pattern, refers to structural patterns, combines an object ... Wikipedia

    Design pattern State Type: behavioral Described in Design Patterns Yes State is a design pattern. It is used in cases where, during program execution, an object ... Wikipedia

Before reading, please review the following conventions and concepts. This article is updated with some frequency, so if you have read it before, it is not a fact that the data has not changed.

Adapter belong to class structural patterns. It is used to convert one interface to another required by the client. The adapter ensures compatibility of incompatible interfaces by implementing a layer.

Principle of operation

The adapter inherits the target interface in an open manner (let's call it Target), and a closed way adaptable interface ( Adaptee). In the implementation of the target interface methods, requests are redirected (delegated) to a class with an adaptable interface

Example

// Target interface, the client can only work with it interface iTarget ( public function query(); ) // Adaptable interface. The client does not know how to work with it, but really wants interface iAdaptee ( public function request(); ) // A class that implements an adaptable interface class Adaptee implements iAdaptee ( public function request() ( return __CLASS__ . "::" . __METHOD__; ) ) class Adapter implements iTarget ( protected $adaptee = null; public function __construct() ( $this -> adaptee = new Adaptee(); ) public function query() ( return $this -> adaptee -> request(); ) ) $ Target = new Adapter(); print $Target -> query(); // "Adaptee::request"

Conclusion

An adapter can adapt several interfaces into a single one at once; this pattern is called object adapter The use of this pattern is justified in several cases. If you want to use an existing class with a different interface. If you are going to use the adaptable interface in several places, and do not have the opportunity to make it look the same everywhere, then using replaceable adapters may be a good idea.

Purpose of the Adapter pattern

Often in new software project Can't reuse existing code. For example, existing classes may have the desired functionality, but have incompatible interfaces. In such cases, you should use the Adapter pattern.

The Adapter pattern, which is a software wrapper over existing classes, converts their interfaces into a form suitable for subsequent use.

Let's look at a simple example of when the Adapter pattern should be used. Let us develop a climate control system designed to automatically maintain the ambient temperature within specified limits. An important component Such a system is a temperature sensor, with which the ambient temperature is measured for subsequent analysis. For this sensor there is already a ready-made software from third-party developers, which is a class with the appropriate interface. However, you cannot use this class directly, since the sensor readings are taken in degrees Fahrenheit. You need an adapter that converts the temperature to Celsius.

The queue, priority_queue and stack containers of the standard STL template library are implemented on the basis of the sequential list, deque and vector containers, adapting their interfaces to the desired form. That is why these containers are called adapter containers.

Description of the Adapter pattern

Let the class whose interface needs to be adapted to the desired form be named Adaptee. To solve the problem of transforming its interface, the Adapter pattern introduces the following class hierarchy:

  • Target virtual base class. Announced here user interface suitable type. Only this interface is available to the user.
  • A derived class of Adapter that implements the Target interface. This class also contains a pointer or reference to an Adaptee instance. The Adapter pattern uses this pointer to forward client calls to Adaptee. Since the Adaptee and Target interfaces are incompatible with each other, these calls usually require conversion.

Implementation of the Adapter pattern

Classic implementation of the Adapter pattern

Here is an implementation of the Adapter pattern. For the example above, we adapt the readings of the temperature sensor of the climate control system, converting them from degrees Fahrenheit to degrees Celsius (it is assumed that the code for this sensor is not available for modification).

#include // Already existing environmental temperature sensor class class FahrenheitSensor ( public: // Get temperature readings in Fahrenheit float getFahrenheitTemp() ( float t = 32.0; // ... some code return t; ) ); class Sensor ( public: virtual ~Sensor() () virtual float getTemperature() = 0; ); class Adapter: public Sensor ( public: Adapter(FahrenheitSensor* p) : p_fsensor(p) ( ) ~Adapter() ( delete p_fsensor; ) float getTemperature() ( return (p_fsensor->getFahrenheitTemp()-32.0)*5.0/9.0 ; ) private: FahrenheitSensor* p_fsensor; ); int main() ( Sensor* p = new Adapter(new FahrenheitSensor); cout<< "Celsius temperature = " << p->getTemperature()<< endl; delete p; return 0; }

Implementation of the Adapter pattern based on closed inheritance

Let our climate control system temperature sensor support the adjustment function for more accurate readings. This function is not required to be used, which is probably why the corresponding adjust() method is declared protected by the developers in the existing FahrenheitSensor class.

The system we develop must support measurement customization. Since access to a protected method through a pointer or link is prohibited, the classic implementation of the Adapter pattern is no longer suitable here. The only solution is to inherit from the FahrenheitSensor class. The interface of this class must remain inaccessible to the user, so the inheritance must be private.

The goals pursued when using open and closed inheritance are different. While open inheritance is used for interface and implementation inheritance, closed inheritance is used only for implementation inheritance.

#include class FahrenheitSensor ( public: float getFahrenheitTemp() ( float t = 32.0; // ... return t; ) protected: void adjust() () // Adjust the sensor (protected method) ); class Sensor ( public: virtual ~Sensor() () virtual float getTemperature() = 0; virtual void adjust() = 0; ); class Adapter: public Sensor, private FahrenheitSensor ( public: Adapter() ( ) float getTemperature() ( return (getFahrenheitTemp()-32.0)*5.0/9.0; ) void adjust() ( FahrenheitSensor::adjust(); ) ); int main() ( Sensor * p = new Adapter(); p->adjust(); cout<< "Celsius temperature = " << p->getTemperature()<< endl; delete p; return 0; }

Results of applying the Adapter pattern

Advantages of the Adapter pattern

  • The Adapter pattern allows you to reuse existing code by adapting its incompatible interface into a usable form.

Disadvantages of the Adapter Pattern

  • The task of converting interfaces can be difficult if the client calls and/or passed parameters do not have a functional correspondence in the object being adapted.

Hi all! Today we will talk about Pattern Adapter design pattern. As the name implies, it serves to ensure that something adapt, but what? And this article will answer this question for you.

Description of the Adapter design pattern

Let's move a little away from programming and look at adapters in real life. For example, you bought some equipment (for example, a computer) abroad. Having arrived home with it, you discovered that the plug of a different standard does not fit into our Russian outlet. What to do? Right! You need to go to the store and buy adapter, using which you can connect your computer to the network. So this one adapter and there is adapter. We insert a foreign plug into it, and plug the adapter itself into the network and everything works great. Those. it serves simply layer between our socket and foreign plug.

So I think you got it figured out what is an adapter in life. In programming it's the same.

Example of adapter implementation in PHP

interface iMain (
public function send();
}

Interface iAdaptee (
public function inquiry();
}

Class Adaptee implements iAdaptee (
public function inquiry() (
return __CLASS__."::".__METHOD__;
}
}

Class Adapter implements iMain (
protected $adaptee = null;

Public function __construct() (
$this->adaptee = new Adaptee();
}

Public function send() (
return $this->adaptee->inquiry();
}
}

$goal = new Adapter();
echo $goal->send(); // "Adaptee::inquiry"
?>

So here's our code. Let's figure it out. We have an interface iMain, the client code can work with it. Next we have the interface iAdaptee, which the client code cannot work with, but we need to somehow interact with it. Then we have class Adaptee iAdaptee and inside it has a method called inquiry, which simply returns a string like CLASS::METHOD. Here we come to the class Adapter, which inherits the interface iMain. Inside it we create a protected property adaptee, equal null. Next, in the constructor we create a class object Adaptee and write it to our protected property. In method send we return the method call inquiry.

That's all. Now we create our adapter object and call the method send.

Those. the whole point is that we can work with one interface, but cannot with another, and in order to somehow interact with this interface inaccessible to us, we create adapter, a layer through which we implement our task.

Conclusion

Now it may seem to you that everything is very complicated and difficult, but it is not so. I think you understand the meaning of the adapter, but now you need more practice. Take a look at the code given in this article again and try to make sense of it. Go through it line by line and recite it as if you were an interpreter php. Also, I advise you to look at the code of some framework, because this template is used there quite often.

This concludes this rather long article, thank you for your attention!