为了更有利于学习,我将模型实现与分析分开写
适配器模式
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。
这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。
我们通过下面的实例来演示适配器模式的使用。其中,音频播放器设备只能播放 mp3 文件,通过使用一个更高级的音频播放器来播放 vlc 和 mp4 文件。
实现
我们有一个 MediaPlayer 接口和一个实现了 MediaPlayer 接口的实体类 AudioPlayer。默认情况下,AudioPlayer 可以播放 mp3 格式的音频文件。
我们还有另一个接口 AdvancedMediaPlayer 和实现了 AdvancedMediaPlayer 接口的实体类。该类可以播放 vlc 和 mp4 格式的文件。
我们想要让 AudioPlayer 播放其他格式的音频文件。为了实现这个功能,我们需要创建一个实现了 MediaPlayer 接口的适配器类 MediaAdapter,并使用 AdvancedMediaPlayer 对象来播放所需的格式。
AudioPlayer 使用适配器类 MediaAdapter 传递所需的音频类型,不需要知道能播放所需格式音频的实际类。AdapterPatternDemo,我们的演示类使用 AudioPlayer 类来播放各种格式。
一.为媒体播放器和更高级的媒体播放器创建接口
MediaPlayer.java
public interface MediaPlayer{
public void play(String audioType, String fileName)
}
AdvancedMediaPlayer.java
public interface AdvancedMediaPlayer{
public void playVlc(String fileName)
}
public playMp4(String fileName)
二.、创建实现了AdvancedMediaPlayer接口的实体类
VlcPlayer.java
public class VlcPlayer implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {
system.out.println("playing vlc file.Name:"+fileName);
}
public void playMp4(String fileName) {
//什么也不做
}
}
Mp4Player.java
public class VlcPlayer implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {
//什么也不做
}
public void playMp4(String fileName) {
system.out.println("playing Mp4 file.Name:"+fileName);
}
}
三、创建实现了MediaPlayer接口的适配器类。适配器类根据文件类型,创建不同的播放类。
MediaAdapter.java
public class MediaAdapter implements Media Player{
AdvancedMediaPlayer advancedMusicPlayer;
public MediaAdapter(String audioType){
if (audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer=new vlcPlayer();
}else if (audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer=new Mp4Player();
}
}
@override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer.playVlc(fileName);
}else if(audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer.playMp4(fileName);
}
}
}
四、创建实现了MediaPlayer接口的实体类。
AudioPlayer.java
public class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;
@Override
public void play(String audioType, String fileName) {
//播放 mp3 音乐文件的内置支持
if(audioType.equalsIgnoreCase("mp3")){
System.out.println("Playing mp3 file. Name: "+ fileName);
}
//mediaAdapter 提供了播放其他文件格式的支持
else if(audioType.equalsIgnoreCase("vlc")
|| audioType.equalsIgnoreCase("mp4")){
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
}
else{
System.out.println("Invalid media. "+
audioType + " format not supported");
}
}
}
五、使用Audioplayer来播放不同类型的音频格式
AdapterPlayerDemo.java
public class AdapterPatternDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "beyond the horizon.mp3");
audioPlayer.play("mp4", "alone.mp4");
audioPlayer.play("vlc", "far far away.vlc");
audioPlayer.play("avi", "mind me.avi");
}
}
六、
执行程序,输出结果:
Playing mp3 file. Name: beyond the horizon.mp3
Playing mp4 file. Name: alone.mp4
Playing vlc file. Name: far far away.vlc
Invalid media. avi format not supported
桥接模式
桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。
这种模式涉及到一个作为桥接的接口,使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。
我们通过下面的实例来演示桥接模式(Bridge Pattern)的用法。其中,可以使用相同的抽象类方法但是不同的桥接实现类,来画出不同颜色的圆。
一、创建桥接实现接口
DrawAPI.java
public interface DrawAPI{
public void drawCircle (int radius,int x,int y);
}
二、创建实现了DrawAPI接口的实体桥接实现类。
RedCiecle.java
pulic class RedCircle implement DrawAPI{
@Override
public void drawCiecle(int radius,int x,int y){
System.out.println("Drawing Circle[color: green,radius:"
+radius+",x"+x+","+y+"]";
)
}
}
GreenCircle.java
public class GreenCircle implements DrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: green, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
三、使用DrawAPI接口创建抽象类Shape,shape的draw功能委任给实现了drawAPI接口的类来实现
Shape.java
public abstract calss Shape{
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawApi=drawAPI;
}
public abstract void draw();
}
四、创建实现了Shape接口的实体类。
Circle.java
public class Circle extends Shape {
private int x, y, radius;
public Circle(int x, int y, int radius, DrawAPI drawAPI) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
五、使用 Shape 和 DrawAPI 类画出不同颜色的圆。
BridgePatternDemo.java
public class BridgePatternDemo {
public static void main(String[] args) {
Shape redCircle = new Circle(100,100, 10, new RedCircle());
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
redCircle.draw();
greenCircle.draw();
}
}
六、执行程序,输出结果:
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]
过滤器模式
比较简单,可以参考https://www.runoob.com/design-pattern/filter-pattern.html