Pattern Adapter: description, functions and features, tips for working

Table of contents:

Pattern Adapter: description, functions and features, tips for working
Pattern Adapter: description, functions and features, tips for working
Anonim

Adapter is a structural design pattern used to organize and implement the methods of an object that cannot be modified by means of a specially designed interface. Otherwise, we can say that this is a structural template that allows objects with incompatible interfaces to interact with each other.

Description

Pattern Adapter performs adaptation between classes and objects. Like any adapter in the world around us, a template is an interface or bridge between two objects. In the real world, we have adapters for power supplies, for hard drives, for headphones, for camera memory cards, and so on. For example, consider several adapters for memory cards. If you cannot directly connect the camera memory card to a laptop, you can use an adapter: the camera memory card is connected to the adapter, and the adapter is connected to the laptop connector. So the problem of incompatibilityinterfaces will be allowed.

Adapter: design pattern and interface
Adapter: design pattern and interface

In the case of software development, things are much the same. You can imagine a situation where there is some class that expects some type of object, and there is an object that offers the same functionality, but with a different interface. Of course, it's beneficial to use both of them so you don't have to re-implement one of the interfaces and change existing classes. It is in this situation that it would be wise to use an adapter for software design.

Implementation

The figure below shows the UML class diagram of the Adapter pattern.

Adapter: UML diagram
Adapter: UML diagram

Classes and objects involved in the design pattern:

  1. (Target) - defines a domain-specific interface that Client uses.
  2. (Adapter) - adapts the interface (Adaptee) to the target interface.
  3. (Adaptee) - Specifies an existing interface to be adapted.
  4. (Client) - interacts with objects corresponding to the interface (Target).

Application

Pattern Adapter is used in the following cases:

When there is a class (Target) that calls the methods defined in the interface. In addition, there is another class (Adapter) that does not implement the interface, but implements the operations and methods that must be called from the first class through the interface. The programmer does not have the ability to change any of the existing codes. The adapter implements itsinterface and will become a bridge between the two classes

Pattern Adapter: building a project
Pattern Adapter: building a project

When writing a class (Target) for general use, it is important to rely on some common interfaces, and the developer has some implemented classes that do not implement the interface. Also this class (Target) must be callable

A good example of using an adapter would be wrappers used to accept third party libraries and frameworks: most applications that use third party libraries use an adapter as an intermediate layer between the application and the third party library to separate the application from the library. If a different library needs to be used, the new library only needs an adapter without having to change the application code.

Delegation based object adapters

Object (Adapter) is a classic example of an adapter pattern. It uses composition, and (Adaptee) delegates calls to itself, which is not available to class adapters that extend (Adaptee). This behavior gives us several advantages over class adapters, but class adapters can be implemented in languages that allow multiple inheritance. The main advantage is that (Adapter) adapts not only (Adaptee), but all of its subclasses. All of these subclasses exist with one "small" limitation: they can't all add new methods because the mechanism used is delegation. So for any new method, the adapter must be changed orexpanded to provide new methods. The main disadvantage is that it requires new code to be written to delegate all necessary requests to the adapter.

Class adapters based on (multiple) inheritance

Class adapters can be implemented in languages that support multiple inheritance. The Java, C, or PHP programming languages do not support multiple inheritance, but they do have interfaces. Thus, such patterns cannot be easily implemented in these languages. A good example of a programming language where design can be easily implemented is C.

Pattern Adapter uses inheritance instead of composition. This means that instead of delegating calls to (Adaptee), it inherits it. Finally, the class adapter must subclass both (Target) and itself (Adapter).

Multiple Inheritance Adapter
Multiple Inheritance Adapter

This approach has its advantages and disadvantages:

  • Pattern adapts a certain class (Adaptee). The class extends this adaptation. If that subclass, it cannot be adapted by an existing adapter.
  • The template does not require all the code needed for delegation to be written for the (Adapter) class.
  • If the object (Target) is represented by an interface rather than a class, we can talk about "class" adapters because we can implement as many interfaces as we want.

Bilateral adapters

