📜  门| GATE CS Mock 2018年|第31章(1)

📅  最后修改于: 2023-12-03 14:58:22.882000             🧑  作者: Mango

GATE CS模拟考试2018年 - 第31章

这是关于GATE CS模拟考试2018年第31章的介绍。本章主要涉及以下主题:

  • 策略模式
  • 工厂模式
  • 单例模式
  • 适配器模式
  • 外观模式
策略模式

策略模式是一种非常常见的设计模式,它允许我们动态地改变对象的行为。这种模式通常涉及三个关键元素:一个抽象策略接口、多个具体策略类和一个上下文类。

在这种模式下,我们可以为不同的具体策略类提供不同的行为,然后在上下文类中使用它们。这使得我们可以在运行时动态地选择不同的算法或行为,而不必改变上下文类的接口或结构。

以下是一个示例实现:

public interface Strategy {
    void execute();
}

public class ConcreteStrategy1 implements Strategy {
    public void execute() {
        System.out.println("Executing strategy 1");
    }
}

public class ConcreteStrategy2 implements Strategy {
    public void execute() {
        System.out.println("Executing strategy 2");
    }
}

public class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void executeStrategy() {
        strategy.execute();
    }
}
工厂模式

工厂模式是一种常见的创建型模式,它允许我们在不暴露创建逻辑的情况下创建对象。例如,我们可以使用工厂模式创建不同类型的按钮,这样就可以在不了解按钮创建逻辑的情况下创建其实例。

以下是一个简单的工厂类实现:

public interface Button {
    void render();
    void onClick();
}

public class HTMLButton implements Button {
    public void render() {
        System.out.println("<button>HTML Button</button>");
    }

    public void onClick() {
        System.out.println("Click! Button says - 'Hello, World!'");
    }
}

public class WindowsButton implements Button {
    public void render() {
        System.out.println("Windows Button");
    }

    public void onClick() {
        System.out.println("Click! Button says - 'Hello, World!'");
    }
}

public class Dialog {
    public void renderWindow(Button button) {
        button.render();
        button.onClick();
    }
}

public class ButtonFactory {
    public static Button createButton(String os) {
        if (os.equals("Windows")) {
            return new WindowsButton();
        } else {
            return new HTMLButton();
        }
    }
}
单例模式

单例模式是一种常用的创建型模式,用于创建只能有一个实例的类。单例模式通常涉及到一个私有构造函数和一个返回唯一实例的静态方法。

以下是一个基本的单例示例:

public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
适配器模式

适配器模式是一种结构型模式,用于将一个接口转换成另一个接口。适配器模式通常涉及到一个适配器类,该类实现了目标接口,并包含一个适配者类的对象。

以下是一个示例:

public interface MediaPlayer {
    public void play(String audioType, String fileName);
}

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

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

    public void playMp4(String fileName) {
    }
}

public class Mp4Player implements AdvancedMediaPlayer{
    public void playVlc(String fileName) {
    }

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

public class MediaAdapter implements MediaPlayer {
    AdvancedMediaPlayer advancedMusicPlayer;

    public MediaAdapter(String audioType){
        if (audioType.equalsIgnoreCase("vlc") ){
            advancedMusicPlayer = new VlcPlayer();

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

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

public class AudioPlayer implements MediaPlayer {
    MediaAdapter mediaAdapter;

    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");
        }
    }
}
外观模式

外观模式是一种结构型设计模式,它提供了一种简单的方法来访问复杂子系统的功能。外观模式通常涉及一个外观类,该类提供了一个简单的接口来访问子系统中的功能。

以下是一个示例:

public interface Shape {
    void draw();
}

public class Rectangle implements Shape {
    public void draw() {
        System.out.println("Rectangle::draw()");
    }
}

public class Square implements Shape {
    public void draw() {
        System.out.println("Square::draw()");
    }
}

public class Circle implements Shape {
    public void draw() {
        System.out.println("Circle::draw()");
    }
}

public class ShapeMaker {
    private Shape circle;
    private Shape rectangle;
    private Shape square;

    public ShapeMaker() {
        circle = new Circle();
        rectangle = new Rectangle();
        square = new Square();
    }

    public void drawCircle(){
        circle.draw();
    }
    public void drawRectangle(){
        rectangle.draw();
    }
    public void drawSquare(){
        square.draw();
    }
}