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);
}
}
欢迎阅读,以及欢迎指出不足之处,如果点赞就更好了;
祝您学习愉快~😊😊😊