美文网首页
【设计模式】-- 适配器模式

【设计模式】-- 适配器模式

作者: 晴岚_Lanny | 来源:发表于2017-07-04 16:15 被阅读0次

示例一:给笔记本电脑充电

需求:只有两孔的插座,如何给需要三孔插座的笔记本充电?

public class TwoPlug {
    
    //使用两孔插座充电
    public void powerWithTwo(){
        System.out.println("使用两孔插座充电,充电中。。。");
    }

}
/*
 * 定义三孔插座接口
 */
public interface ThreePlugInterface {
    
    //使用三孔插座充电
    public void powerWithThree();

}
/*
 * 两孔转三孔插座的适配器
 */
public class TwoPlugAdapter implements ThreePlugInterface {
    
    private TwoPlug plug;

    public TwoPlugAdapter(TwoPlug plug) {
        this.plug = plug;
    }

    @Override
    public void powerWithThree() {
        System.out.println("两孔转三孔,转化成功。。。");
        plug.powerWithTwo();

    }

}
public class NoteBook {
    
    private ThreePlugInterface plug;
    
    public NoteBook(ThreePlugInterface plug){
        this.plug = plug;
    }
    
    //使用插座充电
    public void charge(){
        plug.powerWithThree();
    }

    public static void main(String[] args) {
        //笔记本要充电,但是我只有两孔的插座,如何给笔记本充电
        TwoPlug two =  new TwoPlug();
        ThreePlugInterface three =  new TwoPlugAdapter(two);//把二相的转三相
        NoteBook nb =  new NoteBook(three);
        nb.charge();
    }

}

输出结果

两孔转三孔,转化成功。。。
使用两孔插座充电,充电中。。。
/*
 * 采用继承式的插座适配器
 */
public class TwoPlugAdapterExtends extends TwoPlug implements ThreePlugInterface {

    @Override
    public void powerWithThree() {
        System.out.println("采用继承式的插座适配器。。。");
        this.powerWithTwo();

    }

}

通过继承式适配器来实现

    public static void main(String[] args) {
        //笔记本要充电,但是我只有两孔的插座,如何给笔记本充电
//      TwoPlug two =  new TwoPlug();
//      ThreePlugInterface three =  new TwoPlugAdapter(two);//把二相的转三相
//      NoteBook nb =  new NoteBook(three);
//      nb.charge();
        
        //继承式的插座适配器
        ThreePlugInterface three =  new TwoPlugAdapterExtends();
        NoteBook nb =  new NoteBook(three);
        nb.charge();
    }

输出结果

采用继承式的插座适配器。。。
使用两孔插座充电,充电中。。。

示例二:多媒体文件的播放

需求:播放器如何实现不同类型文件的播放?

/*
 * 定义一个统一的播放方法
 */
public interface MediaPlayer {
    
    public void play(String audioType, String fileName);

}
/*
 * 不同的类型走不同的方法
 */
public interface VideoMediaPlayer {

    public void playAVI(String fileName);

    public void playMP4(String fileName);

}
/*
 * AVI文件的实现
 */
public class AVIPlayer implements VideoMediaPlayer {


    @Override
    public void playAVI(String fileName) {
        
        System.out.println("开始播放AVI文件,名字为:" + fileName);
        
    }

    @Override
    public void playMP4(String fileName) {
        
    }

}
/*
 * MP4文件的实现
 */
public class Mp4Player implements VideoMediaPlayer {

    @Override
    public void playAVI(String fileName) {

    }

    @Override
    public void playMP4(String fileName) {
        System.out.println("开始播放MP4文件,名字为: " + fileName);
    }

}
/*
 * 根据不同的类型去实例化不同的对象
 */
public class MediaAdapter implements MediaPlayer {

    private VideoMediaPlayer videoMediaPlayer;

    public MediaAdapter(String audioType) {

        if (audioType.equalsIgnoreCase("avi")) {
            videoMediaPlayer = new AVIPlayer();

        } else if (audioType.equalsIgnoreCase("mp4")) {
            videoMediaPlayer = new Mp4Player();
        }
    }

    @Override
    public void play(String audioType, String fileName) {

        if (audioType.equalsIgnoreCase("avi")) {
            videoMediaPlayer.playAVI(fileName);
        } else if (audioType.equalsIgnoreCase("mp4")) {
            videoMediaPlayer.playMP4(fileName);
        }

    }

}
/*
 * 根据不同的类型走不同的方法
 */
public class Adapter implements MediaPlayer {
    
    private MediaAdapter mediaAdapter; 

    @Override
    public void play(String audioType, String fileName) {

        if (audioType.equalsIgnoreCase("avi") || audioType.equalsIgnoreCase("mp4")) {
            mediaAdapter = new MediaAdapter(audioType);
            mediaAdapter.play(audioType, fileName);
        }
        else {
            System.out.println("无效的文件,不支持播放" + audioType + "类型");
        }
    }

}
/*
 * 测试方法
 */
public class AdapterPatternTest {

    public static void main(String[] args) {

        Adapter Player = new Adapter();
        Player.play("mp3", "Test MP3 File.mp3");
        Player.play("mp4", "Test MP4 File.mp4");
        Player.play("avi", "Test AVI File.avi");
        Player.play("flv", "Test FLV File.avi");
    }
    
}

测试结果

无效的文件,Test MP3 File.mp3不支持播放
开始播放MP4文件,名字为: Test MP4 File.mp4
开始播放AVI文件,名字为:Test AVI File.avi
无效的文件,Test FLV File.avi不支持播放

相关文章

  • 简说设计模式之适配器模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是适配器模式 适配器模式(Adapter...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 最常用的设计模式---适配器模式(C++实现)

    适配器模式属于结构型的设计模式,它是结构型设计模式之首(用的最多的结构型设计模式)。 适配器设计模式也并不复杂,适...

  • iOS设计模式(5)策略模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(6)模板模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(7)建造者模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(4)抽象工厂模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(1)简单工厂模式

    设计模式系列文章 《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象工厂...

  • iOS设计模式(2)工厂模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象...

  • iOS设计模式(8)外观模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

网友评论

      本文标题:【设计模式】-- 适配器模式

      本文链接:https://www.haomeiwen.com/subject/kbbthxtx.html