Home > Java > javaTutorial > body text

Design patterns in Java--Adapter pattern

巴扎黑
Release: 2017-07-22 14:07:39
Original
1042 people have browsed it

Definition (Baidu Encyclopedia):
In computer programming, the adapter pattern (sometimes also called packaging style or packaging) adapts the interface of a class to what the user expects.
An adaptation allows classes that normally would not work together because of incompatible interfaces to work together by wrapping the class's own interface in an existing class.

UML class diagram:

##Specific code:

public class Client {public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Adapter adapter = new Adapter(adaptee);
        adapter.request();
    }
}public interface Target {void request();
}public class Adaptee {void adapteeRequest() {
        System.out.println("本类");
    }
}public class Adapter implements Target {

    Adapter(Adaptee adaptee) {this.adaptee = adaptee;
    }private Adaptee adaptee;
    @Overridepublic void request() {
        System.out.println("适配类");
        adaptee.adapteeRequest();
    }
}
Copy after login

Example: Daily examples of this mode, the most common ones are various transfer interfaces When connecting a Mac to a projector, it usually requires an adapter. This is the adapter mode.
In actual development examples, the current system is developed iteratively, step by step. What happens is that the same function may have two or more different interfaces,
Some need to call the previous one , some calls later are messed up, so you need to add an adapter to make the two new and old compatible.

Applicable scenarios: You want to use an existing class, but its interface does not meet your needs.
You want to create a reusable class that can work with other unrelated or unforeseen classes (that is, classes whose interfaces may not necessarily be compatible).
You want to use some existing subclasses, but it's not possible to subclass each one to match their interface. The object adapter can adapt to its parent class interface

Advantages and disadvantages:

Advantages:

Better reusability and scalability

Disadvantages:

The combination of many adapters will make the system too chaotic and difficult to grasp.

Summary: The adapter mode is relatively simple. It can be summed up in two words "compatible". This is the essence of this mode. Of course, the system does not need to be compatible to be better.

The above is the detailed content of Design patterns in Java--Adapter pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template