示例一:给笔记本电脑充电
需求:只有两孔的插座,如何给需要三孔插座的笔记本充电?
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不支持播放