不同类的接口设计模式
创始人
2025-01-09 07:00:27
0

不同类的接口设计模式是一种将不同类之间的接口进行设计和组织的方法,以便更好地实现代码的复用和可扩展性。下面给出几种常见的不同类的接口设计模式及其代码示例。

  1. 适配器模式(Adapter Pattern) 适配器模式用于将一个类的接口转换成客户端所期望的另一个接口,使得原本由于接口不兼容而无法一起工作的类可以一起工作。
public interface MediaPlayer {
    void play(String audioType, String fileName);
}

public interface AdvancedMediaPlayer {
    void playVlc(String fileName);
    void playMp4(String fileName);
}

public class VlcPlayer implements AdvancedMediaPlayer {
    @Override
    public void playVlc(String fileName) {
        System.out.println("Playing vlc file. Name: " + fileName);
    }

    @Override
    public void playMp4(String fileName) {
        // do nothing
    }
}

public class Mp4Player implements AdvancedMediaPlayer {
    @Override
    public void playVlc(String fileName) {
        // do nothing
    }

    @Override
    public void playMp4(String fileName) {
        System.out.println("Playing mp4 file. Name: " + fileName);
    }
}

public class MediaAdapter implements MediaPlayer {
    private AdvancedMediaPlayer advancedMediaPlayer;

    public MediaAdapter(String audioType) {
        if (audioType.equalsIgnoreCase("vlc")) {
            advancedMediaPlayer = new VlcPlayer();
        } else if (audioType.equalsIgnoreCase("mp4")) {
            advancedMediaPlayer = new Mp4Player();
        }
    }

    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("vlc")) {
            advancedMediaPlayer.playVlc(fileName);
        } else if (audioType.equalsIgnoreCase("mp4")) {
            advancedMediaPlayer.playMp4(fileName);
        }
    }
}

public class AudioPlayer implements MediaPlayer {
    private MediaAdapter mediaAdapter;

    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("mp3")) {
            System.out.println("Playing mp3 file. Name: " + fileName);
        } 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");
        }
    }
}

public class Main {
    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");
    }
}
  1. 桥接模式(Bridge Pattern) 桥接模式用于将抽象部分与实现部分分离,使它们可以独立地变化。可以通过桥接模式将不同类的接口进行组合,实现更灵活的类之间的耦合。
public interface DrawAPI {
    void drawCircle(int radius, int x, int y);
}

public class RedCircle implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }
}

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: " + y + "]");
    }
}

public abstract class Shape {
    protected DrawAPI drawAPI;

    protected Shape(DrawAPI drawAPI) {
        this.drawAPI = drawAPI;
    }

    public abstract void draw();
}

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;
    }

    @Override
    public void draw() {
        drawAPI.drawCircle(radius, x, y);
    }
}

public class Main {

相关内容

热门资讯

微扑克游戏辅助器!aa pok... 微扑克游戏辅助器!aa poker下载地址(透视)起初是有挂(详细辅助曝光教程)1、超多福利:超高返...
透视软件!wpk透视脚本链接,... 透视软件!wpk透视脚本链接,约局吧德州透视(详细辅助系统教程);1、起透看视 wpk透视脚本链接透...
wpk提高胜率!wpk微扑克俱... 您好,wpk提高胜率这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这...
透视免费!wpk透视辅助靠谱吗... 透视免费!wpk透视辅助靠谱吗,aapoker怎么提高中牌率(详细辅助攻略教程)亲,关键说明,wpk...
德州ai辅助有用!poker有... 德州ai辅助有用!poker有挂辅助吗(透视)原来真的有挂(详细辅助透明教程);德州ai辅助有用!p...
透视脚本!智星德州菠萝透视插件... 透视脚本!智星德州菠萝透视插件工具,aapoker透视脚本(详细辅助专业教程);1、每一步都需要思考...
wpk有辅助挂!wepoke支... wpk有辅助挂!wepoke支持安卓吗(透视)往昔存在有挂(详细辅助扑克教程)(1)wpk有辅助挂!...
透视总结!aapoker脚本怎... 透视总结!aapoker脚本怎么用,wepoker轻量版透视方法(详细辅助微扑克教程)1)aapok...
德州ai辅助!wpk辅助软件查... 德州ai辅助!wpk辅助软件查得出来吗(透视)竟然真的有挂(详细辅助wepoke教程)是一款可以让一...
透视实锤!红龙poker有辅助... 透视实锤!红龙poker有辅助吗,cloudpoker作弊(详细辅助切实教程);1.红龙poker有...