Bilateral adapters are adapters that implementboth interfaces: both (Target) and (Adaptee). An adapted object can be used as (Target) in new class-based systems (Target), or as (Adaptee) in other class-based systems (Adaptee). Going further in this direction, we can have adapters implementing n interfaces adapting to n systems. Bilateral adapters and n-lane adapters are difficult to implement on systems that do not support multiple inheritance. If an adapter must extend a class (Target), it cannot extend another class such as (Adaptee), so (Adaptee) must be an interface and all calls can be delegated from the adapter to an object (Adaptee).

Using the Adapter Template in VR Development
Using the Adapter Template in VR Development

In addition, if (Target) and (Adapter) are similar, then the adapter should simply delegate requests from the (Target) class to the (Adapter) class, and if (Target) and (Adaptee) are not similar, then the adapter may need to convert the data structures between them and implement operations required by (Target) but not implemented in the (Adaptee) class.

Implementation example

Suppose we have a class (Bird) with fly() and makeSound() methods. And also a class (ToyDuck) with a Squeak() method. Let's say we don't have many objects (ToyDuck) and want to use objects (Bird) instead. Birds have similar functionality but implement a different interface so we can't use them directly. Therefore, we will use the adapter template. Here our (Client) will be (ToyDuck) and (Adaptee) will be (Bird). Belowan example of the implementation of the design of the Adapter pattern in Java, one of the most common programming languages, is given.


interface Bird { public void fly(); public void makeSound(); } class Sparrow implements Bird { public void fly() { System.out.println("Flying"); } public void makeSound() { System.out.println("Chirp Chirp"); } } interface ToyDuck { public void squeak(); } class PlasticToyDuck implements ToyDuck { public void squeak() { System.out.println("Squeak"); } } class BirdAdapter implements ToyDuck { Bird bird; public BirdAdapter(Bird bird) { this. bird=bird; } public void squeak() { bird.makeSound(); } } class Main { public static void main(String args[]) { Sparrow sparrow=new Sparrow(); ToyDuck toyDuck=new PlasticToyDuck(); ToyDuck birdAdapter=new BirdAdapter(sparrow); System.out.println("Sparrow…"); sparrow.fly(); sparrow.makeSound(); System.out.println("ToyDuck…"); toyDuck.squeak(); System.out.println("BirdAdapter…"); birdAdapter.squeak(); } }

Suppose we have a bird that can do Sound() and a plastic toy duck that can squeak - Squeak(). Now suppose our (Client) changes the requirement and wants (ToyDuck) to execute Sound(), but how?

Design example based on the Adapter pattern
Design example based on the Adapter pattern

The solution is that we simply change the implementation class to a new adapter class and tell the client to pass the bird instance to this class. That's all. Now changing only one line, we will teach (ToyDuck)chirp like a sparrow.

Popular topic

Editor's choice

  • IPv6 protocol: setup on Windows systems
    IPv6 protocol: setup on Windows systems

    Probably, many users of computer systems, delving into the network settings, noticed that in the list of protocols, in addition to the well-known IPv4, there is also the sixth version (IPv6)

  • View history on computer
    View history on computer

    Today you can learn about almost all the actions taken on the computer. It's about browsing history

  • Computer technology hardware: definition, description and types
    Computer technology hardware: definition, description and types

    Modern computers to ensure maximum performance and correct operation use hardware and software that are very interconnected and clearly interact in different directions. Now let's touch on the consideration of hardware, since initially it is they who occupy a dominant position in ensuring the operability of any computer or even mobile system

  • Keyboard shortcuts and the evolution of operating systems
    Keyboard shortcuts and the evolution of operating systems

    It takes about a minute to copy or move a file from one location to another using the menu system. The developers introduced a keyboard shortcut that replaced all these actions and made it possible to reduce the time of this common operation by 3 or more times. True, for this it was necessary to slightly modernize the keyboard: new keys were added - "Ctrl", and later - "Windows"

  • If the torrent does not download
    If the torrent does not download

    An article for those who consider the ability to download files from torrent trackers a convenience, not an echo of the past. Not only the unavailability of "high-speed" tariff plans is the reason for the choice, but also personal preferences