Java # Design Patrern 设计模式例子---源代码

Java # Design Patrern 设计模式例子---源代码

练习1-AudioClipManager

/**
 * This class can be used to avoid playing two audio clips at the same
 * time. The class has only one instance that can be accessed through
 * its getInstance method.  When you play audio clips through that
 * object, it stops the last audio clip it was playing before
 * it starts the newly requested one.  If all audio clips are played
 * through the AudioClipManager object then there will never be more
 * than one audio clip playing at the same time.
 */
public class AudioClipManager implements AudioClip {
    private static final AudioClipManager myInstance = new AudioClipManager();
    /**
     * The previously requested audio clip.
     */
    private AudioClip prevClip;
    private AudioClipManager() {}
    public static AudioClipManager getInstance() {
        return myInstance;
    }
    /**
     * Start playing this audio clip. Each time this method is called,
     * the clip is restarted from the beginning.
     */
    public void play() {
        if (prevClip != null) {
            prevClip.play();
        }
    }
    /**
     * Stop the previously requested audio clip and play the given audio
     * clip.
     *
     * @param clip the new audio clip to play
     */
    public void play(AudioClip clip) {
        if (prevClip != null) {
            prevClip.stop();
        }
        prevClip = clip;
        clip.play();
    }

    /**
     * Starts playing this audio clip in a loop.
     */
    public void loop() {
        if (prevClip != null) {
            prevClip.loop();
        }
    }
    /**
     * Stop the previously requested audio clip and play the given audio
     * clip in a loop.
     *
     * @param clip the new audio clip to play
     */
    public void loop(AudioClip clip) {
        if (prevClip != null) {
            prevClip.stop();
        }
        prevClip = clip;
        clip.loop();
    }
    /**
     * Stops playing this audio clip.
     */
    public void stop() {
        if (prevClip != null) {
            prevClip.stop();
        }
    }
}

//另外一个类
public interface AudioClip {
    /**
     * Starts playing the audio clip.
     */
    void play();
    /**
     * Starts looping the audio clip.
     */
    void loop();
    /**
     * Stops playing the audio clip.
     */
    void stop();
}

练习2-Bibliothek

import java.util.List;
public interface Aufnehmen {
    public void neuesBuch(List<Buch> liste, Buch buch);
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;

public class Bibliothek {
    public static final int ARRAYLIST = 1;
    public static final int LINKEDLIST = 2;
    private List<Buch> bestand;
    public Bibliothek(int variante) {
        switch (variante) {
        case ARRAYLIST:
            bestand = new ArrayList<Buch>();
            break;
        case LINKEDLIST:
            bestand = new LinkedList<Buch>();
            break;
        default:
            throw new IllegalArgumentException("unbekanntes Argument: " + variante);
        }
    }
    public void neuesBuchAufnehmen(Buch neuesBuch, Aufnehmen algorithmus) {
        algorithmus.neuesBuch(bestand, neuesBuch);
    }
}
public class Buch implements Comparable<Buch> {
    private String isbn;
    private String titel;

    public Buch(String isbn, String titel) {
        this.isbn = isbn;
        this.titel = titel;
    }

    @Override
    public int compareTo(Buch buch) {
        return isbn.compareTo(buch.isbn);
    }
    public String getIsbn() {return isbn;}
    @Override
    public String toString() {
        return "ISBN: " + isbn + ", Titel: " + titel;
    }
}
import java.util.List;
public class EinfachesAufnehmen implements Aufnehmen {
    @Override
    public void neuesBuch(List<Buch> liste, Buch buch) {
        liste.add(buch);
    }
}
import java.util.Collections;
import java.util.List;
public class SortiertesAufnehmen implements Aufnehmen {
    @Override
    public void neuesBuch(List<Buch> liste, Buch buch) {
        liste.add(buch);
        Collections.sort(liste);
    }
}

欢迎阅读,以及欢迎指出不足之处,如果点赞就更好了;
祝您学习愉快~😊😊😊

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值