java 23 Design Patterns - 06 - Adapter Patterns

Keywords: Java JDK github

Catalog

Adapter mode

The Adapter Pattern serves as a bridge between two incompatible interfaces. This type of design pattern belongs to structural pattern, which combines the functions of two independent interfaces.

This pattern involves a single class, which is responsible for adding independent or incompatible interface functions. For example, a card reader acts as an adapter between a memory card and a notebook. You insert the memory card into the reader, and then insert the reader into the notebook, so that you can read the memory card through the notebook.

We demonstrate the use of the adapter pattern with the following examples. Among them, the audio player device can only play mp3 files, using a more advanced audio player to play vlc and mp4 files.

Actual case

Overview of Class Information:

Class name Explain
Main.java Total entry of method
AdapterDelegate.java Cohesive approach
Adaptee.java Cohesive class
Adapter.java Adapter
Target.java User's Expected Interface

Definition

  • AdapterDelegate.java
package com.ryo.design.pattern.note.adapter;

/**
 * Ways of aggregation
 * @author houbinbin
 * @version 1.0
 * @on 2017/8/10
 * @since 1.7
 */
public class AdapterDelegate implements Target {


    private Adaptee adaptee;

    public AdapterDelegate(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    /**
     * Use a delegated approach
     */
    public void twoPlugin() {
        adaptee.twoPlugin();
    }

    @Override
    public void standardThreePlugin() {
        System.out.println("Adapter with three plugins...");
    }

}
  • Adaptee.java
package com.ryo.design.pattern.note.adapter;

/**
 * Objects of two births
 * @author houbinbin
 * @version 1.0
 * @on 2017/8/10
 * @since 1.7
 */
public class Adaptee {

    /**
     * There are only two jacks...
     */
    public void twoPlugin() {
        System.out.println("Only has two plugins...");
    }

}
  • Adapter.java
package com.ryo.design.pattern.note.adapter;

/**
 * Ways of Inheritance
 * @author houbinbin
 * @version 1.0
 * @on 2017/8/10
 * @since 1.7
 */
public class Adapter extends Adaptee implements Target {


    @Override
    public void standardThreePlugin() {
        System.out.println("Adapter with three plugins...");
    }

}
  • Target.java
package com.ryo.design.pattern.note.adapter;

/**
 * The interface the customer expects. Targets can be concrete or abstract classes or interfaces.
 * @author houbinbin
 * @version 1.0
 * @on 2017/8/10
 * @since 1.7
 */
public interface Target {

    /**
     * Standard interface.
     * 1. Standard three plugs on the plugboard
     */
    void standardThreePlugin();

}

test

  • Main.java
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 2012-2018. houbinbini Inc.
 * design-pattern All rights reserved.
 */

package com.ryo.design.pattern.note.adapter;

/**
 * <p> </p>
 *
 * <pre> Created: 2018/5/13 5 p.m. </pre>
 * <pre> Project: design-pattern  </pre>
 *
 * @author houbinbin
 * @version 1.0
 * @since JDK 1.7
 */
public class Main {

    public static void main(String[] args) {
        Adapter adapter = new Adapter();
        adapter.standardThreePlugin();
        adapter.twoPlugin();


        AdapterDelegate delegate = new AdapterDelegate(new Adaptee());
        delegate.standardThreePlugin();
        delegate.twoPlugin();
    }

}
  • test result
Adapter with three plugins...
Only has two plugins...
Adapter with three plugins...
Only has two plugins...

Realization way

UML & Code

UML

The UML diagram is as follows

Code

Code address

Adapter mode

Series navigation

Series navigation

Posted by etnastyles on Sun, 16 Dec 2018 06:36:03 -0800