1. 程式人生 > >模擬電視機樣品生產過程(工廠+單例+原型)

模擬電視機樣品生產過程(工廠+單例+原型)

建立型模式綜合練習

一、作業內容

利用設計模式模擬電視機樣品生產過程。海爾工廠負責生產海爾樣機,海信工廠負責生產海信樣機,如果需要生產TCL或創維樣機,只需要對應增加新的TCL工廠或創維工廠即可。設計需要滿足如下要求:
1.生產的電視機物件具有自我複製能力(淺拷貝)
2.由於是樣品機,因此每種品牌電視機僅允許生產一臺,且僅允許該電視 機自我複製一次。
3.使用工廠方法模式、單例模式和原型模式綜合起來設計。

二、UML類圖

在這裡插入圖片描述

三、開發環境

Windows10、Java8、Eclipse

四、工程檔案架構

在這裡插入圖片描述

五、原始碼

1.factory包
TVFactory.java

package factory;

import TV.TV;

public interface TVFactory {
	public TV produceTV();
}

HaierTVFactory.java

package factory;
import TV.HaierTV;
import TV.TV;

public class HaierTVFactory implements TVFactory{

	@Override
	public TV produceTV() {
		
		return HaierTV.getHaierTVInstance();
	}

}

HisenseTVFfactory.java

package factory;

import TV.HisenseTV;
import TV.TV;

public class HisenseTVFfactory implements TVFactory{

	@Override
	public TV produceTV() {
		
		return HisenseTV.getHisenseTVInstance();
	}

}

2.TV包

TV.java

package TV;

public interface TV {
	public void play();
	public TV clone();
	
}

HaierTV.java

package TV;

public class HaierTV implements TV, Cloneable{

	private int COPY_MAX = 1;
	private static HaierTV instance = null;
	
	@Override
	public void play() {
		System.out.println("海爾電視機播放。。。");
	}
	@Override
	public TV clone() {
		if(COPY_MAX == 1) {
			HaierTV clone = null;
			try {
				clone = (HaierTV) super.clone();
				COPY_MAX = 0;
			} catch (CloneNotSupportedException e) {
				
				e.printStackTrace();
			}
			return clone;
		}else {
			try {
				throw new Exception("只能複製一次,您已經複製過了,不可再次複製!");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			return null;
		}
	}
	
	private HaierTV() {

	}
	
	public static HaierTV getHaierTVInstance() {
		if(instance == null) {
			instance = new HaierTV();
			return instance;
		}else {
			return instance;
		}
			
	}
}

HisenseTV.java

package TV;

public class HisenseTV implements TV, Cloneable{

	private int COPY_MAX = 1;
	private static HisenseTV instance = null;
	
	@Override
	public void play() {
		System.out.println("海信電視機播放。。。");
	}
	@Override
	public TV clone() {
		if(COPY_MAX == 1) {
			HisenseTV clone = null;
			try {
				clone = (HisenseTV) super.clone();
				COPY_MAX = 0;
			} catch (CloneNotSupportedException e) {
				
				e.printStackTrace();
			}
			return clone;
		}else {
			try {
				throw new Exception("只能複製一次,您已經複製過了,不可再次複製!");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			return null;
		}
	}
	
	private HisenseTV() {
		
	}
	
	public static HisenseTV getHisenseTVInstance() {
		if(instance == null) {
			instance = new HisenseTV();
			return instance;
		}else {
			return instance;
		}
			
	}
	
}

3.util包

XMLUtil.java

package util;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLUtil {
	public static Object getBean() {
		try {
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dFactory.newDocumentBuilder();
			Document doc = (Document) builder.parse(new File("config.xml"));
			
			NodeList nList = doc.getElementsByTagName("className");
			Node classNode = (Node) nList.item(0).getFirstChild();
			String className = classNode.getNodeValue();
			
			Class c = Class.forName(className);
			Object object = c.newInstance();
			return object;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

4.config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<className>factory.HaierTVFactory</className>
</config>

5.test包
Client.java

package test;

import TV.HisenseTV;
import TV.TV;
import factory.TVFactory;
import util.XMLUtil;

//客戶端,全部是面向抽象程式設計
public class Client {
	public static void main(String[] args) {
		try {
			TV tv;
			TVFactory factory;
			factory = (TVFactory)XMLUtil.getBean();
			tv = factory.produceTV();
			tv.play();
			
			TV tv2 = tv.clone();
			TV tv3 = factory.produceTV();
			tv2.play();//淺拷貝,和tv執行結果一樣
			tv3.play();
			System.out.println("tv==tv3?"+(tv==tv3));//證明是單例模式,返回true
			
			//證明只能複製一次
			//TV tv4 = tv.clone();//丟擲異常:只能複製一次,您已經複製過了,不可再次複製!
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

六、實驗結果

在這裡插入圖片描述