這是抽象工廠麼?

choumao發表於2007-01-19

abstract class Factory {

	public abstract VideoMpeg createVideoMpeg();
	public abstract VideoRm createVideoRm();
	public abstract AudioMp3 createAudioMp3();
	public abstract AudioWav createAudioWav();
}

class ProduceFactory extends Factory {

	public VideoMpeg createVideoMpeg() {
		return new VideoMpeg();
	}

	public VideoRm	createVideoRm() {
		return new VideoRm();
	}
	
	public AudioMp3 createAudioMp3() {
		return new AudioMp3();
	}

	public AudioWav createAudioWav() {
		return new AudioWav();
	}

}

class AudioMp3 {

	public AudioMp3() {
		// TODO: 在此處新增建構函式邏輯	
	}

	public void play() {
		System.out.println("this is mp3 play...");
	}
}

class AudioWav {

	public AudioWav() {
		// TODO: 在此處新增建構函式邏輯	
	}

	public void play() {
		System.out.println("this is wav play...");
	}
}

class VideoRm {

	public VideoRm() {
		// TODO: 在此處新增建構函式邏輯	
	}

	public void play() {
		System.out.println("this is rm play...");
	}
}

class VideoMpeg {

	public VideoMpeg() {
		// TODO: 在此處新增建構函式邏輯	
	}

	public void play() {
		System.out.println("this is mpeg play...");
	}
}

public class AbstractFactory {

	public static void main(String[] args) {
		
		Factory _factory = new ProduceFactory();
		
		AudioMp3 _mp3 = _factory.createAudioMp3();
		VideoRm  _rm  = _factory.createVideoRm();

		_mp3.play();
		_rm.play();
	}
}
<p>

看了抽象工廠的概念,寫了一段程式碼,不知道寫對了沒有。請賜教謝謝 :)

相關文章