不同类的接口设计模式
创始人
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 {

相关内容

热门资讯

透视能赢!pokemmo脚本辅... 透视能赢!pokemmo脚本辅助-关于开挂透视辅助攻略(有挂实锤);pokemmo脚本辅助是一种具有...
查到实测辅助“佛手在线辅助器苹... 查到实测辅助“佛手在线辅助器苹果版”原本有开挂辅助插件(有挂规律);佛手在线辅助器苹果版免费下载原版...
透视模拟器!wejoker辅助... 透视模拟器!wejoker辅助脚本-解迷开挂透视辅助插件(有挂猫腻)1、wejoker辅助脚本ai机...
必备辅助推荐“wepoker安... 您好:wepoker安装教程这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
透视黑科技!wepoker有辅... 透视黑科技!wepoker有辅助-详情开挂透视辅助插件(的确有挂)wepoker有辅助辅助器中分为三...
必知教程“hhpoker可以开... 您好:hhpoker可以开透视这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
透视智能ai!wpk模拟器多开... 透视智能ai!wpk模拟器多开-详情开挂透视辅助攻略(有挂技巧)1、wpk模拟器多开透视辅助简单,w...
玩家必备攻略“wepoker辅... 玩家必备攻略“wepoker辅助软件视频”总是有开挂辅助助手(有挂方法);wepoker辅助软件视频...
透视好友房!pokerrrr2... 透视好友房!pokerrrr2辅助-揭露开挂透视辅助app(有挂技巧)1)pokerrrr2辅助辅助...
三分钟了解“钱塘十水三插件”固... 三分钟了解“钱塘十水三插件”固有有开挂辅助器(有挂教程);小薇(透视辅助)致您一封信;亲爱钱塘十水三...