design mode (c): Adapter mode

Source: Internet
Author: User

Introduced

Intent : Convert the interface of the class to another interface that the customer expects. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. is a bridge between two incompatible interfaces.
Main Solution : The main solution in the software system, often to put some "existing objects" into the new environment, and the new environment requirements of the interface is not satisfied with the present object.
when to use : 1, the system needs to use the existing classes, and this class of interfaces do not meet the needs of the system. 2. You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future, that do not necessarily have a consistent interface. 3, through the interface transformation, one class is inserted into another class system.
How to resolve : inheritance or dependency (recommended).
Key Code : The adapter inherits or relies on an existing object to implement the desired target interface.

Real-world examples
Please note that there are some photos on your memory card that need to be transferred to your computer. In order to transfer them, you need some kind of adapter that is compatible with the computer port to connect the memory card to the computer. In this case, the card reader is the adapter. Another example is the famous power adapter; The three-pin plug cannot be connected to a dual socket and requires a power adapter to make it compatible with the double fork socket. Another example is a translator who translates a person's words to another person.

Simply put,
The adapter mode allows you to wrap other incompatible objects in the adapter so that they are compatible with another class.

Wikipedia says

In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is commonly used to make existing classes work with other classes without modifying their source code.

Instance

Imagine a captain who would not be the only one who would use a rowboat.
First, we have interfaces rowingboat and fishingboat.
Kayak

public interface RowingBoat {  void row();}

Fishing vessel interface and implementation

public interface FishingBoat { void sail();}
public class FishingBoatOperator implements FishingBoat {  @Override  public void sail() {    System.out.printf("The fishing boat is sailing");  }}

The captain realizes the rowing interface so that it can move.

public class Captain implements RowingBoat {  private RowingBoat rowingBoat;  public Captain(RowingBoat rowingBoat) {    this.rowingBoat = rowingBoat;  }  @Override  public void row() {    rowingBoat.row();  }}

Now let's say the Pirates are coming, our captain needs to flee, but only fishing boats are available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing skills.

public class FishingBoatAdapter implements RowingBoat {  private FishingBoat boat;  public FishingBoatAdapter() {    boat = new FishingBoatOperator();  }  @Override  public void row() {    boat.sail();  }}

Now, captain can manipulate fishing boats to escape pirates.

Captain captain = new Captain(new FishingBoatAdapter());captain.row();
Applicable scenarios

When using adapter mode

    • You want to use an existing class whose interface does not match the interface you need.
    • You want to create a reusable class that works with unrelated or unforeseen classes, that is, classes that do not necessarily have compatible interfaces.
    • You need to use several existing subclasses, but it is impractical to adjust their interfaces by subclasses of each subclass. The object adapter can adjust the interface of its parent class.
    • Most applications that use third-party libraries use adapters as the middle tier between applications and third-party libraries to separate applications from libraries. If you must use another library, you only need the adapter for the new library without changing the application code.
Results

Classes and object adapters have different tradeoffs:
Class Adapter

    • Make Adaptee adapt to target by committing to specific adaptee classes. Therefore, the class adapter will not work when we want to adjust the class and all its subclasses.
    • Let the adapter overwrite some behavior of adaptee, because adapter is a subclass of Adaptee.
    • Only one object is introduced, and no additional pointers are required to come to the adapter indirectly.

Object Adapter

    • Have an adapter work with many adaptee-the adaptee itself and all its subclasses, if any. The adapter can also add functionality to all adaptee immediately.
    • Make it more difficult to cover adaptee behavior. It will need to subclass Adaptee and make adapter reference subclasses instead of Adaptee itself.
Summarize

Pros : 1. You can let any two classes that are not associated run together. 2, improve the reuse of the class. 3, increase the transparency of the class. 4, good flexibility.
disadvantage : 1, excessive use of the adapter, will make the system very messy, not easy to grasp the whole. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. So if it's not necessary, you can refactor the system without using the adapter. 2. Since JAVA inherits at most one class, only one adapter class can be fitted at most, and the target class must be an abstract class.
Note : The adapter is not added at the detailed design time, but instead solves the problem of the project in service.

    • Translation: Iluwatar-java-design-patterns
    • Reference: runoob.com

design mode (c): Adapter mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.